B
bijoy
We are trying to sort out the issue regarding updating
the identity column value in DataTable after an insert to
DB. Although several examples including the one below
seem to profess that it will work , the below approach
seems to work only in theory
The below sniippet doesn't seem to update the row as
professed in all the technical articles both on msdn and
the web
Although an alternative approach suggests that we
subscribe to RowUpdated event of the DataAdapter
Every material which we posses seem to suggest this isnt
necessary for SQL 2000 (which we are using ) and is only
required for MS ACCESS
I have included the code which to test the same ( Pubs ,
Jobs Table) , U may test the below code by binding what
it returns to a datagrid
Any alternative suggestions , workarounds would be highly
regard
Kind regards
Bijoy
public static DataSet test()
{
DataSet ds = new DataSet();
SqlDataAdapter da = new
SqlDataAdapter(
"select * from jobs",
"server=localhost;uid=sa;database=pubs");
//populate the dataset;
da.MissingSchemaAction =
MissingSchemaAction.AddWithKey;
da.Fill(ds);
//change the identity seed and
increment so that we are not mislead that code actually
is getting value //from db;
ds.Tables[0].Columns
["job_id"].AutoIncrementSeed =39;
ds.Tables[0].Columns
["job_id"].AutoIncrementStep =-1;
//create the row
ds.Tables[0].Rows[1][1] = "newer
description";
ds.Tables[0].Rows.Add(
new object[4] {
null, "new row", 40, 40 });
DataSet ds2 = new DataSet();
ds2 = ds.GetChanges();
SqlCommandBuilder bld = new
SqlCommandBuilder(da);
da.InsertCommand =
bld.GetInsertCommand();
da.UpdateCommand =
bld.GetUpdateCommand();
da.DeleteCommand =
bld.GetDeleteCommand();
// make sure we get the identity
column on insert
da.InsertCommand.CommandText +=
";select * from jobs
where job_id = @@identity";
da.InsertCommand.UpdatedRowSource
=
UpdateRowSource.FirstReturnedRecord;
//save to the database the
changes to the dataset
da.Update(ds2);
// dataset should show now
contains latest changes (The id generated by the DB
Engine) which it doesnt
ds.Merge(ds2);
return ds;
}
the identity column value in DataTable after an insert to
DB. Although several examples including the one below
seem to profess that it will work , the below approach
seems to work only in theory
The below sniippet doesn't seem to update the row as
professed in all the technical articles both on msdn and
the web
Although an alternative approach suggests that we
subscribe to RowUpdated event of the DataAdapter
Every material which we posses seem to suggest this isnt
necessary for SQL 2000 (which we are using ) and is only
required for MS ACCESS
I have included the code which to test the same ( Pubs ,
Jobs Table) , U may test the below code by binding what
it returns to a datagrid
Any alternative suggestions , workarounds would be highly
regard
Kind regards
Bijoy
public static DataSet test()
{
DataSet ds = new DataSet();
SqlDataAdapter da = new
SqlDataAdapter(
"select * from jobs",
"server=localhost;uid=sa;database=pubs");
//populate the dataset;
da.MissingSchemaAction =
MissingSchemaAction.AddWithKey;
da.Fill(ds);
//change the identity seed and
increment so that we are not mislead that code actually
is getting value //from db;
ds.Tables[0].Columns
["job_id"].AutoIncrementSeed =39;
ds.Tables[0].Columns
["job_id"].AutoIncrementStep =-1;
//create the row
ds.Tables[0].Rows[1][1] = "newer
description";
ds.Tables[0].Rows.Add(
new object[4] {
null, "new row", 40, 40 });
DataSet ds2 = new DataSet();
ds2 = ds.GetChanges();
SqlCommandBuilder bld = new
SqlCommandBuilder(da);
da.InsertCommand =
bld.GetInsertCommand();
da.UpdateCommand =
bld.GetUpdateCommand();
da.DeleteCommand =
bld.GetDeleteCommand();
// make sure we get the identity
column on insert
da.InsertCommand.CommandText +=
";select * from jobs
where job_id = @@identity";
da.InsertCommand.UpdatedRowSource
=
UpdateRowSource.FirstReturnedRecord;
//save to the database the
changes to the dataset
da.Update(ds2);
// dataset should show now
contains latest changes (The id generated by the DB
Engine) which it doesnt
ds.Merge(ds2);
return ds;
}