Binding Problem: TextBox vs. DataSet (maybe simple solution?)

  • Thread starter Thread starter Stuart Powers
  • Start date Start date
S

Stuart Powers

Hello,

This is a /very/ odd problem and any insight would be greatly appreciated.

1) I have used SqlDataAdapter to fill a DataTable in a DataSet.
2) I binded textbox's "Text" property to columns in the DataTable.
3) I edit the textboxes Text value.
in doing so, the DataTable's values have changed
I know this because i have done:
MessageBox.Show(dataTable.Rows[0]["colname"].ToString());
4) I attempt to update the database: myDataAdapter.Update(myDataSet,
"tablename");
this does not work.

now the weird part:

if you replace steps 2 and 3 with:
2) Create a datagrid whose DataSource = myDataSet.Tables["tablename"];
3) make changes in the datagrid

then step will 4 work correctly. (somehow the datagrid must change the
dataset differently than the textboxes do, it appears)
however, this makes no sense, because doing:
MessageBox.Show(dataTable.Rows[0]["colname"].ToString());
prints the same thing with both methods

what is /really/ weird:

Suppose I have both the textboxes and the datagrid, both bound to the
DataTable. When I edit a textbox, then try and update the database, it
sitll doesn't work (as expected, because it didn't work before). However,
If I edit a textbox, then merely click on the datagrid (give it focus) the
value in the datagrid then changes (as if its being refreshed to show the
change I made to the textbox). now, after clicking the datagrid, when I
press try and update the database: myDataAdapter.Update(myDataSet,
"tablename"); it WORKS.

I have have absolutely no idea why merely clicking on the datagrid makes a
difference. They are both bound to the same DataSet/DataTable.

Any help would be greatly appreciated as I am quite stumped.
Thanks,
Stuart
 
Right before you submit the changes via the update, use
try debug.WriteLine(dataSet.HasChanges) and see if it
returns true.

I think the problem stems from when the changed event
fires b/c I know the grid is a little easier to work with
when binding. If you HasChanges returns True, then the
problem is with your update logic.. Since you aren't
getting an exception from what I can see, you'll probably
get False when you check the changes.

Assuming it's false, programatically add a row and then
delete it before you submit the update. If the update
works now, it's the event sequencing. By adding and
deleting the row, you are simply forcing the data to
change. I suspect this will 'fix' the problem but it's
not a real solution either. Let me know what values are
returned, I can be of more help then.

Good Luck,

Bill

(e-mail address removed)
www.knowdotnet.com
 
I have since fixed the problem.

Right after I fill the DataTable, I now call the BeginEdit() function, and
right before I update the DB I call the EndEdit() function.
This appears to fix the problem.

Thank you though,
Stuart

William Ryan said:
Right before you submit the changes via the update, use
try debug.WriteLine(dataSet.HasChanges) and see if it
returns true.

I think the problem stems from when the changed event
fires b/c I know the grid is a little easier to work with
when binding. If you HasChanges returns True, then the
problem is with your update logic.. Since you aren't
getting an exception from what I can see, you'll probably
get False when you check the changes.

Assuming it's false, programatically add a row and then
delete it before you submit the update. If the update
works now, it's the event sequencing. By adding and
deleting the row, you are simply forcing the data to
change. I suspect this will 'fix' the problem but it's
not a real solution either. Let me know what values are
returned, I can be of more help then.

Good Luck,

Bill

(e-mail address removed)
www.knowdotnet.com
-----Original Message-----
Hello,

This is a /very/ odd problem and any insight would be greatly appreciated.

1) I have used SqlDataAdapter to fill a DataTable in a DataSet.
2) I binded textbox's "Text" property to columns in the DataTable.
3) I edit the textboxes Text value.
in doing so, the DataTable's values have changed
I know this because i have done:
MessageBox.Show(dataTable.Rows[0]["colname"].ToString());
4) I attempt to update the database: myDataAdapter.Update (myDataSet,
"tablename");
this does not work.

now the weird part:

if you replace steps 2 and 3 with:
2) Create a datagrid whose DataSource = myDataSet.Tables ["tablename"];
3) make changes in the datagrid

then step will 4 work correctly. (somehow the datagrid must change the
dataset differently than the textboxes do, it appears)
however, this makes no sense, because doing:
MessageBox.Show(dataTable.Rows[0]["colname"].ToString());
prints the same thing with both methods

what is /really/ weird:

Suppose I have both the textboxes and the datagrid, both bound to the
DataTable. When I edit a textbox, then try and update the database, it
sitll doesn't work (as expected, because it didn't work before). However,
If I edit a textbox, then merely click on the datagrid (give it focus) the
value in the datagrid then changes (as if its being refreshed to show the
change I made to the textbox). now, after clicking the datagrid, when I
press try and update the database: myDataAdapter.Update (myDataSet,
"tablename"); it WORKS.

I have have absolutely no idea why merely clicking on the datagrid makes a
difference. They are both bound to the same DataSet/DataTable.

Any help would be greatly appreciated as I am quite stumped.
Thanks,
Stuart



.
 
Stuart,

This may be because you havn't moved off the current record
In the code that calls the update, call the EndCurrentEdit method of the
your forms CurrencyManager to ensure all changes are committed to the
DataTable (before calling DataAdapter.Update)

Stephen
 
Back
Top