Solved

Changing from using a GUID to Int

Posted on 2013-01-22
12
397 Views
Last Modified: 2013-01-28
I'm testing a table and .net form and am trying to change from using a UNIQUEIDENTIFIER to Int.

On the table, I removed the UNIQUEIDENTIFIER, and created a new column with the same name but with data type Int.

On my code behind I've changed the insert to reference the Int data type, except I don't know how to change this reference so that it creates a new Int rather than GUID?
"Guid RegistrationID = Guid.NewGuid();"  

    protected void Button1_Click(object sender, EventArgs e)
        {
            // generate new RegistrationID
            Guid RegistrationID = Guid.NewGuid();

            // create insert query

            using (SqlCommand command = new SqlCommand())
            {
                command.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);

command.CommandText = @"insert into [Registration](RegistrationID, FirstName, LastName)
values(@RegistrationID, @FirstName, @LastName)";

                command.Parameters.Add("@RegistrationID", SqlDbType.Int).Value = RegistrationID2;               
                command.Parameters.Add("@FirstName", SqlDbType.VarChar, 128).Value = litFirstName.Text;
                command.Parameters.Add("@LastName", SqlDbType.VarChar, 128).Value = litLastName.Text;
}

Open in new window



I get this error when trying the above code:

Object must implement IConvertible.
Thanks for the help.
0
Comment
Question by:javierpdx
  • 3
  • 3
  • 3
  • +2
12 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 38806149
This is a GUID
3a1b7275-f326-4302-a5b7-1875104b2b13

Note the non numerical characters in it.  What you are attempting won't work.
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 38806151
If you going to use an int column the usual thing to do would be to make this an identity column in the database and let the database generate the id, then there is no need to generate the id in code and pass it in as part of the insert query.
0
 
LVL 19

Expert Comment

by:Amandeep Singh Bhullar
ID: 38806323
i agree with the @jacko72 comments, but still you want to do from the code then you can use your GUID variable as

RegistrationID.ToString().GetHashCode()

it will return the integer value.

Hope this will help you
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 38806444
If the database is expecting an integer value you could use the random class to generate those instead of GUID:

http://social.msdn.microsoft.com/Forums/eu/Vsexpressvcs/thread/55fb3116-c978-4ac8-9381-a2605e16e256
0
 

Author Comment

by:javierpdx
ID: 38806477
Thanks.
I do have the RegistrationID set as Identity and the Primary Key.
It does not need to be random and no long need the UNIQUIEIDENTIFIER.
What I'm struggling with is that this line of code"Guid RegistrationID = Guid.NewGuid();"  was setting a variable for me to use latter on in my code behind by setting a variable called RegistrationID.  Now that I am moving away from the GUID, how do I set the same variable but based on a  new data type for Int?

Thanks.
0
 
LVL 4

Expert Comment

by:Srinivasulu Muppala
ID: 38806531
please post the conversion part of the code and let me know is it windows application or web application?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Assisted Solution

by:Srinivasulu Muppala
Srinivasulu Muppala earned 250 total points
ID: 38806546
command.Parameters.Add("@RegistrationID", SqlDbType.Int).Value = RegistrationID2;

change the above line of code such as:

command.Parameters.Add("@RegistrationID", SqlDbType.Varchar).Value = RegistrationID2;
0
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 250 total points
ID: 38806617
See revised code below to return the identity value from the sqlcommand text:

 protected void Button1_Click(object sender, EventArgs e)
        {

            // create insert query

            using (SqlCommand command = new SqlCommand())
            {
                command.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);

command.CommandText = @"insert into [Registration](FirstName, LastName)
values(@FirstName, @LastName) SELECT SCOPE_IDENTITY()";

                command.Parameters.Add("@FirstName", SqlDbType.VarChar, 128).Value = litFirstName.Text;
                command.Parameters.Add("@LastName", SqlDbType.VarChar, 128).Value = litLastName.Text;
} 

Dim identity As Integer = -1
identity = Integer.Parse(cmd.ExecuteScalar().ToString()) 'Get the Identity Column value

Open in new window

0
 
LVL 4

Assisted Solution

by:Srinivasulu Muppala
Srinivasulu Muppala earned 250 total points
ID: 38806677
change your query try

"insert into [Registration](FirstName, LastName)
values(@FirstName, @LastName);SELECT @@IDENTITY()";
0
 

Author Comment

by:javierpdx
ID: 38807817
This is a c#, .net, web application with a SQL 2012 backend.

Sorry.  I'm new to programming and C#, so I am struggling.

I have a few questions.  Would I remove line 4 from my original post?  " Guid RegistrationID = Guid.NewGuid();"

Based on jacko72's recommendation I would remove it, but if I do remove it, how then I would need to declare the local variable to be call RegistrationID2 latter in my code.
I've tried several different options and have not been successful.

With this command parameter:
command.Parameters.Add("@RegistrationID", SqlDbType.Int).Value = RegistrationID2;
I would still need to declare the local variable RegistrationID2 before I can use it.
0
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 250 total points
ID: 38807867
You would remove it, If you look at the code in my post I assign the returned identity to the variable identity so you should be able to use this variable later in the code, obviously you can change the name of this variable to whatever you want.
0
 

Author Closing Comment

by:javierpdx
ID: 38828450
Thank you for the help.  I appreciate it.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now