Get the ID of the newly added row

  • Thread starter Thread starter Imran Aziz
  • Start date Start date
I

Imran Aziz

Hello All,
I am fairly new to ADO.net, I am using this simple command to add a new
row to the database, what I want now is to get the auto increment ID of the
row added, can anyone please help me with this?
strQuery = "insert into tblFeedChannels ....";

SqlCommand objCmd = new SqlCommand(strQuery, conn);

objCmd.ExecuteNonQuery();



Imran.
 
At the end of your sql add this (writing out of my head): "; SELECT
@NewId=SCOPE_IDENTITY()", add a parameter with out direction
SqlParameter newId = objCmd.Parameters.Add("@NewId",
SqlDbType.Int).Direction = ParameterDirection.Output;

And after ExecuteNonQuery() the value will appear in the parameter:
newId.Value
 
Thank you so very much for helping me out with this. The article link from
Ryan is awesome, and the help code from Miha is a quick solution, thanks a
lot.
Imran
 
Back
Top