Bindings And Database Updates - ADO.NET

D

Darryn Ross

Hi,

I am trying to create am update procedure in my application for my access
database records, the controls (text boxes) on my form are bound to the
database, when i go to apply the changes i have made to the fields on the
form, the original details are restored and all my changes are lost, I know
it has something to do with the binding i have setup but am unsure how to
get around it.

Here is my binding code followed by the update procedure i use...

SqlSelect = "Select * From tbl WHERE Code=" + "'" + StrCode + "'" ;
//Connection details
DtlsConnection.ConnectionString = StrConnectionPath ;
DtlsConnection.Open() ;
//Command details
DtlsCommand.CommandText = SqlSelect ;
DtlsCommand.Connection = DtlsConnection ;
//Adapter details
DtlsAdapter.SelectCommand = SelectDtlsCommand ;
DtlsAdapter.Fill(DtlsDs, "tbl") ;

//Binding text boxes to their specified database fields
txtName.DataBindings.Add(new Binding
("Text", DtlsDs, "tbl.Name")) ;
txtType.DataBindings.Add(new Binding
("Text", DtlsDs, "tbl.Type")) ;

DtlsConnection.Close() ;


/*--------------------------------------------------------------------------
-----------*/


DtlsConnection.ConnectionString = StrConnectionPath ;
DtlsConnection.Open() ;

DtlsCommand.CommandText = "Select * From tbl" ;
DtlsCommand.Connection = DtlsConnection ;
DtlsAdapter.SelectCommand = DtlsCommand ;
DtlsAdapterInsertCommand() ;
DtlsAdapterUpdateCommand() ;
DtlsAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey ;
DtlsAdapter.Fill(DtlsDs, "tbl") ;

DataRow tblRow ;
tblRow = DtlsDs.Tables["tbl"].Rows.Find(StrCode) ;
tblRow.BeginEdit() ;

try {
tblRow["Name"] = txtName.Text ; //this is not changing??
tblRow["Type"] = txtType.Text ; //this is not changing??
tblRow.EndEdit() ;
DtlsAdapter.Update(DtlsDs, "tbl") ;
}
catch(Exception e) {
MessageBox.Show(e.Message, "Error Updating Record", MessageBoxButtons.OK,
MessageBoxIcon.Error) ;
tblRow.CancelEdit() ;
}

Any help would be appreciated...

Kind Regards
Darryn Ross
 
S

Stephen Muecke

Darryn
Your controls are not bound to the 'database', they are bound to a dataset
(which is a disconnected, in-memory copy of the data from your database)
When you change the text in the textboxes, you are updating the dataset.
In order to update the database, you must call the Update method of your
DataAdaptor (DtlsAdapter.Update)
Note also that you should close the connection immediately after call the
Fill method.
Stephen
 
D

Darryn Ross

Stephen,

As you can see from my code i submitted, i am updating the dataset
through the adapter. That is not where my problem is, the problem is
that the information in my text fields is not changing even if i change
the text thats in them, this has been noted as a comment in my code
'//This is not changing'. The system isn't picking up the changes i make
to the text boxes and thus i think it has something to do with me
binding them to the dataset, this is causing the same information to be
saved each time.

Darryn
 

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

Similar Threads


Top