I am using the following code to add a single record to a database. (And the code is working fine):
The table has a primary key "ID" that is set as an identity and is auto-incremented. I want to obtain the newly assigned ID field to use as a foreign key in another table. Both the myrow.item("ID") is NULL and the last row in the myDS dataset is NULL.Code:myDA = New SqlDataAdapter("SELECT * FROM Rescue", myCon) myCB = New SqlCommandBuilder(myDA) myDS = New DataSet myDA.Fill(myDS, "Rescue") Dim myRow As DataRow = myDS.Tables("Rescue").NewRow myRow.Item("CallNum") = Me.TxtCallNum.Text ....other fields added here..... myDS.Tables("Rescue").Rows.Add(myRow) myDA.Update(myDS, "Rescue")
Only if I execute the sqldataadapter SELECT again to repopulate the table will it show, but by this time it's too late. I no longer know which is the newly added record. I don't think I should just assume it's the highest 'ID' number.
How can I obtain the auto-generated ID field when adding a new row?
Greg




Reply With Quote