AshleyC-UK

ok im adding a new row into a datatable , the table on the database has an identity field. When the row is added the database updates correctly and i get a new value in the identity field as expected.

What doesnt seem to be happening is the new identity value isnt being reflected in the datatable. When i query the table later on in the application theres only a DBNull value where the new ID should be.

Heres the code im using to update the datatable, how would i make the new identity field appear in it Theres no obvious datatable.refresh method and its a pretty large table anyway, id imagine it would be a bit wastefull to have to reload it all every time i add a new record to it. is there a way to get the new identity value set in the row youve just added to the datatable

NewRow("Type") = intAddressType

NewRow("Description") = Description

NewRow("Address") = Address

NewRow("Salutation") = Salutation

NewRow("AdHoc") = 1

dsAddressTables.Tables("AddressList").Rows.Add(NewRow)

daAddressList.Update(dsAddressTables, "AddressList")

dsAddressTables.AcceptChanges()



Re: .NET Framework Data Access and Storage VB.Net Datatables and Identity Fields problem...

VMazur

When you call Update, identity values are not updated in your DataTable automatically ad you need to add extra code to do this. You need to use SCOPE_IDENTITY() function (for the SQL Server) or @@IDENTITY variable to get generated value first and then assign this value to the column in a DataTable. Here are some samples

http://support.microsoft.com/kb/320141/en-us

http://support.microsoft.com/kb/815629/en-us

http://support.microsoft.com/kb/310350/en-us