Can I use addrow in a dataset to retrieve autoincrementing keyfield

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In ADO.NET Core Reference they give an example of the savings in code of adding a row using a dataset rather than a datatable:

ds.Customers.AddCustomersRow("ALFKI", "Alfreds Futterkiste")

If you are adding a row that has an autoincrementing integer as its keyfield, can the integer be retrieved, as it is with an addhandler using tables and rows?

polynomial5d
 
Hi,

polynomial5d said:
In ADO.NET Core Reference they give an example of the savings in code of
adding a row using a dataset rather than a datatable:
ds.Customers.AddCustomersRow("ALFKI", "Alfreds Futterkiste")

If you are adding a row that has an autoincrementing integer as its
keyfield, can the integer be retrieved, as it is with an addhandler using
tables and rows?

Sure, type something like int key = ds.Customers[rowNumber].KeyFieldName
where KeyFieldName is the name of the field.
 
If you want to retrieve the the last-inserted identity value, you can use
@@IDENTITY , for example:

SELECT @@IDENTITY

Regards,

Felix Wu
=============
This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
Thread-Topic: Can I use addrow in a dataset to retrieve autoincrementing keyfield
thread-index: AcPosUt87ur+kgwXQZm0hs1HyANn8Q==
X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
From: =?Utf-8?B?cG9seW5vbWlhbDVk?= <[email protected]>
Subject: Can I use addrow in a dataset to retrieve autoincrementing keyfield
Date: Sun, 1 Feb 2004 02:51:06 -0800
Lines: 7
Message-ID: <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain;
charset="Utf-8"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
Newsgroups: microsoft.public.dotnet.framework.adonet
Path: cpmsftngxa07.phx.gbl
Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.adonet:73236
NNTP-Posting-Host: tk2msftcmty1.phx.gbl 10.40.1.180
X-Tomcat-NG: microsoft.public.dotnet.framework.adonet

In ADO.NET Core Reference they give an example of the savings in code of
adding a row using a dataset rather than a datatable:

ds.Customers.AddCustomersRow("ALFKI", "Alfreds Futterkiste")

If you are adding a row that has an autoincrementing integer as its
keyfield, can the integer be retrieved, as it is with an addhandler using
tables and rows?

polynomial5d
 
Thanks both the Miha and Felix.

I have questions about applying your suggestions.

Miha, I'm writing in vb. Does int key mean dim key as integer key = ds......

Where would I place that statement? After the update of the dataadapter?

Also what would I use as the row number? Count -1? Doesn't this assume nobody else has updated the database as well?

Felix,

I use the @@IDENTITY with the table and row objects. How would I use it here? Where would I place the select statement? Right after updating the dataadapter?

Thanks again.

Polynomial5d
 
Hi,

polynomial5d said:
Thanks both the Miha and Felix.

I have questions about applying your suggestions.

Miha, I'm writing in vb. Does int key mean dim key as integer key = ds......

Yep.

Where would I place that statement? After the update of the dataadapter?

Yes. Only after update are "real" autoincrement values retrieved from
database.
Also what would I use as the row number? Count -1? Doesn't this assume
nobody else has updated the database as well?

Update just updates current datatable. It doesn't retrieve any new rows.
Felix,

I use the @@IDENTITY with the table and row objects. How would I use it
here? Where would I place the select statement? Right after updating the
dataadapter?

If you are using Sql server then you should use SCOPE_IDENTITY while
@@identity is good for Access 2000 and newer (jet 4 engine).
You might read:
Retrieving Identity or Autonumber Values .net help topic,
Managing an @@IDENTITY Crisis (ADO.NET Technical Articles)
http://tinyurl.com/iny0.

You might also tell us what are you after - why do you need identity field
for.
 
Hi Polynomial5d,

You can hook the RowUpdated event of the DataAdapter. Run the "SELECT
@@IDENTITY"query by using another Command object in the RowUpdated event
handler. You can take a look at this Knowledge Base article. It may give
you some hints:

816112 HOW TO: Retrieve the Identity Value While Inserting Records into
Access
http://support.microsoft.com/?id=816112

Best Regards,

Felix Wu
=============
This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
Thread-Topic: Can I use addrow in a dataset to retrieve autoincrementing keyfield
thread-index: AcPpdvjouErhu5K2QuaK7LAOtkwoEQ==
X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
From: =?Utf-8?B?cG9seW5vbWlhbDVk?= <[email protected]>
References: <[email protected]>
Subject: RE: Can I use addrow in a dataset to retrieve autoincrementing keyfield
Date: Mon, 2 Feb 2004 02:26:08 -0800
Lines: 17
Message-ID: <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain;
charset="Utf-8"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
Newsgroups: microsoft.public.dotnet.framework.adonet
Path: cpmsftngxa07.phx.gbl
Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.adonet:73301
NNTP-Posting-Host: tk2msftcmty1.phx.gbl 10.40.1.180
X-Tomcat-NG: microsoft.public.dotnet.framework.adonet

Thanks both the Miha and Felix.

I have questions about applying your suggestions.

Miha, I'm writing in vb. Does int key mean dim key as integer key =
ds......

Where would I place that statement? After the update of the dataadapter?

Also what would I use as the row number? Count -1? Doesn't this assume
nobody else has updated the database as well?

Felix,

I use the @@IDENTITY with the table and row objects. How would I use it
here? Where would I place the select statement? Right after updating the
dataadapter?

Thanks again.

Polynomial5d
 
Miha

Thank you very much

Your answer to your suggestion seems perfectly clear. I'll give it a try. I think I understand your answer to my questions regarding Felix's suggestion, so I'll give that a try as well

Why do I need it

In a form frmTopicFromStart I have a number of tabs = general, author(s), concepts, keywords

My keywords are more comprehensive than most. The user can select text in the textbox that contains the topic text, say a newspaper article. Suppose it's a name. The KeywordSet will be Arafat, Yassir. There may be only one keyword here, also Arafat, Yassir. However there can be many search phrases, Yassir Arafat, Yasir Arafat, Yasser Arafat, President Arafat, Chairman Arafat, etc. There are three tables. There's a fourth. KeywordSetTopicAssignments. So I need the ID of the KeywordSet to update the other tables. I've left some out, but I'm sure you get the idea

polynomial5
 
Back
Top