all groups > vb.net data > january 2007 >
You're in the

vb.net data

group:

Data Bind from Child table To Parent Table


Data Bind from Child table To Parent Table Mar72Vin
1/12/2007 7:11:46 PM
vb.net data:
Hi All,
I have two tables in a dataset with a relation.

tblPerson and tblAddress

tblPerson.PostalAddressID relates to tblAddress.AddressID

I have a DataGrid bound to tblPerson. I can change the current record
and all related textboxes on the form are updated correctly through
binding. txtFirstName, txtSurname etc.

My relations are setup as follows:

ChildColumn = tblPerson.Columns("PostalAddressID")
ParentColumn = tblAddress.Columns("AddressID")

relNewRelation = New DataRelation( _
tblPersontblAddress, ParentColumn, ChildColumn)

My problem is that when the relation between the two tables is setup
the tblAddress must be the Parent Table because the FK exists on
tblPerson.

If i make the parent table tblPerson i get the message "These Columns
don't currently have unique values".

Currently if i try the following i get an error "Child list for field
tblPersontblAddress cannot be created."

TextBox1.DataBindings.Add("Text", tblPerson,
"tblPersontblAddress.StreetNumber")

Any suggestions?
Re: Data Bind from Child table To Parent Table RobinS
1/12/2007 11:04:14 PM
Why would you do this? Are you trying to get all the
parent addresses to show up for selection for the child address?

Robin S.
-----------------------------------
[quoted text, click to view]

Re: Data Bind from Child table To Parent Table Mar72Vin
1/13/2007 4:36:09 PM
Hi Robin,
Thanks for the reply :)

I have a datagrid that is bound to tblPerson. This datagrid lists
"Persons" (Firstname and LastName).
When the record changes in the datagrid i have textboxes bound to the
same datatable that show other information from tblPerson (Firstname,
PhoneNumber, etc).

I also have tblAddress that has values that i would like shown in
textboxes (StreetNumber, addressline1, etc)

tblPerson.PostalAddressID is the FK for tblAddress.AddressID

How can i bind text boxes to tblAddress so that address details are
updated on screen when the child table bound to the datagrid
(tblPerson) moves?

It seems to me that i am forced to make tblAddress the Parent table in
the relation because the FK exists on tblPerson. I cant seem to make
databinding work in this situation where i change records in the child
table and expect the parent table to refresh with it.

cheers,
Tim

[quoted text, click to view]
Re: Data Bind from Child table To Parent Table RobinS
1/13/2007 11:20:08 PM
Webforms or Windows Forms? VB2003 or VB2005?

Thanks,
Robin S.
--------------------------------------
[quoted text, click to view]

Re: Data Bind from Child table To Parent Table Mar72Vin
1/14/2007 1:19:20 AM
Hi Robin,
I am creating a windows form using VB.net 2005 Express edition.

Cheers,
Tim

[quoted text, click to view]
Re: Data Bind from Child table To Parent Table RobinS
1/14/2007 4:46:15 PM
tblPerson should be the parent, not the child. It has a field
called PostalAddressID that links up with tblAddress.AddressID,
right? Can you actually see the data relation between those two
tables somewhere? (I'm not sure how you're creating your dataset.)

I don't understand why the FK is defined from person to address;
maybe you are misunderstanding it?

Both of your tables need to be in the same DataSet. Assuming
they are, try this:

Add a binding source (drag it from the Data components in the
Toolbox and drop it on the form), call it PersonBindingSource.

Add another binding source, call it AddressBindingSource.

PersonBindingSource.DataSource = tblPerson
AddressBindingSource.DataSource = PersonBindingSource

'bind the child using the foreign key to the parent
AddressBindingSource.DataMember = "FK_Address_Person"

Bind all the fields that are in tblPerson to the
PersonBindingSource instead of to tblPerson directly.

Bind all of the fields from the child to
AddressBindingSource.

Does this help?

If you'd still having an FK problem, then re-post and tell
me how you are creating the two tables.

Robin S.

Re: Data Bind from Child table To Parent Table Mar72Vin
1/26/2007 7:01:38 PM
Hi Robin,
Sorry to take so long to get back to you i have been away for a few
weeks.

I have tried to bind all controls like you have suggested but still
have a few issues.

I am filling the dataset (contains both tblPerson and tblAddress with a
datarelation) and binding controls all at run-time.

PostalAddressID is defiantly on tblPerson. (tblPerson.PostalAddressID
-> tblAddress.AddressID)

PostalAddressID is defined on tblPerson because tblAddress can also be
joined to other tables i.e. tblCompany.PostalAddressID ->
tblAddress.AddressID, tblPerson.PhysicalAddressID ->
tblAddress.AddressID, etc...

Previously, because of the way the relations are defined i had to make
tblAddress the parent table in the relation because of the error:
"These Columns don't currently have unique values".

I have now changed every tblPerson to have their own unique
PostalAddressID, Now i can make tblAddress the child table without
receiving the error above.

My question now becomes; How can I define the relation (at run-time)
between tblPerson.PostalAddressID and tblAddress.AddressID if more than
one tblPerson record can have the same PostalAddressID (i.e. Link to
the same tblAddress record)

We are almost there :)

Thanks again for all you help so far.

Cheers,
Tim

[quoted text, click to view]
Re: Data Bind from Child table To Parent Table RobinS
2/1/2007 10:08:29 AM
Just to review, you have two tables in a dataset, one is tblPerson, the
other is tblAddress.

tblPerson should only have a unique PostalAddressID if every person has a
different address. Is that the case? I'm confused, because then later in
your post, you say that you have multiple people with the same
PeopleAddressID.

What you *should* have is this:

tblAddress should be a list of addresses, with no duplication,
each keyed by AddressID.

tblPerson should have an AddressID field that refers to the
entry in tblAddress that it uses.

All of your other tables that use Address should have an AddressID
field that refers to the appropriate entry in tblAddress.

How is your database set up? Are you the one doing it, or is somebody else?
What is the database? Is it SQLServer, Oracle, Access? Are there any data
relations specified on the tables?

Are your tables defined in a strongly typed dataset? (It looks like they
are from the code you are posting). If so, in the dataset designer, do you
see any relations drawn between the tables?

Answer my q's, and we'll get this done.

Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
----------------------------------------------

[quoted text, click to view]

AddThis Social Bookmark Button