Hello.
I'm trying to get the primary key for a newly inserted row using
ADO.NET and SqlServer. The convention seems to be to do this with the
RowUpdated event, i.e.
_sqlConnectionToTickerTextDb.Open();
SqlCommand cmdGetPK = new SqlCommand("SELECT @@IDENTITY",
_sqlConnectionToTickerTextDb);
object pkObject = cmdGetPK.ExecuteScalar();
int pkNunber = Int32.Parse((pkObject).ToString());
e.Row[_tickerIDColumnName] = pkNunber;
e.Row.AcceptChanges();
_sqlConnectionToTickerTextDb.Close();
However, I am getting the value System.DBNull instead of the primary
key.
I''ve tried replacing @@IDENTITY with SCOPE_IDENTITY() but the same
happens.
Is there a setting that I need to alter in the database or am I
approaching this in the wrong way?
I've also noticed that most people dont use .Open and .Close() in
coding the above, yet my app complains if I don't.
Is there an easier more practical way of getting the new primary key
e.g. by looking at the attributes of the SqlRowUpdatedEventArgs
parameter?
Greg.