J
Jorgen D.
Everybody seems to get this to work. What am I doing wrong?
Here is a simple sample with two textboxes (Id and Name)
and a "Save" button.
The table contains 1 row with Id=1 and Name="foo". The
form is showing the proper data but any changes to i.e
Name isn't saved to the database.
Table creation:
SqlCeConnection cn = new SqlCeConnection("Data Source =
Sample.sdf");
cn.Open();
SqlCeCommand cmd = new SqlCeCommand("CREATE TABLE Cust (Id
int PRIMARY KEY, Name nvarchar(50) not null)",cn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
cmd.CommandText = "INSERT INTO Cust (Id, Name) VALUES
(1,'foo')";
cmd.ExecuteNonQuery();
FormLoad :
cn.Open();
da = new SqlCeDataAdapter("SELECT * FROM Cust",cn);
string updCmd = "UPDATE Cust SET Name=? WHERE Id=?";
da.UpdateCommand = new SqlCeCommand(updCmd,cn);
da.UpdateCommand.Parameters.Add
("pName",SqlDbType.NVarChar,50,"Name");
da.UpdateCommand.Parameters.Add
("pId",SqlDbType.Int,1,"Id");
ds = new DataSet();
da.Fill(ds,"Cust");
cn.Close();
tbId.DataBindings.Clear();
tbId.DataBindings.Add("Text",ds.Tables["Cust"],"Id");
tbName.DataBindings.Clear();
tbName.DataBindings.Add("Text",ds.Tables["Cust"],"Name");
Now the tbName is changed and the "Save" button is clicked.
btnSave_click:
da.Update(ds,"Cust");
With the debugger I can see that at no point the
HasChanges on the dataset is true and the RowState on the
currentrow is always Unchaged.
I've tried different senarios, but have not got the
RowState or HasChanges to change.
Input is very much appreciated.
Regards, Jorgen D.
Here is a simple sample with two textboxes (Id and Name)
and a "Save" button.
The table contains 1 row with Id=1 and Name="foo". The
form is showing the proper data but any changes to i.e
Name isn't saved to the database.
Table creation:
SqlCeConnection cn = new SqlCeConnection("Data Source =
Sample.sdf");
cn.Open();
SqlCeCommand cmd = new SqlCeCommand("CREATE TABLE Cust (Id
int PRIMARY KEY, Name nvarchar(50) not null)",cn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
cmd.CommandText = "INSERT INTO Cust (Id, Name) VALUES
(1,'foo')";
cmd.ExecuteNonQuery();
FormLoad :
cn.Open();
da = new SqlCeDataAdapter("SELECT * FROM Cust",cn);
string updCmd = "UPDATE Cust SET Name=? WHERE Id=?";
da.UpdateCommand = new SqlCeCommand(updCmd,cn);
da.UpdateCommand.Parameters.Add
("pName",SqlDbType.NVarChar,50,"Name");
da.UpdateCommand.Parameters.Add
("pId",SqlDbType.Int,1,"Id");
ds = new DataSet();
da.Fill(ds,"Cust");
cn.Close();
tbId.DataBindings.Clear();
tbId.DataBindings.Add("Text",ds.Tables["Cust"],"Id");
tbName.DataBindings.Clear();
tbName.DataBindings.Add("Text",ds.Tables["Cust"],"Name");
Now the tbName is changed and the "Save" button is clicked.
btnSave_click:
da.Update(ds,"Cust");
With the debugger I can see that at no point the
HasChanges on the dataset is true and the RowState on the
currentrow is always Unchaged.
I've tried different senarios, but have not got the
RowState or HasChanges to change.
Input is very much appreciated.
Regards, Jorgen D.