How to Generate GUID in Asp.Net



GUID stands for Global Unique Identifier.

A GUID is a
128-bit integer (16 bytes) that you can use across all computers and networks wherever a unique identifier is required.

Here are some frequently asked questions about GUIDs. Without getting into detail, let me tell you there are 2^122 or 5,316,911,983,139,663,491,615,228,241,121,400,000 possible combination.

System.GUID class represents a GUID in .NET Framework. In Sql Server We can generate GUID in Sql Server with the help of NEWID () function.

Strings like this are typical GUIDs:
  • C56A4180-65AA-42EC-A945-5FD21DEC0538
  • c56a4180-65aa-42ec-a945-5fd21dec0538
  • {c56a4180-65aa-42ec-a945-5fd21dec0538}
  • C56a418065aa426ca9455fd21deC0538
  • {C56a418065aa426ca9455fd21deC0538}
Strings like this are NOT GUIDs at all:
  • C56A4180-65AA-42EC-A945-5FD21DEC (too short)
  • ! (BANG!)
  • x56a4180-h5aa-42ec-a945-5fd21dec0538 (non-hex characters)
  • your mom (your mom is not a GUID)
  • null (Cannot convert null to 'System.Guid' because it is a non-nullable value type)
  • (empty string is not a GUID)
The format of the converted string can be modified using a format specifier. This is passed as a string parameter to the ToString method. The four available format specifiers are listed in the table below.
Specifier
Description
Examples
D
Default format of thirty-two digits with groups separated by hyphens.
"00000000-0000-0000-0000-000000000000"
N
A series of thirty-two hexadecimal digits with no separators between groups.
"00000000000000000000000000000000"
B
Default format surrounded by brackets.
"{00000000-0000-0000-0000-000000000000}"
P
Default format surrounded by parentheses.
"(00000000-0000-0000-0000-000000000000)"

Guid. ToString (String, IFormatProvider) has the following parameters.
  • Format - A single format specifier that indicates how to format the value of this Guid. The format parameter can be "N", "D", "B", "P", or "X". If format is null or an empty string (""), "D" is used.
  • Provider - (Reserved) an object that supplies culture-specific formatting information.

HTML Code:

<div>
        <table>
            <tr>
                <td colspan="2">
                    <asp:CheckBoxList ID="CHKSelect" runat="server" RepeatDirection="Horizontal"
                        CellPadding="2" Height="28px" Width="501px">
                        <asp:ListItem Value="1">{} Braces</asp:ListItem>
                        <asp:ListItem Value="2">Uppercase</asp:ListItem>
                        <asp:ListItem Value="3">Hyphen</asp:ListItem>
                        <asp:ListItem Value="4">Empty Guid</asp:ListItem>
                        <asp:ListItem Value="5">() parentheses</asp:ListItem>
                    </asp:CheckBoxList>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    <asp:Button ID="Button1" runat="server" Text="Generate GUID" OnClick="Button1_Click"
                        Height="37px" Width="203px" />
                </td>
                <td class="style2">
                </td>
            </tr>
            <tr>
                <td colspan="2">
                </td>
            </tr>
            <tr>
                <td colspan="2" style="text-align: center; vertical-align: middle; background-color: transparent;"
                    class="style1">
                    <asp:Label ID="Label1" runat="server" Height="29px" Width="377px"></asp:Label>
                </td>
            </tr>
        </table>
        <br />
    </div>
    <hr style="width: 471px; margin-left:0px;" />
    <div>
        <table>
            <tr>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server" Font-Bold="False" Font-Size="Large" ForeColor="#3333CC"
                        Height="24px" Width="350px"></asp:TextBox>
                </td>
                <td>
                    <asp:Button ID="Button2" runat="server" Text="Validate" OnClick="Button2_Click" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
                </td>
            </tr>
        </table>
    </div>
    <div>
        <asp:Button ID="Button3" runat="server" Text="No hyphens" OnClick="Button3_Click" />
    </div>






















Code Behind:
protected void Button1_Click(object sender, EventArgs e)
        {
            Run();
}

