MarkD Posted May 30, 2005 Posted May 30, 2005 Happy Memorial Day! :) I have an ADO.Net datatable coding question; this may be elementary, but I'm stumped... Is there an easy way (or what's the best way) to retrieve the key of a record you just added to a table? I have the following code... can someone help me fill in the ??? ' add a new row to the table Dim drRow As DataRow drRow = selectedDataTbl.NewRow() selectedDataTbl.Rows.Add(drRow) ' save its ID iCurrentRowID = selectedDataTbl.??? Thanks, Mark Quote
Afraits Posted May 31, 2005 Posted May 31, 2005 If you mean that the row has a primary key field, autonumbering ID field or similar then the current version of drRow will provide access to the populated field. Quote Afraits "The avalanche has started, it is too late for the pebbles to vote"
MarkD Posted May 31, 2005 Author Posted May 31, 2005 Thanks Afraits, Yes, it is an autonumber field. Do you have an example line of code I would use to do this? (I tried: iCurrentRow = drRow.Item("IncidentID") after the .Add(drRow) above thinking by that time the key would have been assigned in the datatable but it comes back NULL) Quote
stustarz Posted May 31, 2005 Posted May 31, 2005 The ID column in your datatable needs to have its autoincrement property set to true, the autoincrementseed and autoincrementstep needs to be set aswell, this way it will autogenerate a value when a new row is entered. Quote Visit: VBSourceSeek - The VB.NET sourcecode library "A mere friend will agree with you, but a real friend will argue."
MarkD Posted June 2, 2005 Author Posted June 2, 2005 Cool! that did it. Thanks to both of you for your help :) Mark Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.