Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Changing from using a GUID to Int

Posted on 2013-01-22
12
Medium Priority
?
408 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 45

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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 45

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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

610 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