Solved

ASP.NET SQL insert error with primary key field

Posted on 2011-03-01
4
475 Views
Last Modified: 2012-06-27
I am using an InsertCommand to insert into my data table, but when the form gets submitted, I get the following error:

"System.Data.SqlClient.SqlException: Implicit conversion from data type sql_variant to uniqueidentifier is not allowed. Use the CONVERT function to run this query." -- The pers_personid field below is type "UniqueIdentifier" and is set as the primary key.  

My InsertCommand:

InsertCommand="INSERT INTO [tblDCPersonTest] ([pers_firstname], [pers_middlename], [pers_lastname], [pers_ssn], [pers_dob], [pers_phonehome], [pers_phonework], [pers_personid]) VALUES (@pers_firstname, @pers_middlename, @pers_lastname, @pers_ssn, @pers_dob, @pers_phonehome, @pers_phonework, @pers_personid)"



How do I fix this?
0
Comment
Question by:saturation
4 Comments
 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 166 total points
ID: 35012477
change it to

InsertCommand="INSERT INTO [tblDCPersonTest] ([pers_firstname], [pers_middlename], [pers_lastname], [pers_ssn], [pers_dob], [pers_phonehome], [pers_phonework], [pers_personid]) VALUES (@pers_firstname, @pers_middlename, @pers_lastname, @pers_ssn, @pers_dob, @pers_phonehome, @pers_phonework, CONVERT(uniqueidentifier,@pers_personid))"
0
 
LVL 32

Accepted Solution

by:
ewangoya earned 167 total points
ID: 35012593

How are you creating your unique identifier, you can just use NEWID() instead of passign the @pers_personid variable
InsertCommand="INSERT INTO [tblDCPersonTest] ([pers_firstname], [pers_middlename], [pers_lastname], [pers_ssn], [pers_dob], [pers_phonehome], [pers_phonework], [pers_personid]) VALUES (@pers_firstname, @pers_middlename, @pers_lastname, @pers_ssn, @pers_dob, @pers_phonehome, @pers_phonework, NEWID())"

Open in new window

0
 

Author Comment

by:saturation
ID: 35012605
Ok, did that.   Now I get

System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'pers_personid', table 'tblDCPersonTest'; column does not allow nulls. INSERT fails.

Did I set my primary key to be the wrong data type?   I just want it to start at 1 and increment automatically when a record is inserted.  
0
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 167 total points
ID: 35012636
Yes uniqueidentifiers are guids, you should be using an integer column with identity insert set to on and then you won't need to pass in your id as it will be created automatically on insert
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to refer to value 8 62
sql how to count case when 4 25
Stored Proc - Rewrite 42 56
What can the "OPTION" in the GROUP help me in my query? 4 21
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

840 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