Pages

Tuesday, December 14, 2010

Random Number Generator Script

Random Number Generator
There are many methods to generate random number in SQL Server.
Method 1 : Generate Random Numbers (Int) between Rang
---- Create the variables for the random number generationDECLARE @Random INT;DECLARE @Upper INT;DECLARE @Lower INT
---- This will create a random number between 1 and 999SET @Lower ---- The lowest random numberSET @Upper 999 ---- The highest random numberSELECT @Random ROUND(((@Upper @Lower -1) * RAND() + @Lower), 0)SELECT @Random
Method 2 : Generate Random Float Numbers
SELECT RAND( (DATEPART(mmGETDATE()) * 100000 )
+ (
DATEPART(ssGETDATE()) * 1000 )
DATEPART(msGETDATE()) )


Method 3 : Random Numbers Quick Scripts

---- random float from 0 up to 20 - [0, 20)SELECT 20*RAND()-- random float from 10 up to 30 - [10, 30)SELECT 10 + (30-10)*RAND()--random integer BETWEEN 0AND 20 [0, 20]SELECT CONVERT(INT, (20+1)*RAND())----random integer BETWEEN 10AND 30 [10, 30]SELECT 10 CONVERT(INT, (30-10+1)*RAND())

Method 4 : Random Numbers (Float, Int) Tables Based with Time

DECLARE @t TABLErandnum float )DECLARE @cnt INTSET @cnt 0WHILE @cnt <=10000BEGIN
SET 
@cnt @cnt 1INSERT INTO @tSELECT RAND( (DATEPART(mmGETDATE()) * 100000 )
+ (
DATEPART(ssGETDATE()) * 1000 )
DATEPART(msGETDATE()) )END
SELECT 
randnumCOUNT(*)FROM @tGROUP BY randnum


Method 5 : Random number on a per row basis

