.Net, ASP.NET, Programming, VB.NET, Web Development, WebForms

Generating GUIDs in .NET

Generating GUIDS in ASP.NET
Generating GUIDS in ASP.NET

GUIDs are useful tools for .NET developers and Windows developers in general. If you need a unique value for your code, then a GUID (Globally Unique Identifier) is one of the best ways to go.

One thing to keep in mind when working with GUIDs is that there is always a chance of getting a collision/duplication between GUIDs, especially if you are working with large numbers of them.

Here is an example 128 bit GUID:

2b3ed884-f3a7-447b-84cd-62e9221e6548

As you can see, based on the number of alphanumeric digits in the example above, the likelihood of getting a duplicate GUID is extremely low. However when using a GUID, there is always a chance that duplicates will appear, especially as the sample size increases, or as the method of generating the GUID changes.

It is because of this that I would suggest using them with small in-memory lists, or to uniquely identify programs in the registry, but I would be leery of using GUIDs as primary keys in a large & permanent SQL Server table (I’m certain there are people who will disagree with me on this point, but I stand firm in my belief that this is bad coding practice).

The simplest syntax for generating a GUID in VB.NET is along the following lines:

Dim guidTest As Guid = Guid.NewGuid()
TextBox1.Text = guidTest.ToString()

The code above will generate a traditional 128-bit GUID in the alphanumeric format:

12345678-1234-1234-1234-123456789abc

This is not always the best format for working with your GUIDs. When dealing with smaller sets of data, I often like to use smaller alphanumeric GUIDs without the dashes.

 

Alternate ways of generating GUIDs

The methods below make use of the GetHashCode method. Using the GetHashCode method is described quite well on MSDN. The main problem is that this method doesn’t guarantee unique return values. In fact, there is more opportunity for duplications than the pure 128 bit GUID above, so the below methods should be used with caution.

Here are a few example methods:

1)  If you use the syntax: guidTest.ToString().GetHashCode().ToString() then you will get a positive or negative Integer value.

For example:

  • -51400596
  • -1707577959
  • 155345252

2) If you add the “x” parameter to the ToString method in the following syntax: guidTest.ToString().GetHashCode().ToString(“x”) then you will no longer have to worry about positive or negative values as are generated in example #1 above. Instead you will now get randomized alphanumeric strings such as:

  • 96d4d6e9
  • bd90ec25
  • 2fca22da

3) As an alternate to using a the GetHashCode method against the GUID object, you can use it against the current Date Time using the following syntax: DateTime.Now.ToString().GetHashCode().ToString(“x”) This syntax still has the possibility for collisions, but in low volume systems this shouldn’t be much of a concern. The nice part of this is that the incrementing datetime ensures that collisions do not have a chance of happening in future times. Here are some examples of the GUIDs that you will get using this method:

  • 81f5f5b4
  • 63ef5b9
  • 2503f5b9

Sample ASP.NET Form

Here is the code for generating the GUIDs test form shown in the image at the start of this article:

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
 Protected Sub Page_Load(sender As Object, e As System.EventArgs)
 Dim guidTest As Guid = Guid.NewGuid()
 TextBox1.Text = guidTest.ToString()
 TextBox2.Text = guidTest.ToString().GetHashCode().ToString()
 TextBox3.Text = guidTest.ToString().GetHashCode().ToString("x")
 TextBox4.Text = DateTime.Now.ToString().GetHashCode().ToString("x")
 End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <title></title>
</head>
<body>
 <form id="form1" runat="server">
 <div>
 <asp:Label ID="Label1" runat="server" Text="GUID V1:"></asp:Label>
 <asp:TextBox ID="TextBox1" runat="server" Width="250px"></asp:TextBox>
 <asp:Label ID="Label5" runat="server" Text=" Guid.NewGuid().ToString()"></asp:Label>
 <br />
 <asp:Label ID="Label2" runat="server" Text="GUID V2:"></asp:Label>
 <asp:TextBox ID="TextBox2" runat="server" Width="250px"></asp:TextBox>
 <asp:Label ID="Label6" runat="server" Text=" Guid.NewGuid().ToString().GetHashCode().ToString()"></asp:Label>
 <br />
 <asp:Label ID="Label3" runat="server" Text="GUID V3:"></asp:Label>
 <asp:TextBox ID="TextBox3" runat="server" Width="250px"></asp:TextBox>
 <asp:Label ID="Label7" runat="server" Text=" Guid.NewGuid().ToString().GetHashCode().ToString('x')"></asp:Label>
 <br />
 <asp:Label ID="Label4" runat="server" Text="GUID V4:"></asp:Label>
 <asp:TextBox ID="TextBox4" runat="server" Width="250px"></asp:TextBox>
 <asp:Label ID="Label8" runat="server" Text=" DateTime.Now.ToString().GetHashCode().ToString('x')"></asp:Label>
 </div>
 </form>
</body>
</html>

See Also:

Advertisements

2 thoughts on “Generating GUIDs in .NET”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s