Click here to Skip to main content
15,905,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to use an SQLreader to fill a combobox, I watched a video on youtube and a guy had a solution that I thought would work for me also. when I tried the 2 lines of code he provided after my sqldatareader I got an "'Conversion from string "business license number" to type 'Integer' is not valid.'"

here is the code:
Dim buslicense As String = dr.GetString("business license number").
         businesslicensecombobox.Items.Add(buslicense)


What I have tried:

tried to switch it to use getint32 instead of the getstring and it gave me the same error message.
Posted
Updated 10-Oct-17 2:01am

1 solution

You can remove the "GetString" and use the below code, it will as you expected

Dim buslicense As String = dr("business license number").ToString
businesslicensecombobox.Items.Add(buslicense)



And if you want to us "GetString" then in the select statement check the index for the column and give the index id like

VB
'if your select statement is "SELECT [business license number] from yourtable"
Dim buslicense As String = dr.GetString(0).ToString
businesslicensecombobox.Items.Add(buslicense)
 
Share this answer
 
v2
Comments
Member 11856456 10-Oct-17 13:03pm    
So either way that I tried that you have listed here only seems to give the first result of the sql select statement. I was hoping to fill the combobox with all the results.

I tried to iterate through and force populate, but the problem now is that it repeats the same result.

here is what I am trying to use, maybe you could lead me in the right direction.

For i As Integer = 0 To newdts2.Rows.Count - 1

Dim buslicense As String = dr("business license number").ToString
businesslicensecombobox.Items.Add(buslicense)

Next
Shah Chandra 10-Oct-17 13:17pm    
You could try do... loop instead of for as below

do while reader.read
Dim buslicense As String = dr("business license number").ToString
businesslicensecombobox.Items.Add(buslicense)
loop

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900