Help -- datasource property vs databinding??

A

amy

Hi all,
This is a school project. I used SQLServer & am trying to
create my GUI in .NET. Here's my problem:

I'm trying to bind a datasource to a combobox control on
my Advertising form so the user is forced to choose from a
list of companies (the company name field has a database
constraint that it cannot be null). I used a wizard to
create my data adapters & the dataset; both tables
(Company & Advertising) are in the dataset. I set the
datasource & displaymember to the Company info. When I go
to run the form the Company Names show up in the combobox
like I want; however, when I try to save the data to the
Advertising Table it tells me it can't because the
CompanyName field cannot be null. How do I get my record
to update to the Advertising table?

Here are my two tables:
Company (Parent)
field name: CompanyName (PK)

Advertising (Child)
field name: CompanyName (FK)

Any help is greatly appreciated. Thanks in advance.

Amy
 
S

Stephen Muecke

Amy
Firstly you should make some changes to improve you database design (In the
Advertising Table you should store the ID of the company, not the name)

CompaniesTable
ID (PK, Int, Identity)
Name (varchar)

AdvertisingTable
Company (int, FK to ID in CompaniesTable)

In you form Load event, set the ComboBox properties
With myComboBox
.DataSource = CompaniesTable
.DisplayMember = "Name"
.ValueMember = "ID"
'Bind the ValueMember to the Company field in the AdvertisingTable
.DataBindings.Add("ValueMember",AdvertisingTable,"Company")
End With

Stephen
 

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