Solved

Changing from using a GUID to Int

Posted on 2013-01-22
12
396 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
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.

707 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

16 Experts available now in Live!

Get 1:1 Help Now