Help -- datasource property vs databinding??

  • Thread starter Thread starter amy
  • Start date Start date
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
 
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
 
Back
Top