Update Dataset In C#

I have a Insert statement to insert new records into the Access database, but how do i Update the dataset



Answer this question

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

    I try to use the commandbuilder but it did not work so I had to use the insert statement. How do u update the dataset Do u refill it .

  • Update Dataset In C#