Home All Groups Group Topic Archive Search About

Typed DataTable - NewRow() - Identity Field

Author
28 Jun 2006 8:30 AM
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 ?!

Author
28 Jun 2006 8:50 AM
Cor Ligthert [MVP]
TheMax,

Use for the identitiyfield a Guid

MyRow.IdentityField = new Guid();

I hope this helps,

Cor

Show quote
"TheMaxx" <themaxxREM***@net.hr> schreef in bericht
news:e7tef5$cr2$1@magcargo.vodatel.hr...
> 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 ?!
>
>
>
Author
28 Jun 2006 9:34 AM
TheMaxx
Why do i need to worry about ID field on client, isn't SQL Server
responsible for that?


Show quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
news:e$3El%23omGHA.2452@TK2MSFTNGP04.phx.gbl...
> TheMax,
>
> Use for the identitiyfield a Guid
>
> MyRow.IdentityField = new Guid();
>
> I hope this helps,
>
> Cor
>
> "TheMaxx" <themaxxREM***@net.hr> schreef in bericht
> news:e7tef5$cr2$1@magcargo.vodatel.hr...
>> 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 ?!
>>
>>
>>
>
>
Author
28 Jun 2006 11:29 PM
Chris Chilvers
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

Show quote
On Wed, 28 Jun 2006 11:34:07 +0200, "TheMaxx" <themaxxREM***@net.hr>
wrote:

>Why do i need to worry about ID field on client, isn't SQL Server
>responsible for that?
>
>
>"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
>news:e$3El%23omGHA.2452@TK2MSFTNGP04.phx.gbl...
>> TheMax,
>>
>> Use for the identitiyfield a Guid
>>
>> MyRow.IdentityField = new Guid();
>>
>> I hope this helps,
>>
>> Cor
>>
>> "TheMaxx" <themaxxREM***@net.hr> schreef in bericht
>> news:e7tef5$cr2$1@magcargo.vodatel.hr...
>>> 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 ?!
>>>
>>>
>>>
>>
>>
>
Author
29 Jun 2006 12:59 PM
TheMaxx
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.



Show quote
"Chris Chilvers" <kee***@dynafus.com> wrote in message
news:5h36a2te50r6gprnarqqb8f6b150iibqa4@4ax.com...
> 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
>
> On Wed, 28 Jun 2006 11:34:07 +0200, "TheMaxx" <themaxxREM***@net.hr>
> wrote:
>
>>Why do i need to worry about ID field on client, isn't SQL Server
>>responsible for that?
>>
>>
>>"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
>>news:e$3El%23omGHA.2452@TK2MSFTNGP04.phx.gbl...
>>> TheMax,
>>>
>>> Use for the identitiyfield a Guid
>>>
>>> MyRow.IdentityField = new Guid();
>>>
>>> I hope this helps,
>>>
>>> Cor
>>>
>>> "TheMaxx" <themaxxREM***@net.hr> schreef in bericht
>>> news:e7tef5$cr2$1@magcargo.vodatel.hr...
>>>> 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 ?!
>>>>
>>>>
>>>>
>>>
>>>
>>

AddThis Social Bookmark Button