Typed DataTable - NewRow() - Identity Field

  • Thread starter Thread starter TheMaxx
  • Start date Start date
T

TheMaxx

ADO 2.0

When i do:
MyRow = MyDataTable.NewMyRow();
MyRow.IdentityField value equals last ID + 1

What if other user is doing the same thing and gets same ID

On DatatTableAdapter.Update both users will try to update with same ID.
I also did not expect IdentityField to have a value (or have it = 0) because
that value should be prvided by SQL Server, not in DataTable in memory ?!
 
TheMax,

Use for the identitiyfield a Guid

MyRow.IdentityField = new Guid();

I hope this helps,

Cor
 
If the field is labeled as an idenity column then the client should
assign a temporary unique value for working localy (for relationships).
When you synch with the database you should have the data adaptor fetch
the created row from the server which will contain the generated ID.

The insert command would read something like (assuming your sql command
type is text, not a stored procedure):

'Insert the new row and simultaniously fetch the newly inserted row
'this will include our id (the other advantage of this is it
'can fetch calculated fields, changes made by triggers, etc.
insert_command.CommandText = _
"INSERT INTO tbl (a, b, c) VALUES (@a, @b, @c)" _
& " SELECT (id, a, b, c) FROM tbl WHERE id = scope_identity"

'This instructs the data adaptor to use the first returned result as the
'rows new values
insert_command.UpdateRowSource = UpdateRowsource.FirstReturnedRecord
 
I've noticed that sometimes client generates ID+1, and sometimes it = 0.

On Adapter.Update it autmatically retreive new generated ID (ADO.Net 2.0,
am not sure that was the case in ADO.Net)
I am not using my custom insert command, but VS generated TableAdapter.
 
Back
Top