Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all i have the following piece of code and cant seem to use SCOPE_IDENTITY correctly. I need to find the ID column so i can use it to insert data into my sql table.

C#
SqlCommand myCommand = new SqlCommand("INSERT INTO cas_user_ext(x_id, fullname, email, name) VALUES(@x_id, @fullname, @email, @name) SET @Identity = SCOPE_IDENTITY();",
sqlcon.Connection);
//int x_id = System.Convert.ToInt32(myCommand.ExecuteScalar());
decimal decimalx_id = (decimal)myCommand.ExecuteScalar();
int x_id = (int)decimalx_id;
myCommand.Parameters.Add("@Name", Environment.UserName);
myCommand.Parameters.Add("@identity", x_id);
myCommand.Parameters.Add("@fullname", textBox1.Text);
myCommand.Parameters.Add("@email", textBox2.Text);
//primarypin = myReader["primarypin"].ToString();
//secondarypin = myReader["secondarypin"].ToString();
myCommand.ExecuteNonQuery();


If anyone could please assist me. I have gone through countless help articles and read up on so many pages but i cant seem to get it working.
Posted
Comments
ZurdoDev 30-Sep-14 11:44am    
See Solution 1. ExecuteNonQuery means you're not expecting to get anything back.
PIEBALDconsult 30-Sep-14 11:46am    
I recommend you not use Identities because they cause too much trouble like this. It's better to know the ID of a record _before_ you INSERT it. Try using Sequences instead.
ZurdoDev 1-Oct-14 9:10am    
Really? I always use IDs and never have any issue.
Ivan Lubbe 2-Oct-14 1:20am    
Honestly i'm learning as i go along. I'm working for a MPS company and am working on some projects on the side and in my free time to make things easier for myself and the company. I appreciate everyone's assistance. I feel like a nuisance having posted so many questions on here.

1 solution

You don't create the identity before the insert. SQL Server will handle that for your. Also don't insert the identity column.

Original Solution:
C#
using (SqlConnection sqlcon = new SqlConnection("Server")) {
	using (SqlCommand myCommand = new SqlCommand("INSERT INTO cas_user_ex(fullname, email, name) VALUES(@fullname, @email, @name); SELCET SCOPE_IDENTITY();", sqlcon)) {
		myCommand.Parameters.Add("@Name", Environment.UserName);
		myCommand.Parameters.Add("@fullname", textBox1.Text);
		myCommand.Parameters.Add("@email", textBox2.Text);

		int iNewRowIdentity = Convert.ToInt32(myCommand.ExecuteScalar());
		}
	}


Revised Solution:
C#
using (SqlCommand myCommand = new SqlCommand("INSERT INTO cas_user_ex(fullname, email, name) VALUES(@fullname, @email, @name); SELCET SCOPE_IDENTITY();", sqlcon.Connection)) {
	myCommand.Parameters.Add("@Name", Environment.UserName);
	myCommand.Parameters.Add("@fullname", textBox1.Text);
	myCommand.Parameters.Add("@email", textBox2.Text);

	int iNewRowIdentity = Convert.ToInt32(myCommand.ExecuteScalar());
	}
 
Share this answer
 
v4
Comments
Ivan Lubbe 1-Oct-14 2:41am    
I'm getting an error with the above code "'SQLConnect:' type used in a using statement must be implicitly convertible to 'System.IDisposable'
Ivan Lubbe 1-Oct-14 3:07am    
my sql connection is initiated by a class if that makes any difference?
SteveyJDay 1-Oct-14 9:10am    
You can remove the SqlConnection using statement and use your class. Check out the revised solution.
Ivan Lubbe 2-Oct-14 1:17am    
Thank you Jared, i will be testing it now :)
Ivan Lubbe 2-Oct-14 2:03am    
Hi Jared,

I am getting the error below

System.Data.SqlClient.SqlException (0x80131904): Cannot insert the value NULL into column 'x_id', table 'eqcas.dbo.cas_user_ext'; column does not allow nulls. INSERT fails.
The statement has been terminated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900