Question

I got two datatables from a mdb file using oledb and modified it.

accessConnection.Open();   

string selectQuery = "SELECT * FROM Students";
DataAdapter = new OleDbDataAdapter(selectQuery, accessConnection);
DataAdapter.Fill(StudentsDataTable);

DataAdapter.SelectCommand.CommandText = "SELECT * FROM Teachers";
DataAdapter.Fill(TeachersDataTable);

// modified the two datatables
// ...

However, I have a problem in updating the two tables back into the mdb file. I can't update StudentsDataTable because the DataAdapter select command was already changed to select TeachersDataTable. When I handle only one DataTable, I don't have this kind of problem.

DataAdapter.UpdateCommand = new OleDbCommandBuilder(DataAdapter).GetUpdateCommand();
DataAdapter.Update(StudentsDataTable); <-- exception error occur that columns don't match.
DataAdapter.Update(TeachersDataTable);

Do you have any ideas for updating two datatables using one DataAdapter? or Should I have separate DataAdapters for the two tables?

No correct solution

OTHER TIPS

Essentially you have to create two instances.

DataAdapter.UpdateCommand = new OleDbCommandBuilder(DataAdapter).GetUpdateCommand();
DataAdapter.Update(TeachersDataTable);

DataAdapter.SelectCommand.CommandText = "SELECT * FROM Students";
DataAdapter.UpdateCommand = new OleDbCommandBuilder(DataAdapter).GetUpdateCommand();
DataAdapter.Update(StudentsDataTable); 

The main issue is your DataAdapter is set to Teachers table as it was the last command set on it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top