Updating Dataset
- From: "Johnny E. Jensen" <johnny@xxxxxxxxx>
- Date: Tue, 2 Oct 2007 20:36:17 +0200
Hello
To update a dataset i created a method (see below)
My question is, how to return the created Primary ID that is created on the
server auto incremential. Is there anyway to place that value into the
correct inserted row?
Kind regards
Johnny E. Jensen
#region Method 'Save'
public void Save(DataSet Datasource, object UserID)
{
bool IsValid = true;
if (Datasource != null)
{
if (Datasource.Tables[0] != null)
{
DataSet dsChanges = Datasource.GetChanges();
if (dsChanges != null)
{
OleDbConnection con = new OleDbConnection(_ConnectionString);
try
{
con.Open();
}
catch (Exception ex)
{
ErrorLogger.WriteToErrorLog(ex.Message, ex.StackTrace,
"Aware.Database.Save()");
IsValid = false;
}
if (IsValid == true)
{
OleDbTransaction dbTran = con.BeginTransaction();
DataTable dtChanges = dsChanges.Tables[0];
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + dtChanges.TableName
+ "]", con);
cmd.Transaction = dbTran;
OleDbDataAdapter dbAdapter = new OleDbDataAdapter(cmd);
dbAdapter.RowUpdated += new
OleDbRowUpdatedEventHandler(dbAdapter_RowUpdated);
OleDbCommandBuilder cb = new OleDbCommandBuilder(dbAdapter);
try
{
dbAdapter.InsertCommand = cb.GetInsertCommand();
}
catch (Exception ex)
{
ErrorLogger.WriteToErrorLog(ex.Message, ex.StackTrace,
"Aware.Database.Save()");
}
try
{
dbAdapter.UpdateCommand = cb.GetUpdateCommand();
}
catch (Exception ex)
{
ErrorLogger.WriteToErrorLog(ex.Message, ex.StackTrace,
"Aware.Database.Save()");
}
try
{
dbAdapter.DeleteCommand = cb.GetDeleteCommand();
}
catch (Exception ex)
{
ErrorLogger.WriteToErrorLog(ex.Message, ex.StackTrace,
"Aware.Database.Save()");
}
if (dbAdapter.InsertCommand != null)
dbAdapter.InsertCommand.Transaction = dbTran;
if (dbAdapter.UpdateCommand != null)
dbAdapter.UpdateCommand.Transaction = dbTran;
if (dbAdapter.DeleteCommand != null)
dbAdapter.DeleteCommand.Transaction = dbTran;
try
{
dbAdapter.Update(dtChanges);
}
catch (Exception ex)
{
ErrorLogger.WriteToErrorLog(ex.Message, ex.StackTrace,
"Aware.Database.Save()");
}
TableLogger(Datasource, UserID);
Datasource.AcceptChanges();
dbTran.Commit();
con.Close();
dsChanges = null;
con = null;
dbTran = null;
dtChanges = null;
cmd = null;
dbAdapter = null;
cb = null;
}
}
}
}
}
.
- Follow-Ups:
- Re: Updating Dataset
- From: Dave Anson
- Re: Updating Dataset
- Prev by Date: struct pointers?
- Next by Date: Re: Dragging data to Excel using DoDragDrop
- Previous by thread: struct pointers?
- Next by thread: Re: Updating Dataset
- Index(es):