Databound text box does not update database

  • Thread starter Thread starter Patrick Demets
  • Start date Start date
P

Patrick Demets

I have a fairly simple form with databound text boxes, but changes (or new
records) are not recognized. The text boxes are successfully populated with
data (from the database) and I can cycle through all the records. Using a
datagrid works fine for updating.

I've based this on the first sample project from the book "C# Professional
Projects" (Premier Press). I've read many posts concerning this (and the MS
KB as well), and most seem to say that databound text boxes must use a
DataRow object (in addition to connection, data adapter, and dataset
objects), and therefore not as simple as a datagrid.

Based on what I've read, I'm inclined to believe that the book is wrong.
Here are the code fragments for loading and saving the data:


private void btnEdit_Click (object sender, System.EventArgs e)
{
customerDataSet.Clear();
sqlDataAdapter1.Fill(customerDataSet);
// . . .
}

private void btnSave_Click (object sender, System.EventArgs e)
{
// seems simple enough, but doesn't work (for databound text boxes),
works for datagrid
sqlDataAdapter1.Update (customerDataSet);
}

If the book is indeed wrong, I plan to still work through the projects (as
idea generators) but use "Programming C#" (Liberty) for support, which seems
like a better book.

Incidentally, I've had other problems with the "C# Professional Projects"
book (the underlying database for the above project is assumed to exist and
be accessible, but no details given). Then again, being new to .NET and C#,
I may be missing something obvious. Anyone else have issues with this book?

Any constructive help is much appreciated,

Patrick
 
Hello Patrick,

The code snippets you've provided are OK. The problem is most likely in that
you have to force the databinding framework to puch the updated values from
the bound controls to the underlying datasource. This is done automatically
when you change the current record, but this might be not the case in your
program. So, try the following before calling the Save method:

CurrencyManager cm = (CurrencyManager)this.BindingContext[myDataSource, "<my
data member here>"];
cm.EndCurrentEdit();
 
Back
Top