?
Solved

Changing from using a GUID to Int

Posted on 2013-01-22
12
Medium Priority
?
405 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Basic Security of Your VPC

So, you’ve got this shiny new VPC and a fancy new application configured on your EC2 servers ready to go. This application is only accessible from your computer, which is great for security, but you need your users to be able to access it! So, what’s the easiest way to do this?

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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

762 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