I have a Combo Box Situation

chrisguk

Active member
Joined
Nov 20, 2011
Messages
29
Programming Experience
Beginner
I have created a service based DB with two tables as a test.


Table structure is as follows:


tblSIMStock:
pkStockID - int
StockDetail
DataNumber
fkStockLocationID


tblStockLocation:
pkStockLocationID - int
LocationDetail


There is a relation setup between fkStockLocationID and pkStockLocationID


This is also added to the dataset diagram.


I drag the tablle SIMStock to the form and then change the field fkStockLocation ID to a combo box. I ensure it is setup as a bound item with tblStockLocationBindingsource.


This is where the problem starts. I debug and then add a new record, I select an item from the combo box and click save. Then add another record and so on. My combo box values do not change according to the values I previously selected. So for example, I have 3 values:


Home
Away
Repair


I select create 3 records with each of them populated. But when I re-navigate through the records they all default to home again.


Any ideas guys?
 
I removed the following extraneous bindingsources before the video starts:
StockLocationBindingSource1
FKSimStockStockLocationBindingSource

For the rest of my actions and an explanation, see the video..
Note that the most critical parts of my posts above (unbind text, bind selected value, change to dropdownlist) were not done in the project supplied.. These are essential foir the very logical reason of:

You expect your combo to use a list of pre-programmed values, you cannot allow the user to edit (style:dropdownlist) because what if they edit it to be a location that isnt in the db?
If you leave Text bound, then whatever is showing as the combo's Text will be written to the SimStock table fkStockLocation column.. this is either nothing (probably in the case of bound items) or it's the text you literally see.. and fkStockLocation cannot accept a value of "Warehouse", it has to accept -1, which means "Warehouse" when decoded by StockLcation table

See attached
 

Attachments

  • Desktop.zip
    1,017 KB · Views: 33
Hi there,

Im actually lost for words because you have gone to great lengths to show me a way to understand. I cant thank you enough, I will study the concepts throughly and hope to build on them in the future.

Again thanks

Chris
 
Hi cjard,, me again.

I ran the project but everytime I goto save the SIMStock form the app throws out an error. due to the foriegn key contraint.

Any ideas?

Chris
 
Mmm.. Try doing StockLocationBindingSource.EndEdit() and saving your StockLocation before you save simstock.. Actually you should use Access, Visual Studio Server Explorer or similar to populate your StockLocation table ahead of time, so the values are already in it when you load the form - know what I mean?

As in, set up your DB with any relevant values first, before even running the program..

Make sure the StockLocation had a Fill() in the form load so that the datatable is filled with data

Note that you didnt actually say which FK is causing the problem. When I looked at your dataset, there were relationships everywhere.. It could be another relationship causing the problem. Make sure that any other parent tables also have values
 
Hiya,

After I did what you said with pre-populating the data it all works fine, again many thanks for you help:
 
Back
Top