public void Run()
        {
            String Selected = "";
            Label1.ForeColor = System.Drawing.Color.Green;
            Label1.Font.Size = 13;
            foreach (ListItem li in CHKSelect.Items)
            { if (li.Selected) { Selected += li.Value; } }

            if (Selected == "12")
            {
                Guid obj = Guid.NewGuid();
                Label1.Text = obj.ToString("B").ToUpper();
            }
            else if (Selected == "13")
            {
                Guid obj = Guid.NewGuid();
                Label1.Text = obj.ToString("B");
            }
            else if (Selected == "23")
            {
                Guid obj = Guid.NewGuid();
                Label1.Text = obj.ToString("D").ToUpper();
            }
            else if (Selected == "14")
            {
                Guid obj = Guid.NewGuid();
                Label1.Text = Guid.Empty.ToString("B");
            }
            else if (Selected == "24")
            {
                Guid obj = Guid.NewGuid();
                Label1.Text = Guid.Empty.ToString().ToUpper();
            }
            else if (Selected == "34")
            {
                Guid obj = Guid.NewGuid();
                Label1.Text = Guid.Empty.ToString("D").ToUpper();
            }
            else if (Selected == "123")
            {
                Guid obj = Guid.NewGuid();
                Label1.Text = obj.ToString("B").ToUpper();
            }
            else if (Selected == "234")
            {
                Guid obj = Guid.NewGuid();
                Label1.Text = Guid.Empty.ToString("D").ToUpper();
            }
            else if (Selected == "134")
            {
                Guid obj = Guid.NewGuid();
                Label1.Text = Guid.Empty.ToString("B");
            }
            else if (Selected == "124")
            {
                Guid obj = Guid.NewGuid();
                Label1.Text = Guid.Empty.ToString("B");
            }
            else if (Selected == "1234")
            {
                Guid obj = Guid.NewGuid();
                Label1.Text = Guid.Empty.ToString("B").ToUpper();
            }
            else if (Selected == "45")
            {
                Guid obj = Guid.NewGuid();
                Label1.Text = Guid.Empty.ToString("P");
            }
            else if (Selected == "35")
            {
                Guid obj = Guid.NewGuid();
                Label1.Text = obj.ToString("P");
            }
            else if (Selected == "25")
            {
                Guid obj = Guid.NewGuid();
                Label1.Text = obj.ToString("P").ToUpper();
            }
            else if (Selected == "345")
            {
                Guid obj = Guid.NewGuid();
                Label1.Text = Guid.Empty.ToString("P");
            }
            else if (Selected == "235")
            {
                Guid obj = Guid.NewGuid();
                Label1.Text = obj.ToString("P").ToUpper();
            }
            else if (Selected == "2345")
            {
                Guid obj = Guid.NewGuid();
                Label1.Text = Guid.Empty.ToString("P").ToUpper();
            }
            else if (Selected == "15" || Selected == "12345" || Selected == "125" || Selected == "135" || Selected == "145" || Selected == "1235" || Selected == "1245" || Selected == "1345")
            {
                Label1.ForeColor = System.Drawing.Color.Red;
                Label1.Text = "Not Possible";
            }
            else if (CHKSelect.SelectedValue == "1")
            {
                Guid obj = Guid.NewGuid();
                Label1.Text = obj.ToString("B");
            }
            else if (CHKSelect.SelectedValue == "2")
            {
                Guid obj = Guid.NewGuid();
                Label1.Text = obj.ToString().ToUpper();
            }
            else if (CHKSelect.SelectedValue == "3")
            {
                Guid obj = Guid.NewGuid();
                Label1.Text = obj.ToString();
            }
            else if (CHKSelect.SelectedValue == "4")
            {
                Guid obj = Guid.NewGuid();
                Label1.Text = Guid.Empty.ToString();
            }
            else if (CHKSelect.SelectedValue == "5")
            {
                Guid obj = Guid.NewGuid();
                Label1.Text = obj.ToString("P");
            }
        }
private static Regex isGuid = new Regex(@"^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$", RegexOptions.Compiled);
        private void IsGuid(string candidate)
        {
            if (candidate != null)
            {
                if (isGuid.IsMatch(candidate))
                {
                    Label2.ForeColor = System.Drawing.Color.Green;
                    Label2.Text = "Valid GUID! " + "<br />" + "Here it is right back at you: " + (TextBox1.Text).ToLower();
                }
                else
                {
                    Label2.ForeColor = System.Drawing.Color.Red;
                    Label2.Text = "This is not Valid GUID";
                }
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text != null)
            {
                IsGuid(TextBox1.Text);
            }
            else
            {
                Label2.Text = "Enter GUID for Validation";
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            byte[] bytes = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
            Guid id = new Guid(bytes);
            Label2.Text = id.ToString("N").ToUpper();
        }




Related Posts

Previous
Next Post »

Thanks for comments.....