Click here to Skip to main content
15,851,711 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried to update a DataTable. The Insert function works, not update function does not. The related code (simplified) is below. Did I miss something? Thanks in advance.
C#
private void InitializeCommand(string sCase, OleDbDataAdapter adapter, string tblName)   {
  OleDbConnection connection = (OleDbConnection)adapter.SelectCommand.Connection;

  // Insert
  adapter.InsertCommand = connection.CreateCommand();
  adapter.InsertCommand.CommandText = "...";     // Simplified here
  AddParameters(adapter.InsertCommand,"xxx",...);   // Simplified here

  // Update
  adapter.UpdateCommand = new OleDbCommand();
  AddParameters(adapter.UpdateCommand,"xxx",...);   // Simplified here
}

protected void ButtonSubmit_Click(object sender, EventArgs e)
{
    adapterRequest = new OleDbDataAdapter("Select * from " + sRequest, dHelp.connectionString2);
    InitializeCommand(TextBoxStatus.Text, adapterRequest, sRequest);
    dsRequest = new DataSet("RequestSet");
    adapterRequest.Fill(dsRequest, sRequest);
    tbl_Request = dsRequest.Tables[0];

    DataRow newRow;    // for Insert case
    ...

    // Update case
    tbl_Request.Rows[10].BeginEdit();  // Simplified here
    tbl_Request.Rows[10][3] = "A Test String";
    ...
    tbl_Request.Rows[10].EndEdit();
    tbl_Request.Rows[10].AcceptChanges();

    adapterRequest.Update(dsRequest, "tbl_Request");
    dsRequest.AcceptChanges();
}
Posted
Updated 5-Mar-14 5:10am
v2
Comments
ZurdoDev 5-Mar-14 10:12am    
What does happen? Errors?
[no name] 5-Mar-14 10:23am    
No error. Checked the DB, the record is not updated.
ZurdoDev 5-Mar-14 10:26am    
I think you'll need to show all of the code for your update then, not the simplified version.
[no name] 5-Mar-14 10:55am    
The whole code body is big. Posting the whole code could cause some confusion. The major code is here. I went through step-by-step. After dsRequest.AcceptChanges(); I checked the the Dataset's table.rows, and the row's record. Everything is fine there. But when I open the Access table, nothing changes.
ZurdoDev 5-Mar-14 10:59am    
I don't ever use DataTables but AcceptChanges only saves the changes to the datatable, not the database. You'll need to use your Adapter or something else to actually write back to the database.

1 solution

Problem solved. The related code is below:
C#
OleDbDataAdapter adapterRequest = new OleDbDataAdapter("Select * from " + sRequest, dHelp.connectionString2);
OleDbConnection connection = (OleDbConnection)adapterRequest.SelectCommand.Connection;
connection.Open();
OleDbCommandBuilder cb = new OleDbCommandBuilder(adapterRequest);
DataTable dtTarget = new DataTable();
adapterRequest.Fill(dtTarget);
...
// Update the record
dtTarget.Rows[intRowNo][dtTarget.Columns["AssignedBy"].ColumnName] = TextBoxAssigner.Text;
...
adapterRequest.UpdateCommand = cb.GetUpdateCommand();
adapterRequest.Update(dtTarget);
connection.Close();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900