If the commandbuilder doesn't work, then you'll have to specify your own INSERT statement in your adapter:
adapter.InsertCommand = new SqlCommand("INSERT INTO myTable VALUES(@Id)", conn); adapter.InsertCommand.Parameters.Add("@Id", ...);
Sending Insert statements directly to the database is not a very good idea if your using a dataset. You must edit your dataset, then using your adapter, send the changes to your database. But if your approach really demands that you send direct updates to the database and update your dataset then you can always refill them. But if your records is huge then it would not be a nice way to update your dataset. Instead you could manually reflect the changes to your dataset this way:
//Delete a Row dataset1.Tables[0].Rows.RemoveAt(<your index>); //Add a Row DataRow dr = dataset1.Tables[0].NewRow(); dr["TestField"] = "Sample"; dataset1.Tables[0].Rows.Add(dr); //Modify a Row dataset1.Tables[0].Rows[<rowindex>]["TestField"] = "some value";
The approach that you are doing is quite the reverse. You must first edit your dataset then send the updates to your access database. To do this you must have a activeconnection, adapter and a dataset. eg:
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM myTable", con); OleDbCommandBuilder cm = new OleDbCommandBuilder(adapter); ... // Fill the DataSet and edit adapter.Fill(ds); ds.Tables[0].Rows[10]["field1"] = "TEsting"; // send updates to database adapter.Update(ds);
Just see the MSDN docs for additional infos regarding the objects used.
Update Dataset In C#
gana
Hi,
If the commandbuilder doesn't work, then you'll have to specify your own INSERT statement in your adapter:
adapter.InsertCommand = new SqlCommand("INSERT INTO myTable VALUES(@Id)", conn);
adapter.InsertCommand.Parameters.Add("@Id", ...);
Sending Insert statements directly to the database is not a very good idea if your using a dataset. You must edit your dataset, then using your adapter, send the changes to your database. But if your approach really demands that you send direct updates to the database and update your dataset then you can always refill them. But if your records is huge then it would not be a nice way to update your dataset. Instead you could manually reflect the changes to your dataset this way:
//Delete a Row
dataset1.Tables[0].Rows.RemoveAt(<your index>);
//Add a Row
DataRow dr = dataset1.Tables[0].NewRow();
dr["TestField"] = "Sample";
dataset1.Tables[0].Rows.Add(dr);
//Modify a Row
dataset1.Tables[0].Rows[<rowindex>]["TestField"] = "some value";
cheers,
Paul June A. Domag
MaurizioBu
Hi,
The approach that you are doing is quite the reverse. You must first edit your dataset then send the updates to your access database. To do this you must have a activeconnection, adapter and a dataset. eg:
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM myTable", con);
OleDbCommandBuilder cm = new OleDbCommandBuilder(adapter);
...
// Fill the DataSet and edit
adapter.Fill(ds);
ds.Tables[0].Rows[10]["field1"] = "TEsting";
// send updates to database
adapter.Update(ds);
Just see the MSDN docs for additional infos regarding the objects used.
cheers,
Paul June A. Domag
Shyam Pather