---- The distribution is pretty good however there are the occasional peaks.
---- If you want to change the range of values just change the 1000 to the maximum value you want.
---- Use this as the source of a report server report and chart the results to see the distribution
SELECT randomNumberCOUNT(1countOfRandomNumberFROM (SELECT ABS(CAST(NEWID() AS binary(6)) %1000) + 1 randomNumberFROM sysobjectssampleGROUP BY randomNumberORDER BY randomNumber

Tuesday, November 23, 2010

Create a custom panel that can expand/contract and that you can nest other controls in

Create a custom panel that can expand/contract and that you can nest other controls in

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Text;
using System.IO;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web;

namespace CustomWebControls
{
    [ToolboxData("<{0}:basePanel runat=server>")]
    [Designer("System.Web.UI.Design.ReadWriteControlDesigner, System.Design")]
    [PersistChildren(true)]
    [ParseChildren(false)]
    ///

    /// Summary description for basePanel.
    /// This control creates a type of panel that has a header and footer bar.
    ///
    /// Copywrite type stuff. Please use where you want but leave my tags on here. Created by Chris Davey www.chemlock.co.uk
    ///

    ///
    public class basePanel : WebControl, INamingContainer
    {

        #region Attributes

        private Label lblTitle;
        private HtmlGenericControl bodyDiv;
        protected bool embedded;

        protected HtmlGenericControl titleDiv;
        protected Panel mainDiv;
        protected HtmlGenericControl footerDiv;

        protected ImageButton ibtnMinMax;
        protected ImageButton ibtnClose;

        protected ImageButton ibtnSave;
        protected ImageButton ibtnCancel;

        public event EventHandler SaveEvent;
        public event EventHandler CancelEvent;

        private string imgExpand;
        private string imgContract;

        protected bool create;


        #endregion Attributes

        #region Properties

        [Bindable(true)]
        [Category("Expand Butotn")]
        [DefaultValue("")]
        [Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
        public string ImageURLExpand
        {
            get { return imgExpand; }
            set { imgExpand = value; }
        }

        [Bindable(true)]
        [Category("Contract Button")]
        [DefaultValue("")]
        [Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
        public string ImageURLContract
        {
            get { return imgContract; }
            set { imgContract = value; }
        }

        [Bindable(true)]
        [Category("Close Button")]
        [DefaultValue("")]
        [Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
        public string ImageURLClose
        {
            get { return this.ibtnClose.ImageUrl; }
            set { this.ibtnClose.ImageUrl = value; }
        }

        [Bindable(true)]
        [Category("Save Button")]
        [DefaultValue("")]
        [Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
        public string ImageURLSave
        {
            get { return this.ibtnSave.ImageUrl; }
            set { this.ibtnSave.ImageUrl = value; }
        }

        [Bindable(true)]
        [Category("Cancel Button")]
        [DefaultValue("")]
        [Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
        public string ImageURLCancel
        {
            get { return this.ibtnCancel.ImageUrl; }
            set { this.ibtnCancel.ImageUrl = value; }
        }

        ///

        /// The title that will appear in the control header
        ///

        public string Title
        {
            get { return this.lblTitle.Text; }
            set { this.lblTitle.Text = value; }
        }

        ///

        /// Read Only value. Set to true of the main control window is maximised
        ///

        public bool Maximized
        {
            get { return this.mainDiv.Visible; }
        }

        ///

        /// Set or get the body css class
        ///

        public string CssClassBody
        {
            get { return this.bodyDiv.Attributes["class"]; }
            set { this.bodyDiv.Attributes.Add("class", value); }
        }

        ///

        /// Set or get the visibility of the Close button
        ///

        public bool ShowCloseButton
        {
            get { return this.ibtnClose.Visible; }
            set { this.ibtnClose.Visible = value; }
        }

        ///

        /// Set or get the visibility of the Min/Max button
        ///

        public bool ShowMinMaxButton
        {
            get { return this.ibtnMinMax.Visible; }
            set { this.ibtnMinMax.Visible = value; }
        }

        ///

        ///
        ///

        public bool ShowFooter
        {
            get { return this.footerDiv.Visible; }
            set { this.footerDiv.Visible = value; }
        }

        ///

        ///
        ///

        public bool ShowHeader
        {
            get { return this.titleDiv.Visible; }
            set { this.titleDiv.Visible = value; }
        }

        ///

        ///
        ///

        public string CssClassMain
        {
            get { return this.mainDiv.CssClass; }
            set { this.mainDiv.CssClass = value; }
        }

        ///

        /// Get or set wether the control is embedded in another control (Does not show header and footer)
        ///

        public bool Embedded
        {
            get { return this.embedded; }
            set { this.embedded = value; }
        }

        #endregion Properties

        #region Constructors

        public basePanel()
        {
            //Create control set now so that properties can be used directly on them
            this.EnsureChildControls();
            this.CssClassBody = "DetailControl";
        }

        #endregion Constructors

        #region Override Methods

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (HasControls())
            {

                //open a temp stream to render first two controls into (base control and custome control)
                System.IO.MemoryStream memstr = new System.IO.MemoryStream();
                System.IO.StreamWriter stream = new System.IO.StreamWriter(memstr);
                HtmlTextWriter wt = new HtmlTextWriter(stream);
                if (this.Controls.Count > 1)
                {
                    //render first 2 controls to stream
                    for (int i = 0; i < 2; i++)
                    {

                        this.Controls[i].RenderControl(wt);
                        wt.Flush();

                    }
                }
                else
                {
                    base.Render(wt);
                    wt.Flush();
                }

                    //set to start of stream
                    memstr.Seek(0, System.IO.SeekOrigin.Begin);

                    //read back the html from memory
                    System.IO.StreamReader rd = new System.IO.StreamReader(memstr);
                    char[] bytes = new char[memstr.Length];
                    rd.ReadBlock(bytes, 0, (int)memstr.Length);

                    //create string builder of the correct length
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    sb.EnsureCapacity((int)memstr.Length);

                    //post the bytes to the string builder
                    sb.Append(bytes);

                    //close everything
                    memstr.Close();
                    stream.Close();
                    rd.Close();
                    wt.Close();
                    if (this.Controls.Count > 2)
                    {
                        //open a temp stream to render any controls added in the IDE
                        memstr = new System.IO.MemoryStream();
                        stream = new System.IO.StreamWriter(memstr);
                        wt = new HtmlTextWriter(stream);

                        //render all other controls added during IDE
                        for (int i = 2; i < Controls.Count; i++)
                        {

                            this.Controls[i].RenderControl(wt);
                            wt.Flush();

                        }

                        //set to start of stream
                        memstr.Seek(0, System.IO.SeekOrigin.Begin);

                        //read back the html from memory
                        rd = new System.IO.StreamReader(memstr);
                        bytes = new char[memstr.Length];
                        rd.ReadBlock(bytes, 0, (int)memstr.Length);

                        //create string builder of the correct length
                        System.Text.StringBuilder sbChild = new System.Text.StringBuilder();
                        sbChild.EnsureCapacity((int)memstr.Length);

                        //post the bytes to the string builder
                        sbChild.Append(bytes);

                        //close everything
                        memstr.Close();
                        stream.Close();
                        rd.Close();
                        wt.Close();

                        //find main div and add all IDE time controls into div statement
                        sb.Replace("
", "
" + sbChild.ToString());
                    }

                    //write the html string
                    writer.WriteLine(sb.ToString());
                }
        

        }

        protected override void CreateChildControls()
        {
            if (!this.ChildControlsCreated)
            {
                this.Controls.Clear();

                base.CreateChildControls();

                this.bodyDiv = new HtmlGenericControl("div");
                this.Controls.Add(this.bodyDiv);

                this.titleDiv = new HtmlGenericControl("div");
                this.titleDiv.Attributes.Add("class", "titleDiv");
                this.bodyDiv.Controls.Add(this.titleDiv);

                this.lblTitle = new Label();
                this.lblTitle.CssClass = "titlelbl";

                this.titleDiv.Controls.Add(this.lblTitle);

                HtmlGenericControl oDivHeadBtns = new HtmlGenericControl("div");
                oDivHeadBtns.Style.Add("float", "right");
                oDivHeadBtns.Style.Add("clear", "right");
                this.titleDiv.Controls.Add(oDivHeadBtns);

                this.ibtnMinMax = new ImageButton();
                this.ibtnMinMax.CssClass = "minmaxbtn";
                this.ibtnMinMax.Click += new ImageClickEventHandler(ibtnMinMax_Click);
                oDivHeadBtns.Controls.Add(this.ibtnMinMax);

                this.ibtnClose = new ImageButton();
                this.ibtnClose.CssClass = "closebtn";
                this.ibtnClose.Click += new ImageClickEventHandler(ibtnClose_Click);
                oDivHeadBtns.Controls.Add(this.ibtnClose);

                this.mainDiv = new Panel();
                this.mainDiv.Attributes.Add("class", "mainDiv");
                this.mainDiv.Visible = true;

                this.bodyDiv.Controls.Add(this.mainDiv);

                this.footerDiv = new HtmlGenericControl("div");
                this.footerDiv.Attributes.Add("class", "footerDiv");
                this.bodyDiv.Controls.Add(this.footerDiv);

                this.ibtnSave = new ImageButton();
                this.ibtnSave.CssClass = "footerbutton";
                this.ibtnSave.Click += new ImageClickEventHandler(ibtnSave_Click);
                this.footerDiv.Controls.Add(this.ibtnSave);

                this.ibtnCancel = new ImageButton();
                this.ibtnCancel.CssClass = "footerbutton";
                this.ibtnCancel.Click += new ImageClickEventHandler(ibtnCancel_Click);
                this.footerDiv.Controls.Add(this.ibtnCancel);

            }
        }

        ///

        /// Render all panels etc as DIV not SPAN
        ///

        ///

        public override void RenderBeginTag(System.Web.UI.HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            //Show the correct image for the expand/contract button
            if (this.mainDiv.Visible)
            {
                this.ibtnMinMax.ImageUrl = this.imgContract;
            }
            else
            {
                this.ibtnMinMax.ImageUrl = this.imgExpand;
            }

            //If control is in embedded mode don't show header or footer
            if (this.embedded)
            {
                this.CssClassBody = "EmbeddedControl";
                this.ShowHeader = false;
                this.ShowFooter = false;
            }

        }

        #endregion Override Methods

        #region Public Methods

        ///

        /// Allows the status of the main div to be switched between expanded and contracted
        ///

        public void ToogleVisible()
        {
            this.mainDiv.Visible = !this.mainDiv.Visible;
            this.footerDiv.Visible = this.mainDiv.Visible;
        }

        #endregion Public Methods

        #region Events

        private void ibtnMinMax_Click(object sender, ImageClickEventArgs e)
        {
            this.ToogleVisible();
        }


        private void ibtnClose_Click(object sender, ImageClickEventArgs e)
        {
            this.Visible = false;
        }


        private void ibtnCancel_Click(object sender, ImageClickEventArgs e)
        {
            //Fire CancelEvent if there is a anything listening for it
            if (CancelEvent != null)
            {
                CancelEvent(this, e);
            }
        }


        private void ibtnSave_Click(object sender, ImageClickEventArgs e)
        {
            //Fire SaveEvent if there is a anything listening for it
            if (SaveEvent != null)
            {
                SaveEvent(this, e);
            }
        }

        #endregion Events
    }
}




How to add OpenID to your ASP.NET web site (in C# or VB.NET)

Adding OpenID support to your VB.NET web site couldn't be easier.  Here is the easiest way:
  1. Download the DotNetOpenId library.
  2. Extract the DotNetOpenId.dll from the bin directory of the .zip file you downloaded.
  3. Make sure you're using FormsAuthentication.
    1. Open Web.config
    2. Find your tag (or create it if you're sure it doesn't already exist) and change it if necessary so it looks something like this: (the really important part is just mode="Forms")
      <authentication mode="Forms">
          <forms defaultUrl="/default.aspx" loginUrl="~/login.aspx"/>
      authentication>
  4. Add a Reference to DotNetOpenId.dll from your web site.
  5. Navigate to your Login.aspx page.
  6. Drag the OpenIdLogin control from your control Toolbox to the location on your page you want it to appear. NOTE: If the controls do not appear in your Toolbox, follow these steps to add them:
    1. Right-click somewhere on the Toolbox and click Choose Items... (it can take a while before the dialog shows up)
    2. In the .NET Framework Components tab, choose Browse.
    3. Select the DotNetOpenId.dll that you extracted from the zip file.
    4. Click OK.
    5. Several controls will get added to your Toolbox.  Drag the OpenIdLogin control to your page.
  7. You should have a couple of new lines in your .aspx file:
    <%@ Register Assembly="DotNetOpenId" Namespace="DotNetOpenId.RelyingParty" TagPrefix="RP" %>
    <RP:OpenIdLogin ID="OpenIdLogin1" runat="server" />
  8. If you're starting a brand new site and don't have a way to tell who is logged in on your site yet, go ahead and drag LoginName and LoginStatus controls to some new page that you can use to check your own logged-in status.
  9. Go ahead and run your app.  You're already done.
Now, this is admittedly the simplest case.  If you have an existing userbase with usernames and passwords, you'll want to build pages to help your users make the transition.  You can certainly support dual-mode authentication, but each web site is different and you'll have to design your own plan for doing that.  But if you find you need more flexibility with how the OpenID login control looks or works, check out the OpenIdTextBox control.  If there's demand (leave a comment on this post), I'll follow up with a post or two on how to customize this.