c# - retrieve unique id for just added row -


i new asp.net. executing simple insert query via sql command. want retrieve unique id newly inserted row , store in array. not know how approach problem.

my code

foreach (gridviewrow gvrow in gvuserdata.rows)         {              string strquery = "insert 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 store id row after insert command            } 

use select scope_identity() execute scalar:

int returnid = 0; string strquery = "insert vishalc.[rental table] (car_id_reference, username,      carname,price,startdate,enddate)" + " values(@carid, @username,     @carname,@price,@startdate,@enddate); select scope_identity()"; ... returnid = (int)cmd.executescalar(); 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -