简体   繁体   中英

How to load items in combobox from data stored in database

I am trying to load some item in combo box from the data stored in the database. I have a button when clicked it populates company details in the relevant boxes properly. and in the database i have three fields like

Dev = Yes or No
Fin = Yes or No
Net = Yes or No

where Dev is the field name and Yes is the text stored in the database.

i read all details for a company in reader so i tried something like this.

If reader(14).ToString = "Yes" then
   combobox1.items.add("Developer")
else if reader(15).ToString = "Yes" Then
   combobox1.items.add("Finance")
Else if reader(15).ToString = "Yes" Then
   combobox1.items.add("Networking")
End iF

It is not working any ideas how to achieve this?

Try this, You are checking this condition else if reader(15).ToString = "Yes" Then twice in your code, May be this could be a problem for you.

If reader(14).ToString = "Yes" then
   combobox1.items.add("Developer")
else if reader(15).ToString = "Yes" Then
   combobox1.items.add("Finance")

'--------------\/ May be this could be your problem. 
Else if reader(15).ToString = "Yes" Then     
   combobox1.items.add("Networking")
End iF

Consider this too, This may solve your casing problems.

    If reader("Dev").ToString.ToUpper() = "YES" then
       combobox1.items.add("Developer")
    else if reader("Fin").ToString.ToUpper() = "YES" Then
       combobox1.items.add("Finance")
    Else if reader("Net").ToString.ToUpper() = "YES" Then     
       combobox1.items.add("Networking")
    End iF

Use Equals to check the values, the name for the field, and ElseIf (all together) for the conditions, and check if that solves your problem:

If reader("Dev").ToString.Equals("yes", StringComparison.InvariantCultureIgnoreCase) Then
    ComboBox1.Items.Add("Developer")
ElseIf reader("Fin").ToString.Equals("yes", StringComparison.InvariantCultureIgnoreCase) Then
    ComboBox1.Items.Add("Finance")
ElseIf reader("Net").ToString.Equals("yes", StringComparison.InvariantCultureIgnoreCase) Then
    ComboBox1.Items.Add("Networking")
EndIf

But it would be better if you store the values in a different way in your database. For example, you can store only the type in one field. being "Dev", "Fin" or "Net" for example. Or even better, have a new table with the id, values and codes and only use the id.

If reader(9).ToString = "yes" Then
    ComboBox1.Items.Add("Developer")
ElseIf reader(9).ToString = "no" Then
End If
If reader(11).ToString = "yes" Then
    ComboBox1.Items.Add("Finance")
ElseIf reader(11).ToString = "no" Then
End If
If reader(10).ToString = "yes" Then
    ComboBox1.Items.Add("Networking")
ElseIf reader(10).ToString = "no" Then
End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM