G
Girish
I was struggling all day with autonumbers not being returned by my
sqldataadapter when I did an insert. I have found the solution (from the
newsgroups). Im just confused a little since I tried two ways to do this and
one didnt work. I just wanted to know why.
Heres the code that didnt work:
---------------------------------
//_dataCommand is an instance of SqlDataAdapter
//_dataTable is an instance of DataTable
SqlCommandBuilder bldr = new SqlCommandBuilder(_dataCommand);
_dataCommand.InsertCommand = bldr.GetInsertCommand();
_dataCommand.InsertCommand.CommandText += ";Select SCOPE_IDENTITY() as
id";
_dataCommand.InsertCommand.UpdatedRowSource =
UpdateRowSource.FirstReturnedRecord;
//_dataCommand.Update(_dataTable.Select("", "", DataViewRowState.Added));
_dataCommand.Update(_dataTable);
_dataTable.AcceptChanges();
Heres the code that DID work:
-------------------------------
//_dataCommand is an instance of SqlDataAdapter
//connection is an instance of ConnectionProvider which has a property
called DBConnection of type SqlConnection
//_dataTable is an instance of DataTable
SqlCommandBuilder bldr = new SqlCommandBuilder(_dataCommand);
SqlCommand cmdInsert = new
SqlCommand(bldr.GetInsertCommand().CommandText, connection.DBConnection);
cmdInsert.CommandText += ";Select SCOPE_IDENTITY() as id";
SqlParameter[] aParams = new
SqlParameter[bldr.GetInsertCommand().Parameters.Count];
bldr.GetInsertCommand().Parameters.CopyTo(aParams, 0);
bldr.GetInsertCommand().Parameters.Clear();
for(int i=0 ; i < aParams.Length; i++)
{
cmdInsert.Parameters.Add(aParams);
}
_dataCommand.InsertCommand = cmdInsert;
_dataCommand.InsertCommand.UpdatedRowSource =
UpdateRowSource.FirstReturnedRecord;
_dataCommand.Update(_dataTable);
_dataTable.AcceptChanges();
---------------------------------------------
1) Can someone explain why my first code didnt work? When I looked at traces
in sql profiler, the "SELECT SCOPE_IDENTITY()" wasnt being executed at all.
2) I have some other issues. Ive read and read and read but cant seem to
figure out what AcceptChanges() is meant for and how
dataCommand.Update(_dataTable.Select("", "", DataViewRowState.Added)) is
different from dataCommand.Update(_dataTable);
Appreciate any help on this.
Thanks,
Girish
sqldataadapter when I did an insert. I have found the solution (from the
newsgroups). Im just confused a little since I tried two ways to do this and
one didnt work. I just wanted to know why.
Heres the code that didnt work:
---------------------------------
//_dataCommand is an instance of SqlDataAdapter
//_dataTable is an instance of DataTable
SqlCommandBuilder bldr = new SqlCommandBuilder(_dataCommand);
_dataCommand.InsertCommand = bldr.GetInsertCommand();
_dataCommand.InsertCommand.CommandText += ";Select SCOPE_IDENTITY() as
id";
_dataCommand.InsertCommand.UpdatedRowSource =
UpdateRowSource.FirstReturnedRecord;
//_dataCommand.Update(_dataTable.Select("", "", DataViewRowState.Added));
_dataCommand.Update(_dataTable);
_dataTable.AcceptChanges();
Heres the code that DID work:
-------------------------------
//_dataCommand is an instance of SqlDataAdapter
//connection is an instance of ConnectionProvider which has a property
called DBConnection of type SqlConnection
//_dataTable is an instance of DataTable
SqlCommandBuilder bldr = new SqlCommandBuilder(_dataCommand);
SqlCommand cmdInsert = new
SqlCommand(bldr.GetInsertCommand().CommandText, connection.DBConnection);
cmdInsert.CommandText += ";Select SCOPE_IDENTITY() as id";
SqlParameter[] aParams = new
SqlParameter[bldr.GetInsertCommand().Parameters.Count];
bldr.GetInsertCommand().Parameters.CopyTo(aParams, 0);
bldr.GetInsertCommand().Parameters.Clear();
for(int i=0 ; i < aParams.Length; i++)
{
cmdInsert.Parameters.Add(aParams);
}
_dataCommand.InsertCommand = cmdInsert;
_dataCommand.InsertCommand.UpdatedRowSource =
UpdateRowSource.FirstReturnedRecord;
_dataCommand.Update(_dataTable);
_dataTable.AcceptChanges();
---------------------------------------------
1) Can someone explain why my first code didnt work? When I looked at traces
in sql profiler, the "SELECT SCOPE_IDENTITY()" wasnt being executed at all.
2) I have some other issues. Ive read and read and read but cant seem to
figure out what AcceptChanges() is meant for and how
dataCommand.Update(_dataTable.Select("", "", DataViewRowState.Added)) is
different from dataCommand.Update(_dataTable);
Appreciate any help on this.
Thanks,
Girish