Solved

Changing from using a GUID to Int

Posted on 2013-01-22
12
400 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

756 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