BindingManager Problem

G

Guest

Consider the following:

I have two Access tables: TableA & TableB

TableA is a list of Names with a ZipID

TableB is a list of City, States, and Zips with a ZipID as a primary key
autonumbered

On a Windows Form I have two text boxes, Name (from TableA) and City (from
TableB)

I have the following code:

OleDbDataAdapter nameAdapter = new OleDbDataAdapter("SELECT
FirstName, ZipID from TableA", connection);
OleDbDataAdapter zipAdapter = new OleDbDataAdapter("SELECT ZipID,
City from TableB", connection);

DataSet completeDataSet = new DataSet();

nameAdapter.Fill(completeDataSet, "TableA");
zipAdapter.Fill(completeDataSet, "TableB");


DataRelation relation = completeDataSet.Relations.Add("FullInfo",
completeDataSet.Tables["TableA"].Columns["ZipID"],
completeDataSet.Tables["TableB"].Columns["ZipID"],false);

bManager = this.BindingContext[completeDataSet, "TableA"];
tbxParentField1.DataBindings.Add("Text", completeDataSet,
"TableA.FirstName");
tbxChildField1.DataBindings.Add("Text", completeDataSet,"TableB.City");

I have Next and Previous buttons on the form, with:

bManager.Position += 1; & bManager.Position -= 1; respectively

The form works fine for displaying the FirstName (tbxParentField1) and
scrolling through the records, but the City does not move (tbxChildField1). I
just can't figure out what I am missing when binding the TableB.City field so
that it updates based on the selected FirstName.

Any help would be greatly appreciated.

thanks,
mitch
 
D

Dave Sexton

Hi Mitch,

Try to bind tbxChildField1 through FullInfo (the relationship, not TableB):

tbxChildField1.DataBindings.Add("Text", completeDataSet,
"TableA.FullInfo.City");


BTW, you might want to consider using a strong-typed DataSet.
 
G

Guest

Hi Dave,

Thanks for your quick response, it worked like a charm. You gave a BTW, why
do you recommend a strong-typed DataSet.

---mitch

Dave Sexton said:
Hi Mitch,

Try to bind tbxChildField1 through FullInfo (the relationship, not TableB):

tbxChildField1.DataBindings.Add("Text", completeDataSet,
"TableA.FullInfo.City");


BTW, you might want to consider using a strong-typed DataSet.

--
Dave Sexton

Mitch W said:
Consider the following:

I have two Access tables: TableA & TableB

TableA is a list of Names with a ZipID

TableB is a list of City, States, and Zips with a ZipID as a primary key
autonumbered

On a Windows Form I have two text boxes, Name (from TableA) and City (from
TableB)

I have the following code:

OleDbDataAdapter nameAdapter = new OleDbDataAdapter("SELECT
FirstName, ZipID from TableA", connection);
OleDbDataAdapter zipAdapter = new OleDbDataAdapter("SELECT ZipID,
City from TableB", connection);

DataSet completeDataSet = new DataSet();

nameAdapter.Fill(completeDataSet, "TableA");
zipAdapter.Fill(completeDataSet, "TableB");


DataRelation relation = completeDataSet.Relations.Add("FullInfo",
completeDataSet.Tables["TableA"].Columns["ZipID"],
completeDataSet.Tables["TableB"].Columns["ZipID"],false);

bManager = this.BindingContext[completeDataSet, "TableA"];
tbxParentField1.DataBindings.Add("Text", completeDataSet,
"TableA.FirstName");
tbxChildField1.DataBindings.Add("Text",
completeDataSet,"TableB.City");

I have Next and Previous buttons on the form, with:

bManager.Position += 1; & bManager.Position -= 1; respectively

The form works fine for displaying the FirstName (tbxParentField1) and
scrolling through the records, but the City does not move (tbxChildField1).
I
just can't figure out what I am missing when binding the TableB.City field
so
that it updates based on the selected FirstName.

Any help would be greatly appreciated.

thanks,
mitch
 
D

Dave Sexton

Hi Mitch,

If you like to use designer support you can drag your DataSet into the Form
from the Toolbox and bind to it using the property grid.

VS 2005 has increased support for binding.

Also, strong-typed DataSets reduce the amount of code required to work with
data, reduce the amount of casting that is required (introducing type-safety)
and makes code more legible. They also encapsulate the relationships between
the tables, constraints and binding support every time you need the data on
multiple forms instead of having to rewrite the same code over and over again.

--
Dave Sexton

Mitch W said:
Hi Dave,

Thanks for your quick response, it worked like a charm. You gave a BTW, why
do you recommend a strong-typed DataSet.

---mitch

Dave Sexton said:
Hi Mitch,

Try to bind tbxChildField1 through FullInfo (the relationship, not TableB):

tbxChildField1.DataBindings.Add("Text", completeDataSet,
"TableA.FullInfo.City");


BTW, you might want to consider using a strong-typed DataSet.

--
Dave Sexton

Mitch W said:
Consider the following:

I have two Access tables: TableA & TableB

TableA is a list of Names with a ZipID

TableB is a list of City, States, and Zips with a ZipID as a primary key
autonumbered

On a Windows Form I have two text boxes, Name (from TableA) and City
(from
TableB)

I have the following code:

OleDbDataAdapter nameAdapter = new OleDbDataAdapter("SELECT
FirstName, ZipID from TableA", connection);
OleDbDataAdapter zipAdapter = new OleDbDataAdapter("SELECT ZipID,
City from TableB", connection);

DataSet completeDataSet = new DataSet();

nameAdapter.Fill(completeDataSet, "TableA");
zipAdapter.Fill(completeDataSet, "TableB");


DataRelation relation = completeDataSet.Relations.Add("FullInfo",
completeDataSet.Tables["TableA"].Columns["ZipID"],
completeDataSet.Tables["TableB"].Columns["ZipID"],false);

bManager = this.BindingContext[completeDataSet, "TableA"];
tbxParentField1.DataBindings.Add("Text", completeDataSet,
"TableA.FirstName");
tbxChildField1.DataBindings.Add("Text",
completeDataSet,"TableB.City");

I have Next and Previous buttons on the form, with:

bManager.Position += 1; & bManager.Position -= 1; respectively

The form works fine for displaying the FirstName (tbxParentField1) and
scrolling through the records, but the City does not move
(tbxChildField1).
I
just can't figure out what I am missing when binding the TableB.City
field
so
that it updates based on the selected FirstName.

Any help would be greatly appreciated.

thanks,
mitch
 
Top