Home All Groups Group Topic Archive Search About

How can I get the value of an autoincremented column when adding new rows to a DataTable?

Author
15 Nov 2006 3:11 PM
0to60
In the following situation:

System.Data.DataRow row = dataTable.NewRow();

row["someField"] = someValue;
// Set some more fields

dataTable.Rows.Add(row);

dataAdapter.Update(dataTable);



If the primary key of my table is an autoincrementing number, can I get to
it from here?

Author
15 Nov 2006 4:42 PM
William (Bill) Vaughn
See my whitepaper "Handling an Identity Crisis" on my website.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
-----------------------------------------------------------------------------------------------------------------------

Show quote
"0to60" <holeshot60_nospam_@yahoo.com> wrote in message
news:uMxaPhMCHHA.5068@TK2MSFTNGP02.phx.gbl...
> In the following situation:
>
> System.Data.DataRow row = dataTable.NewRow();
>
> row["someField"] = someValue;
> // Set some more fields
>
> dataTable.Rows.Add(row);
>
> dataAdapter.Update(dataTable);
>
>
>
> If the primary key of my table is an autoincrementing number, can I get to
> it from here?
>
Author
17 Nov 2006 2:48 PM
0to60
I can't find it anywhere on  your site.  Can you send me a link?


Show quote
"William (Bill) Vaughn" <billvaRemoveT***@nwlink.com> wrote in message
news:%23I$qNUNCHHA.3380@TK2MSFTNGP04.phx.gbl...
> See my whitepaper "Handling an Identity Crisis" on my website.
>
> --
> ____________________________________
> William (Bill) Vaughn
> Author, Mentor, Consultant
> Microsoft MVP
> INETA Speaker
> www.betav.com/blog/billva
> www.betav.com
> Please reply only to the newsgroup so that others can benefit.
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
> __________________________________
> Visit www.hitchhikerguides.net to get more information on my latest book:
> Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
> -----------------------------------------------------------------------------------------------------------------------
>
> "0to60" <holeshot60_nospam_@yahoo.com> wrote in message
> news:uMxaPhMCHHA.5068@TK2MSFTNGP02.phx.gbl...
>> In the following situation:
>>
>> System.Data.DataRow row = dataTable.NewRow();
>>
>> row["someField"] = someValue;
>> // Set some more fields
>>
>> dataTable.Rows.Add(row);
>>
>> dataAdapter.Update(dataTable);
>>
>>
>>
>> If the primary key of my table is an autoincrementing number, can I get
>> to it from here?
>>
>
>
Author
17 Nov 2006 4:58 PM
William (Bill) Vaughn
http://www.betav.com/Files/Content/whitepapers.htm "Handling an Identity
Crisis"

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
-----------------------------------------------------------------------------------------------------------------------

Show quote
"0to60" <holeshot60_nospam_@yahoo.com> wrote in message
news:eFvTydlCHHA.992@TK2MSFTNGP03.phx.gbl...
>I can't find it anywhere on  your site.  Can you send me a link?
>
>
> "William (Bill) Vaughn" <billvaRemoveT***@nwlink.com> wrote in message
> news:%23I$qNUNCHHA.3380@TK2MSFTNGP04.phx.gbl...
>> See my whitepaper "Handling an Identity Crisis" on my website.
>>
>> --
>> ____________________________________
>> William (Bill) Vaughn
>> Author, Mentor, Consultant
>> Microsoft MVP
>> INETA Speaker
>> www.betav.com/blog/billva
>> www.betav.com
>> Please reply only to the newsgroup so that others can benefit.
>> This posting is provided "AS IS" with no warranties, and confers no
>> rights.
>> __________________________________
>> Visit www.hitchhikerguides.net to get more information on my latest book:
>> Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
>> -----------------------------------------------------------------------------------------------------------------------
>>
>> "0to60" <holeshot60_nospam_@yahoo.com> wrote in message
>> news:uMxaPhMCHHA.5068@TK2MSFTNGP02.phx.gbl...
>>> In the following situation:
>>>
>>> System.Data.DataRow row = dataTable.NewRow();
>>>
>>> row["someField"] = someValue;
>>> // Set some more fields
>>>
>>> dataTable.Rows.Add(row);
>>>
>>> dataAdapter.Update(dataTable);
>>>
>>>
>>>
>>> If the primary key of my table is an autoincrementing number, can I get
>>> to it from here?
>>>
>>
>>
>
>
Author
19 Nov 2006 4:30 PM
Otis Mukinfus
On Fri, 17 Nov 2006 08:58:04 -0800, "William \(Bill\) Vaughn"
<billvaRemoveT***@nwlink.com> wrote:

I believe you meant to "Dealing With An Identity Crisis:"


Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Author
15 Nov 2006 6:53 PM
Cor Ligthert [MVP]
Oto,

Every regular in this newsgroup will advice you not to use the autoincrement
but Guids, however if you persist on using that awfull method with datasets,
than have a look at the seed properties from the datacolumn.

http://msdn2.microsoft.com/en-us/library/system.data.datacolumn.autoincrementseed.aspx

I hope this helps,

Cor

Show quote
"0to60" <holeshot60_nospam_@yahoo.com> schreef in bericht
news:uMxaPhMCHHA.5068@TK2MSFTNGP02.phx.gbl...
> In the following situation:
>
> System.Data.DataRow row = dataTable.NewRow();
>
> row["someField"] = someValue;
> // Set some more fields
>
> dataTable.Rows.Add(row);
>
> dataAdapter.Update(dataTable);
>
>
>
> If the primary key of my table is an autoincrementing number, can I get to
> it from here?
>
Author
15 Nov 2006 8:29 PM
Jim Rand
Assuming you are using SQL Server, getting the autoincrement key back is
really trivial.

Dataset
--------

set the primary key field's AutoIncrementStep and AutoIncrementSeed both
to -1

The adapter's insert statement
------------------------------
Add code to the end of the insert statement to get back the autoincrement
key

INSERT INTO [dbo].[AccessFlagLookup] ([AccessFlag], [Description],
[LastUpdatedBy]) VALUES (@AccessFlag, @Description, @LastUpdatedBy);SELECT
AccessFlagLookupID FROM dbo.AccessFlagLookup WHERE (AccessFlagLookupID =
SCOPE_IDENTITY())

Trap RowUpdated event for inserts
------------------------------------

/* Skip current row for statement type insert. Necessary to prevent adding a
new row for
* each insert instead of changing the autoincrement primary key */
foreach (System.Data.SqlClient.SqlDataAdapter da in adapterList)
{
  da.RowUpdated += new
System.Data.SqlClient.SqlRowUpdatedEventHandler(da_RowUpdated);
}

static void da_RowUpdated(object sender,
System.Data.SqlClient.SqlRowUpdatedEventArgs e)
{
if (e.StatementType == System.Data.StatementType.Insert) e.Status =
System.Data.UpdateStatus.SkipCurrentRow;
}

This methodology works great with SQL Server.  For other database, such as
Sybase's SQL Anywhere, it's a little more complicated but very doable.

AddThis Social Bookmark Button