I'm using @@Identity
to get identity of a particular row after successful insert on a table, but sometimes I'm getting someother identity other than what it created for a particular row.
Sample:
ID UserName Age Location
1 Andy 22 USA
2 Robert 24 Canada
3 James 26 Mexico
From above sample while getting identity for user 'Robert' some other created 'James' and I'm getting the lastest identity as '3' instead of '2'.
All this is happening in a ASP.NET website user registration, it's giving the latest identity for each connection object and the same connection object is using for all registrations.
Please some one help me this out how to get the exact identiy when some other people using same connection object to get the identity?
If i use SCOPE_IDENTITY()
for the below query, I'm getting DBNULL
exception while convertion.
cmd.Connection = conn;
conn.Open();
int intStatus = cmd.ExecuteNonQuery();
if (intStatus == 1)
{
SqlCommand cmd1 = new SqlCommand("Select SCOPE_IDENTITY()", conn);
id_package = Convert.ToInt32(cmd1.ExecuteScalar());
}