DataBinding - I'm going nuts !!!!!

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.
 
A

Arnaldo Fuziy

I'm gonna suggest a few things to you by looking in your code...

We've succesfully implemented complex binding using something like that:
you) tbId.DataBindings.Add("Text",ds.Tables["Cust"],"Id")
us) tbId.DataBindings.Add(New
Windows.Forms.Binding("Text",ds.Tables["Cust"],"Id"))

Have you tried to use the CommandBuilder:
Dim obj as New SqlCeCommandBuilder(da)
da.UpdateCommand = obj.GetUpdateCommand (and so for the insert 'n delete
command)

The rest seems to be fine...

Hope it helps.
 
G

Guest

I've tried the overload of the add method which takes a
binding object but that doesn't help. I also tried using
the commandbuilder but that dosen't work either, it
dosen't create the commands, even with all the
requirements fullfilled (primarykey and so on). I'm
starting to think that there is somthing fundamental
wrong. I'm using VS.NET 2003 from the box, no updates
installed (couldn't find any) I've updated the CF with SP1.

I've heard somthing about a binding-manager/-context. Is
it that requiered for it to work ?

Regards,
Jorgen D.
-----Original Message-----
I'm gonna suggest a few things to you by looking in your code...

We've succesfully implemented complex binding using something like that:
you) tbId.DataBindings.Add("Text",ds.Tables["Cust"],"Id")
us) tbId.DataBindings.Add(New
Windows.Forms.Binding("Text",ds.Tables["Cust"],"Id"))

Have you tried to use the CommandBuilder:
Dim obj as New SqlCeCommandBuilder(da)
da.UpdateCommand = obj.GetUpdateCommand (and so for the insert 'n delete
command)

The rest seems to be fine...

Hope it helps.


Jorgen D. said:
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.


.
 
R

Ray Fink

Umm, do the textboxes have TextChanged events that modify the data set
value?

I've tried the overload of the add method which takes a
binding object but that doesn't help. I also tried using
the commandbuilder but that dosen't work either, it
dosen't create the commands, even with all the
requirements fullfilled (primarykey and so on). I'm
starting to think that there is somthing fundamental
wrong. I'm using VS.NET 2003 from the box, no updates
installed (couldn't find any) I've updated the CF with SP1.

I've heard somthing about a binding-manager/-context. Is
it that requiered for it to work ?

Regards,
Jorgen D.
-----Original Message-----
I'm gonna suggest a few things to you by looking in your code...

We've succesfully implemented complex binding using something like that:
you) tbId.DataBindings.Add("Text",ds.Tables["Cust"],"Id")
us) tbId.DataBindings.Add(New
Windows.Forms.Binding("Text",ds.Tables["Cust"],"Id"))

Have you tried to use the CommandBuilder:
Dim obj as New SqlCeCommandBuilder(da)
da.UpdateCommand = obj.GetUpdateCommand (and so for the insert 'n delete
command)

The rest seems to be fine...

Hope it helps.


Jorgen D. said:
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.


.
 

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