What I'm trying to do:
Update a .mdb Access database using a DataTable and DataAdapter. Please see my code below.
using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;
Jet OLEDB:Database Password=iaggyah;User ID=Admin;
Data Source=C:\aaa\aaa\aaa\aaa\aaa\aaa\aaa\resources\Survey1.mdb;"))
{
con.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [SurveyTest1]", con);
surveyTbl.Load(cmd.ExecuteReader());
surveyTbl.Rows[0]["SurveyName"] = "test2";
try
{
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = new OleDbCommand("SELECT * FROM [SurveyTest1]", con);
OleDbCommandBuilder cmdBuilder = new OleDbCommandBuilder(adapter);
cmdBuilder.QuotePrefix = "[";
cmdBuilder.QuoteSuffix = "]";
adapter.DeleteCommand = cmdBuilder.GetDeleteCommand();
adapter.InsertCommand = cmdBuilder.GetInsertCommand();
adapter.UpdateCommand = cmdBuilder.GetUpdateCommand();
adapter.Update(surveyTbl);
}
catch (Exception e)
{
string error = e.Message;
}
}
Problem
When it hits 'adapter.Update' line, it throws an OleDbException with the message "The parameter is incorrect". Please see the error details below:
Error code: -2147024809 Source: System.Data.Common Stack trace:
at System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)
at Tests.Test.UpdateTest_ReturnsTrue() in C:\aaa\aaa\aaa\aaa\aaa\aaa\aaa\Test.cs:line 251
I've googled the problem extensively and I haven't found anything that has been relevant. I've tried writing an update command and updating it that way, and that seems to work. Therefore, the other alternative I thought of was just to update each row in a for loop, but I need to be able to update any table in the database at runtime, so that might make dynamic updates difficult with different data types. I think I'd be forced to just use varchar (unless I'm mistaken??).
Additional details:
Windows 10
.NET framework 4.7.2
Visual Studio 2019
A class library project
Thank you in advance.