Learn how to generate the unique string using GUID method in asp.net using c#

in this asp.net tutorial you will learn how to generate the unique string using GUID feature in asp.net. GUID stands for Global unique identifier. GUID is a 128 bit integer that can be used to uniquely identify any thing in database. It can help you like a primary key in your database table. There is no chance of duplication of GUID in your database table. So if you want to use GUID as a primary key then go ahead and use it as a primary key without any hesitation.
I have seen many programmers who use the combination of current date time, random number and some string to make the unique string and then store it in database table to uniquely identify the records. Using GUID you will not go with such hassle.

How to create a GUID in asp.net using c#

You can find the GUID method in the system namespace. The GUID method System.Guid.NewGuid() initializes a new instance of GUID class.

There are also some overloads available for those programmers who want to format the GUID in a particular fashion.

Let's have a look over code below to learn the use of GUID method.

Generating the unique string using GUID method in asp.net using c#

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

Use these overloads method to format the GUID string.


There is also a data type uniqueidentifier in sql server to store the GUID value. If you don't want to use this GUID value through c# then you can use NEWID() fucntion of sql server.

GUID is Everywhere

In windows environment GUID is used everywhere. Open the registry by executing the regedit command in run on your windows system, you can easily see that GUIDs are used extensively to uniquely identify every application, they serve like application IDS under the HKEY_CLASSES_ROOT section (AppID key).

Typical format of GUID


So this is the way to generate the unique string using GUID method in asp.net using c#.