Click here to Skip to main content
15,898,036 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I need to auto generate a GUID for each user who has registered, but I am not able to do so it is giving me an error.
Below is the code which I have created in different class and I will call this method on button click.

Can someone please guide me.
I have never used GUID'S before but have understood it's concept.
C#
public class guid_for_user
{
        string ConnectionStrings;

        public guid_for_user(string ConString)
        {
            ConnectionStrings = ConString;
        }

        public  void  guid_for_users()
        {
            Guid g;
            g = Guid.NewGuid();

            SqlConnection cn = new SqlConnection(ConnectionStrings);

            SqlParameter user_GUID = new SqlParameter("user_guid", SqlDbType.UniqueIdentifier);
            user_GUID.Value = g;

            SqlCommand cmdd = new SqlCommand("INSERT INTO u_security(user_guid) VALUES (g)", cn);
            cmdd.Parameters.Add(user_GUID);

            cn.Open();
            cmdd.ExecuteNonQuery();
            cn.Close();
       }
}

Thanks in advance.
Posted
Updated 1-Apr-12 0:24am
v2
Comments
André Kraak 1-Apr-12 6:24am    
Edited question:
Added pre tags
Formatted text/code
Spelling/Grammar

1 solution

You have to set the name of the parameter (instead of g) in the SqlCommand. Something like:


C#
SqlParameter user_GUID = new SqlParameter("@user_guid", SqlDbType.UniqueIdentifier);
user_GUID.Value = g;
 
SqlCommand cmdd = new SqlCommand("INSERT INTO u_security(user_guid) VALUES (@user_guid)", cn);
cmdd.Parameters.Add(user_GUID);
 
Share this answer
 
Comments
Member 7849477 1-Apr-12 5:39am    
thanks a lot!! it worked....

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