DataSet problem

S

Shahar

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();

OleDbDataAdapter adapter = new OleDbDataAdapter("Select * from
Employees", @"Provider=Microsoft.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.AcceptChanges();
adapter.Update(ds, "Employees");

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

ozbear

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();

OleDbDataAdapter adapter = new OleDbDataAdapter("Select * from
Employees", @"Provider=Microsoft.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.AcceptChanges();
adapter.Update(ds, "Employees");

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

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.AcceptChanges();
}

Oz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top