Connecting Tech Pros Worldwide Forums | Help | Site Map

DataAdapter update - not working

Shahar
Guest
 
Posts: n/a
#1: Nov 15 '05
Hi
Why this code does not updated the DB, just the DataSet:



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

Eliyahu Goldin
Guest
 
Posts: n/a
#2: Nov 15 '05

re: DataAdapter update - not working


Get rid of ds.AcceptChanges();

AcceptChanges commits changes to the dataset. All added and modified rows
become "Unchanged", and, when you call Update (..), the data adapter doesn't
find any modified rows to update. Update () calls AcceptChanges ()
internally.

Hope this helps,

Eliyahu

"Shahar" <shahar@log-on.com> wrote in message
news:e803974e.0308100221.2f2786d1@posting.google.c om...[color=blue]
> Hi
> Why this code does not updated the DB, just the DataSet:
>
>
>
> 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());
> }[/color]


Closed Thread