How to Update MDB file?
-
i did a application that creates a new and empty database file(MDB file). i want to create a new row in the new database. i did this:
string connectionString = "provider=Microsoft.JET.OLEDB.4.0; " + "data source =" + Dir;
string commandString = "Select id,cell from Events";
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(commandString, connectionString);
DataSet ds = new DataSet();
dataAdapter.Fill(ds, "Events");DataTable datatable = ds.Tables[0];
DataRow newRow = datatable.NewRow(); //adding a new RownewRow["id"] = "Gil";
newRow["Event"] = "Gil2";datatable.Rows.Add(newRow);
dataAdapter.Update(ds, "Events");
ds.AcceptChanges();it throws me a exception: Update requires a valid InsertCommand when passed DataRow collection with new rows. What is Wrong?..i am not understand p.s All of this is in try catch... thank you!
-
i did a application that creates a new and empty database file(MDB file). i want to create a new row in the new database. i did this:
string connectionString = "provider=Microsoft.JET.OLEDB.4.0; " + "data source =" + Dir;
string commandString = "Select id,cell from Events";
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(commandString, connectionString);
DataSet ds = new DataSet();
dataAdapter.Fill(ds, "Events");DataTable datatable = ds.Tables[0];
DataRow newRow = datatable.NewRow(); //adding a new RownewRow["id"] = "Gil";
newRow["Event"] = "Gil2";datatable.Rows.Add(newRow);
dataAdapter.Update(ds, "Events");
ds.AcceptChanges();it throws me a exception: Update requires a valid InsertCommand when passed DataRow collection with new rows. What is Wrong?..i am not understand p.s All of this is in try catch... thank you!
-
i did a application that creates a new and empty database file(MDB file). i want to create a new row in the new database. i did this:
string connectionString = "provider=Microsoft.JET.OLEDB.4.0; " + "data source =" + Dir;
string commandString = "Select id,cell from Events";
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(commandString, connectionString);
DataSet ds = new DataSet();
dataAdapter.Fill(ds, "Events");DataTable datatable = ds.Tables[0];
DataRow newRow = datatable.NewRow(); //adding a new RownewRow["id"] = "Gil";
newRow["Event"] = "Gil2";datatable.Rows.Add(newRow);
dataAdapter.Update(ds, "Events");
ds.AcceptChanges();it throws me a exception: Update requires a valid InsertCommand when passed DataRow collection with new rows. What is Wrong?..i am not understand p.s All of this is in try catch... thank you!
You will need to define the InsertCommand property of the DataAdapter like so:
// Connection & DataAdapter initialization code should go here
// ...OleDbCommand insertCommand = connection.CreateCommand();
insertCommand.CommandText = "Insert Into Table(Col1, Col2, Col3) Values (?, ?, ?)";
dataAdapter.InsertCommand = insertCommand;// ...
You may also want to check out the
UpdateCommand
andDeleteCommand
properties which will allow your adapter to update and delete records which is what it seems you are also trying to do in your code snippet./F - .NET Developer