using a combobox as a lookup for values from another table.
Hi, Using VS 2005, SQL Express 2005 in a Winforms app
I hope someone can help me as I have now read so many different articles, I'm getting very confused and frustrated.
Scenario: I have a QUOTES form where the user will fill out various information, ie: date, quote number, etc. Importantly I want the user to select the Client that the quote is being created for. The client names are from the Clients table, I want to display the 'Company Name' and save into the Quotes Table, the ClientID.
What I have Done:
1. Created a windows form, created a QUOTES dataset which contains all the Quote table information only. Dragged and dropped the 'Details' onto this form.
2. Changed the existing text box on this form to a combo box for the clientID/company name.
3. On the Quotes FORM_Load event, the following code appears:
Me
.ClientsTableAdapter.Fill(Me
.DSClients.Clients)Me
.QuotesTableAdapter.Fill(Me
.DSQuote.Quotes)WithMe.cboClientID
.DataSource =Me.DSClients.Clients
.DisplayMember ="CompanyName"
.ValueMember =
"ClientID".SelectedIndex = -1
EndWith
4. On theClientIDComboBox_SelectedIndexChanged event
TextBox1.Text =Me.cboClientID.SelectedValue 'this captures the value of the combobox
5. I have set the textbox1 Databinds.Text value to QuotesBindingSource - ClientID (maybe I don't need to do this.)
6. On the save button, I have the following code:
Me.Validate()Me.ClientsBindingSource.EndEdit()Me.QuotesBindingSource.EndEdit()<- errorMe.QuotesTableAdapter.Update(Me.DSQuote.Quotes) When I press the Save Button, the value from the textbox1 and the cboClientID just disappear. WHAT am I doing wrong? I would think this is not a hard thing to do, but it seems to be eluding me.
error: Column 'ClientID' does not allow nulls.
TIA for help.
Michelle
Hi,
forget the "with" part of your code there is a simplest way
delete the comboBox from your form
expand on the datasource the Quotes
use the smart tag and change the CliendID field to combobox
drag and drop clientID on the form
drag and drop the client table on the combobox
expand the smart tag of the combobox and at the displaymember choose CompanyName from the dropdown
Thank for your reply. I'm not sure I understand some of the points that you make.
> delete the comboBox from your form - Done
> expand on the datasource the Quotes.
I'm not sure what you mean by this. Can you please explain.
> use the smart tag and change the CliendID field to combobox - I did this previously when I dropped the Quotes dataset using 'details' onto the form. So the combobox is from the Quotes dataset.
> drag and drop clientID on the form - see above
> drag and drop the client table on the combobox
I'm not sure what you mean by this. Do you mean to drop the clients dataset (grid/details) onto the form?
> expand the smart tag of the combobox and at the displaymember choose CompanyName from the dropdown
In order to set the Displaymember property, I need to set the bindings to something. Am i setting the same settings that I had previously set in code that I commented out?
Thanks for your information, I look forward to further clarification.
Michelle
hi,
at the datasource window you can see the Clients table
drag and drop the Client table on the comboBox not on the form surface
vb 2005 will automaticly create a new bindingsource most probably named ClientsBindingSource1
now if you expand the smart tag of the combobox on your form
you will see that it has turned to databound mode
and that there is the newly created ClientsBindingSource1, a displaymembed, valuemember and a selectedvalue
for more information check this on your help system : Walkthrough: Creating a Lookup Table