Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15057

[RESOLVED] ComboBox using database index - a better way

$
0
0
I'm pretty sure I fell back into my VB 6.0 safety zone and solved a issue with a hammer...I'd like to find a better way. I have a table of reference values and I save the primary key of the refence table as a foreign key on the database table with claim information. I sort the reference table by description so the foreign key values are in a random order. So when I use the foreign key to set the selected index it pulls up the wrong ComboBox entry. For example, look at the screen print, if "Hearing Loss" was selected than the value saved is "5". When I reload the screen and use "5" to set the selected index it will display "Poisoning" as the display because the is its placement when I loaded it. Here is where the hammer comes in. When I load the ComboBox I save the value off the database next to the sequential number of the entries being loaded in a List of:

Code:

While SQLReader.Read()
    cboInjuryType.Items.Add(New ComboboxItems(SQLReader.Item("pkRefOSHAInjuryType"), SQLReader.Item("Description")))
    InjuryTypeDetail.ComboBoxIndex = mAddItemCtr                -------------------------------------------------------------------------------- sequential number of the entries being loaded
    InjuryTypeDetail.DataBaseIndex = SQLReader.Item("pkRefOSHAInjuryType") --------------------------------------------------------------- Primary key of reference table.
    InjuryTypeList.Add(InjuryTypeDetail)
    mAddItemCtr = mAddItemCtr + 1
End While

Then when I load the ComboBox I do this:

Code:

For Each Me.InjuryTypeDetail In InjuryTypeList
If SQLReader.Item("fkRefOSHAInjuryType") = InjuryTypeDetail.DataBaseIndex Then
    mInjuryDetailSelectedIndex = InjuryTypeDetail.ComboBoxIndex
    Exit For
End If
Next

That give me the correct entry.

Code:

cboInjuryType.SelectedIndex = mInjuryDetailSelectedIndex
The old VB 6.0 method I used doesn't work the same in .Net.

In the screen print the sequential count represents the ComboBox entry and the second column the primary key from the reference table (I know it really starts at zero). My issue was what is selection stored on the database doesn't match the selected indexes in the ComboBox. I used the List of to match them up.
Attached Images
 

Viewing all articles
Browse latest Browse all 15057

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>