DataSet problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shahar

    #1

    DataSet problem

    Hi what's worng with the following code, the chages I made is only
    change the DataSet but not the Data-Base:

    try
    {
    DataSet ds = new DataSet();

    OleDbDataAdapte r adapter = new OleDbDataAdapte r("Select * from
    Employees", @"Provider=Micr osoft.Jet.OLEDB .4.0;Data Source=My.mdb") ;

    adapter.Fill(ds , "Employees" );

    ds.Tables["Employees"].Rows[0].BeginEdit();
    ds.Tables["Employees"].Rows[0]["name"] = "X";
    ds.Tables["Employees"].Rows[0].EndEdit();

    ds.AcceptChange s();
    adapter.Update( ds, "Employees" );

    foreach(DataRow row in ds.Tables["Employees"].Rows)
    {
    Console.WriteLi ne(row["id"] + " " + row["name"]);
    }
    }
    catch(Exception exp)
    {
    Console.WriteLi ne(exp.Message. ToString());
    }
  • ozbear

    #2
    Re: DataSet problem

    On 10 Aug 2003 00:01:14 -0700, shahar@log-on.com (Shahar) wrote:
    [color=blue]
    >Hi what's worng with the following code, the chages I made is only
    >change the DataSet but not the Data-Base:
    >
    >try
    >{
    > DataSet ds = new DataSet();
    >
    > OleDbDataAdapte r adapter = new OleDbDataAdapte r("Select * from
    >Employees", @"Provider=Micr osoft.Jet.OLEDB .4.0;Data Source=My.mdb") ;
    >
    > adapter.Fill(ds , "Employees" );
    >
    > ds.Tables["Employees"].Rows[0].BeginEdit();
    > ds.Tables["Employees"].Rows[0]["name"] = "X";
    > ds.Tables["Employees"].Rows[0].EndEdit();
    >
    > ds.AcceptChange s();
    > adapter.Update( ds, "Employees" );
    >
    > foreach(DataRow row in ds.Tables["Employees"].Rows)
    > {
    > Console.WriteLi ne(row["id"] + " " + row["name"]);
    > }
    >}
    >catch(Exceptio n exp)
    >{
    > Console.WriteLi ne(exp.Message. ToString());
    >}[/color]

    You are "updating" with the wrong dataset.
    Prior to calling the AcceptChanges method, you need to call the
    GetChanges method to return a dataset containing only the changed
    records (inserted, deleted, or modified). That is the dataset that
    Update should be called with, i.e.,

    // change some fields...
    // now get the changed records and apply them
    DataSet changedData = ds.GetChanges() ;
    if (changedData != null)
    {
    adapter.Update( changedData);
    ds.AcceptChange s();
    }

    Oz

    Comment

    Working...