Create a Random or Sequential GUID using ASP.NET and C#

The 2 controls above create either a GUID or a Sequential GUID. Generate a Radom GUID online or a Generate a Sequential GUID online using the above control. There is nothing too complicated about creating a random GUID. Using the below C# code provides a unique value.

string uniqueRandomKey = Guid.NewGuid().ToString();

I like to use GUIDs for primary keys on database tables. I am a firm believer that primary keys on databases should have no meaning and a GUID is perfect for that. There are some discussions about the size of the GUID and the storage requirements it could have on larger or mega-large database. However, with the cost of storage space where it is now and expecting it to decrease more, this is a non-issue in my book.

However, as GUIDs are random, using a random GUID as the primary key causes fragmentation. This means that indexing and clustering does not work well, if at all. This is where sequential GUIDs are used. As they are in a sequence, they can be used successfully in indexing and clustering of data.

I re-use the code from NHibernate which generates a sequential GUID using the current Date, Time or DateTime. The code is very similar to the following.

public static Guid GenerateComb()
     byte[] destinationArray = Guid.NewGuid().ToByteArray();
     DateTime time = new DateTime(0x76c, 1, 1);
     DateTime now = DateTime.Now;
     // Get the days and milliseconds which will be
     // used to build the byte string
     TimeSpan span = new TimeSpan(now.Ticks - time.Ticks);
     TimeSpan timeOfDay = now.TimeOfDay;
     // Convert to a byte array
     // Note that SQL Server is accurate to 1/300th of a 
     // millisecond so we divide by 3.333333
     byte[] bytes = BitConverter.GetBytes(span.Days);
     byte[] array = BitConverter.GetBytes(
                    (timeOfDay.TotalMilliseconds / 3.333333));
     // Reverse the bytes to match SQL Servers ordering
     // Copy the bytes into the guid
     Array.Copy(bytes, bytes.Length - 2,
                       destinationArray.Length - 6, 2);
     Array.Copy(array, array.Length - 4,
                       destinationArray.Length - 4, 4);
     return new Guid(destinationArray);

Then I add a method in the webpage.aspx.cs code-behind which calls the method and returns it as a string.

public string GenerateSequentialGuidPanel()
     return GenerateComb().ToString();

Lastly, I added an UpdatePanel to the webpage.aspx which calls the method in the code-behind without refreshing the ASP.NET page. This was cool, because I did not want the entire page to rebuild just to call a single method.

<asp:ScriptManager ID="ScriptManager" runat="server" />
<asp:UpdatePanel ID="UpdatePanelSeqGUID" UpdateMode="Conditional"
        <legend>Generate sequential GUID</legend>
        <br />
        <br /><br />
        <asp:Button ID="buttonSequentialGUID" Text="Go" runat="server" />

That’s it. There are many times when I need a GUID, so I will come back to the page often to capture it. Generate a Radom GUID online or a Generate a Sequential GUID online or use the code in other programs to create primary keys. It’s good.

Download the source