retrieve unique id for just added row

azarath

New Member
I am new to asp.net. I am executing a simple insert query via a SQL command. Now I want to retrieve the unique id for this newly inserted row and store it in an array. I do not know how to approach this problem.My code\[code\]foreach (GridViewRow gvrow in gvuserdata.Rows) { string strQuery = "insert into vishalc.[Rental Table] (Car_Id_Reference, UserName, CarName,Price,startdate,enddate)" + " values(@carid, @username, @carname,@price,@startdate,@enddate)"; SqlCommand cmd = new SqlCommand(strQuery); cmd.Parameters.AddWithValue("@carid", gvrow.Cells[0].Text); cmd.Parameters.AddWithValue("@username", gvrow.Cells[1].Text); cmd.Parameters.AddWithValue("@carname", gvrow.Cells[2].Text); cmd.Parameters.AddWithValue("@price", gvrow.Cells[3].Text); cmd.Parameters.AddWithValue("@startdate", gvrow.Cells[4].Text); cmd.Parameters.AddWithValue("@enddate", gvrow.Cells[5].Text); cmd.CommandType = CommandType.Text; cmd.Connection = con; cmd.ExecuteNonQuery(); //I want to store ID for this row after insert command }\[/code\]
 
Top