howto updating a Dataset which is bound to Textboxes...

S

Sam Carleton

I have followed the "Walkthrough: Displaying Data in a Windows
Form Using a Parameterized Query" and would like to modify it to
update the row being displayed.

If you are not familiar with the walkthrough, it is very simply.
You setup a Data Connection, then a Data Adapter, and finally a
Dataset. Then you add a few textboxes to the form and bind them
to the Dataset columns. One of the textboxes (txtStateParameter )
is for the user to enter the state id to lookup the specific.
Here is the only actuall code from the walkthrough, to this point:

private void btnShow_Click(object sender, System.EventArgs e)
{
oleDbDataAdapter1.SelectCommand.Parameters["state"].Value =
txtStateParameter.Text;
dsAuthors1.Clear();
oleDbDataAdapter1.Fill(dsAuthors1);
}

The question I have now is how do I update this row when the user
changes something on the form and clicks the save button? I have
seen others posting stating that with textboxes, one must first
call the BeginEdit() and EndEdit(). I dug around and found these
methods on the DataRow object. At this point, I don't have a
DataRow, only a Dataset.

Can anyone fill me in on what I am missing? Thanks!

Sam
 
C

Christiaan van Bergen

Hi Sam,

First, understand that a DataRow is part of a DataTable, which is in its
turn part of a DataSet.
So if you want to access a DataRow in a DataSet, you must first select a
DataTable in the DataSet.
Either by index number or by name

dsAuthors1.Tables[0] <= will retrieve the first table in the set
dsAuthors1.Tables["authors"] <= will retrieve the table with the name
"authors"

After selecting the DataTable you can go after the row by index

MyTable.Rows[0] <= will retrieve the first row in the table
dsAuthors1.Tables["authors"].Rows[0] <= again, retrieve the first row

Second, if I am not mistaken the textboxes you use are databound to the
dataset. So as soon as you enter a new value in the form it will have effect
on the DataSet. Because the DataSet has now changes you might want to commit
to the database, just offer the dataset back to the DataAdapter. Something
like this

private void btnSave_Click(object sender, System.EventArgs e)
{
oleDbDataAdapter1.Update(dsAuthors1);
}

Third, the BeginEdit and EndEdit methods of the DataRow are called
implicitly when the user changes the value of a data-bound control.
Check
http://msdn.microsoft.com/library/d...frlrfsystemdatadatarowclassbeginedittopic.asp
for more info.

HTH
Christiaan
 
W

W.G. Ryan - MVP

Sam Carleton said:
I have followed the "Walkthrough: Displaying Data in a Windows
Form Using a Parameterized Query" and would like to modify it to
update the row being displayed.

If you are not familiar with the walkthrough, it is very simply.
You setup a Data Connection, then a Data Adapter, and finally a
Dataset. Then you add a few textboxes to the form and bind them
to the Dataset columns. One of the textboxes (txtStateParameter )
is for the user to enter the state id to lookup the specific.
Here is the only actuall code from the walkthrough, to this point:

private void btnShow_Click(object sender, System.EventArgs e)
{
oleDbDataAdapter1.SelectCommand.Parameters["state"].Value =
txtStateParameter.Text;
dsAuthors1.Clear();
oleDbDataAdapter1.Fill(dsAuthors1);
}

The question I have now is how do I update this row when the user
changes something on the form and clicks the save button?
oleDbDataAdapter1.Update(dsAuthors1.Tables[0]);

I have
seen others posting stating that with textboxes, one must first
call the BeginEdit() and EndEdit(). I dug around and found these
methods on the DataRow object. At this point, I don't have a
DataRow, only a Dataset.
DataSets are collections of one or more datatables and datatables are
collections of one or more datarows and datacolumns. Each item that your
textbox is bound to is a datarow so you defnintely have them. You can
reference them by bindingManagerBase[Index] or directly by using
DataSetName.Tables[TableNameOrIndex].Rows[RowIndex][ColumnNameOrIndex];

HTH,

Bill
 
S

Sam Carleton

Hi Sam,

First, understand that a DataRow is part of a DataTable, which is in its
turn part of a DataSet.
So if you want to access a DataRow in a DataSet, you must first select a
DataTable in the DataSet.
Either by index number or by name

dsAuthors1.Tables[0] <= will retrieve the first table in the set
dsAuthors1.Tables["authors"] <= will retrieve the table with the name
"authors"

After selecting the DataTable you can go after the row by index

MyTable.Rows[0] <= will retrieve the first row in the table
dsAuthors1.Tables["authors"].Rows[0] <= again, retrieve the first row

Second, if I am not mistaken the textboxes you use are databound to the
dataset. So as soon as you enter a new value in the form it will have effect
on the DataSet. Because the DataSet has now changes you might want to commit
to the database, just offer the dataset back to the DataAdapter. Something
like this

private void btnSave_Click(object sender, System.EventArgs e)
{
oleDbDataAdapter1.Update(dsAuthors1);
}

Third, the BeginEdit and EndEdit methods of the DataRow are called
implicitly when the user changes the value of a data-bound control.
Check
http://msdn.microsoft.com/library/d...frlrfsystemdatadatarowclassbeginedittopic.asp

Thank you very much, all the info is educational, but it does not
solve my problem. I have tried calling the Update method on the
adapter, but it does not work. I am thinking that I missed a step
to hook everything up correctly in Form builder. Is there
anything I need to do at the form level to bind it to the dataset
or dataadapter? I have looked through the walkthrough a few times
and have not seen anything.

Sam
 

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