Insert Record & retrieve ID
Hi,
I have a frustrating problem. My code inserts a new record into an Access DB via ADO.Net. I need access to the autoincremented ID number of the new record.
When the new record is inserted, I open the connection, execute the procedure and close the connection again. If I check the DB in Access the new record is there, with the correct ID number.
I then run the following code:
dim lngID as Long
lngID=da.ExecuteScalar("SELECT MAX(custID) FROM Customers")
where da is a DataAccess DLL with the following code for the method used:
Function ExecuteScalar(byval SQL as string) as object
Dim c as new oledbConnection(connString)
Dim cmd as oledbCommand(SQL,c)
Dim newValue as Object
c.Open
newValue=cmd.ExecuteScalar
'tidy up
c.close
return newValue
End Function
This all works fine, BUT the number returned is 1 smaller than the ID assigned to the newly inputted record! i.e. the last value added.
Anyone know what's going on??
Thanks
Dave
|