PC Review


Reply
Thread Tools Rate Thread

DataAdapter.Update() adding row with identity field

 
 
PeterB
Guest
Posts: n/a
 
      26th Aug 2003
I have a datatable and use a SqlCeDataAdapter to insert/delete data to a
database. I don't call the update method until I leave my form where I can
modify this particular table. When I add a row I don't get the identity
field value updated into the datatable from the DB. I got this code snippet
from ADO docs using RowUpdate event:

private static void sqlAdapt_RowUpdated(object sender,
SqlCeRowUpdatedEventArgs e)
{
int newID = 0;
SqlCeCommand sqlCmd = new SqlCeCommand("SELECT @@IDENTITY", sqlConn);

if (e.StatementType == StatementType.Insert)
{
newID = (int)sqlCmd.ExecuteScalar();
e.Row["myIdentField"] = newID;
}
}

The event is fired when I call Adapter.Update(), which is called when I
leave the form. This means several rows may have been added and the code
above wouldn't work since the @@IDENTITY retrieves the last inserted
identity value. So my question is now how would I go about to retrieve the
identity field values if it isn't just the last inserted identity value? Is
it impossible? Is it good practice to call Adapter.Update() whenever a row
is inserted/deleted etc. instead of, as I do, when all rows has been added
and deleted by the use?

I know I could use another combination of unique fields to retrieve the
identity value, but I am curious if there is any other better ways. Maybe
you don't have another unique index...

thanks,

Peter



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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
Adding records to a SQL 2000 table with an identity field, in Access 2003 Kyle O via AccessMonster.com Microsoft Access VBA Modules 12 8th Aug 2005 07:03 PM
Adding a record to an Access db using DataAdapter.Update Winshent Microsoft ADO .NET 1 15th Jul 2005 01:45 AM
Adding a record to an Access db using DataAdapter.Update Winshent Microsoft VB .NET 1 15th Jul 2005 01:45 AM
Need to update dataadapter from datagrid that doesn't contain field Larry Woods Microsoft VB .NET 3 14th Nov 2004 10:29 PM
SQLCE DataAdapter Update with Identity field PeterB Microsoft ADO .NET 5 9th Oct 2003 01:26 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:30 AM.