Insert now row, and get the ID number in return  
Author Message
Dan Mikkelsen





PostPosted: .NET Framework Data Access and Storage, Insert now row, and get the ID number in return Top

If i insert a new row into a table that has a column ID as the primary key with autonumber, how can i get that ID value returned after i inserted the new row.

I'm coding in vb2005 and uses abd access database



.NET Development31  
 
 
ahmedilyas





PostPosted: .NET Framework Data Access and Storage, Insert now row, and get the ID number in return Top

The other is to include perhaps an out parameter which will be used by your T-SQL statement to set the current record ID back to this parameter...

lets give this a shot

'ok so you have just inserted your record. Still keep the connection open and lets execute a different command

Dim theReader as OleDbDataReader = theOleDbCommand.ExecuteReader(CommandBehavior.CloseConnection)

Dim lastId as Integer = -1

if theReader.HasRows = true then

theReader.Read()

lastId = Convert.ToInt32(theReader(0))

end if

'close connection

does this help



 
 
Dan Mikkelsen





PostPosted: .NET Framework Data Access and Storage, Insert now row, and get the ID number in return Top

yep... That did the trick!!

Thanks