Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > how do I get the ID of a row I just added using OleDbDataAdapter.Update() ?

Reply
Thread Tools

how do I get the ID of a row I just added using OleDbDataAdapter.Update() ?

 
 
Bennett Haselton
Guest
Posts: n/a
 
      10-17-2004
Suppose I add a new row to a table in a dataset, and then I use an
OleDbDataAdapter to add that new row to a SQL Server database using
OleDbDataAdapter.Update(), as in the following code:

dsLocalDataSet.user_postRow newRow =
dsLocalDataSet1.user_post.Newuser_postRow();
newRow.post_text = this.lblHiddenMessageStorage.Text;
newRow.post_datetime = System.DateTime.Now;
dsLocalDataSet1.user_post.Adduser_postRow(newRow);
this.oleDbUserPostAdapter.Update(dsLocalDataSet1.u ser_post);

How do I get the ID in the underlying database, of the new row that I
just added?

It might not be the same as the ID of the row that was just added to
the DataSet table. Even if new IDs are generated sequentially in both
the underlying database table and in the DataSet table, and say the
records already in the table had IDs 1, 2, 3, 4, and 5, when I add the
new row to the DataSet table it will get ID 6. However, in the
meantime, some other process might have added a new row to the table
in the underlying database, so that when the row actually gets added
through adapter's Update() method, in the database it will get ID 7.
So I can't just look at the ID of the row in the DataSet.

In MySQL you can do SELECT LAST_INSERT_ID() to get the last
automatically generated value that was inserted into an AUTO_INCREMENT
column by your current connection. Is there an equivalent for SQL
Server? (Or even better, a way to get the value using the built-in
functions of the typed DataSet, so you don't have to get it
inelegantly passing a raw command to SQL Server, which is error-prone
in case you ever want to switch to a different data source type, etc.)

-Bennett
 
Reply With Quote
 
 
 
 
Bennett Haselton
Guest
Posts: n/a
 
      10-18-2004
(Bennett Haselton) wrote in message news:<. com>...
> Suppose I add a new row to a table in a dataset, and then I use an
> OleDbDataAdapter to add that new row to a SQL Server database using
> OleDbDataAdapter.Update(), as in the following code:
>
> dsLocalDataSet.user_postRow newRow =
> dsLocalDataSet1.user_post.Newuser_postRow();
> newRow.post_text = this.lblHiddenMessageStorage.Text;
> newRow.post_datetime = System.DateTime.Now;
> dsLocalDataSet1.user_post.Adduser_postRow(newRow);
> this.oleDbUserPostAdapter.Update(dsLocalDataSet1.u ser_post);
>
> How do I get the ID in the underlying database, of the new row that I
> just added?
>
> It might not be the same as the ID of the row that was just added to
> the DataSet table. Even if new IDs are generated sequentially in both
> the underlying database table and in the DataSet table, and say the
> records already in the table had IDs 1, 2, 3, 4, and 5, when I add the
> new row to the DataSet table it will get ID 6. However, in the
> meantime, some other process might have added a new row to the table
> in the underlying database, so that when the row actually gets added
> through adapter's Update() method, in the database it will get ID 7.
> So I can't just look at the ID of the row in the DataSet.
>
> In MySQL you can do SELECT LAST_INSERT_ID() to get the last
> automatically generated value that was inserted into an AUTO_INCREMENT
> column by your current connection. Is there an equivalent for SQL
> Server? (Or even better, a way to get the value using the built-in
> functions of the typed DataSet, so you don't have to get it
> inelegantly passing a raw command to SQL Server, which is error-prone
> in case you ever want to switch to a different data source type, etc.)
>
> -Bennett


Well I found how to do this -- in SQL Server, you call SELECT
@@IDENTITY to get the last automatically inserted ID, however it's
only valid if the connection has not been closed since the update
occurred for which you're trying to get the last inserted ID. So if
you just call a data adapter's Update() method, and the connection was
in the closed state before you called it, the connection will be
closed again when Update returns, and you'll lose the information
about the last inserted ID. So you have to put the adapter's
connection object in the Open state first, then call Update() and then
do a SELECT @@IDENTITY query.

Here's a utility function that does it:

public static int GetLastInsertID(OleDbConnection conn)
{
/*
* The connection object must be open at the time this method
* is called, and the connection cannot have been closed since
* the insertion occurred for which you are trying to get the
* last inserted ID. Otherwise, an exception will be thrown.
*/
OleDbDataAdapter objAdapter = new OleDbDataAdapter(
"SELECT @@IDENTITY AS 'Identity';", conn
);
DataSet ds = new DataSet();
objAdapter.Fill(ds, "tablename");
string strID = ds.Tables["tablename"].Rows[0]["Identity"].ToString();
if (strID == "")
{
throw new Exception("GetLastInsertID called but @@IDENTITY returned
nothing");
}
int nID = System.Int32.Parse(strID);
return nID;
}

and the code that calls the function:

>>>

dsLocalDataSet.wbuserRow newRow =
this.dsLocalDataSet1.wbuser.NewwbuserRow();
newRow.username = "abc";
newRow.password_hash = "def";
newRow.email_address = "ghi";
this.dsLocalDataSet1.wbuser.AddwbuserRow(newRow);

// must open connection first or GetLastInsertID won't work
this.oleDbLocalConnection.Open();

this.oleDbWbuserAdapter.Update(dsLocalDataSet1.wbu ser);
Response.Write("last insert: " +
DatabaseUtils.GetLastInsertID(this.oleDbLocalConne ction).ToString() +
"<br>\n");
this.oleDbLocalConnection.Close();
>>>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HOWTO: FormView Insert Operation - Get Row ID of Inserted Row Dan Sikorsky ASP .Net 2 04-30-2009 09:50 PM
Solution for posterity: GridView, Datakeys, and "Item has already been added. Key in dictionary: 'CategoryID' Key being added: 'CategoryID'" ASP .Net 2 11-02-2006 04:48 AM
Sorting a row of letters, using a blank space, with some added rules TheOriginalThemePark@hotmail.com Java 6 05-05-2006 02:21 PM
ok I can do a totals row but how about a percentage row after each data row D ASP .Net Datagrid Control 0 05-23-2005 04:10 PM
how come user control added to page doesn't get added to codebehind file? Bennett Haselton ASP .Net 1 11-08-2004 09:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57