Solved

ASP.NET SQL insert error with primary key field

Posted on 2011-03-01
4
476 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
[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
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:
Ephraim Wangoya 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Copy Database Wizard 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.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

730 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