Click here to Skip to main content
15,916,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using Visual studio 2015 Enterprise edition for Empty Template of ASP.Net Project. In this project i am getting the session User id value and stored it in int and the value of the integer is given to string to convert the id value to the Guid... I have created conversion class to convert it. It is converting but not in the correct way. Can anyone explain me in this issue

What I have tried:

Conversion Class has taken from the internet itself
public static Guid IntToGuid(int value)
{
    byte[] bytes = new byte[16];
    BitConverter.GetBytes(value).CopyTo(bytes, 0);
    return new Guid(bytes);
}


int id = Convert.ToInt32(Session["UserID"].ToString());
// Convert integer 182 as a hex in a string variable
string hexValue = id.ToString("x32");
Guid myID = Conversion.IntToGuid(Convert.ToInt32(hexValue));


Here my id value for example 1 then the output is displayed as for
myID = 00000001-0000-0000-0000-000000000000

Can anyone help me
Posted
Updated 6-Mar-18 19:57pm

1 solution

The problem is that you can't store a GUID in an integer: a standard int is an Int32 - a 32 bit number, so it can store values from −2,147,483,648 to 2,147,483,647. A GUID is a unsigned 128 bit number, so it can store from 0 to 340,282,366,920,938,463,463,374,607,431,768,211,455 - which obviously won't fit! As a result, you waste the whole advantage of using a GUID: that it is extremely unlikely to be be duplicated. Unlike database IDENTiTY fields, a GUID is not a "regular sequence" of numbers such that if you use two in quick succession you get values which differ by one - with GUIDs you get values that are wildly different!

If you want to use integer ID's then use integers throughout - don't try to assume that it's a "valid guid".
If you want to use GUID ids, then use GUIDs throughout - dont; try to store them in any other format.


Quote:
My DB Table fields are UserID, Username, Password and Email
In this fields
UserID is an integer datatype
Username is an varchar datatype
password is an varchar datatype
email is an varchar datatype
Three other things to do:
1) Check other tables first before you change anything - if they contain foreign keys to the old ID, then you need to add a column to that tables as well and copy the "new GUID ID" into them or you will lose the connection.
2) Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^] It's considered a Code Crime[^]
3) Use NVARCHAR instead of VARCHAR - it uses Unicode instead of ASCII, which matches to .NET code (which is all Unicode) ASCII is a much smaller set of characters, and some usernames may not work at all if you store them an VARCHAR fields. You should also consider emails as Unicode as it has been allowed since 2012.
 
Share this answer
 
v2
Comments
Member 8583441 7-Mar-18 2:05am    
Okay fine but i am having the id number in the database retrieving it from the database. The id value stored is in integer format so i thought that to change the value from integer to Guid format so that it can be accessed to the next pages also.

Then definitely i will change the field datatype of id but can you give some explanation about this to change it to the Guid from interger in SQL Server 2012
OriginalGriff 7-Mar-18 2:12am    
You are missing the point: you can't store a GUID value in an integer, there isn't anything like enough space. And GUID's aren't "just random numbers" - they have a structure and a specific set of generation algorithms which produce numbers which just do not fit in 32 bits!

If you are storing it in the DB as an integer, it isn't a GUID, it's more likely an IDENTITY field.

What does your DB table look like?
Member 8583441 7-Mar-18 2:16am    
My DB Table fields are UserID, Username, Password and Email
In this fields
UserID is an integer datatype
Username is an varchar datatype
password is an varchar datatype
email is an varchar datatype
OriginalGriff 7-Mar-18 2:31am    
Answer updated.
Member 8583441 7-Mar-18 2:09am    
How to change the previous value that is having integer datatype to unique identifier

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900