views:

48

answers:

2

Please help, how do I make a while loop equivalent of this for loop. So that I could read from one row in the table of mysql database and display it on the combobox in vb.net.

I use this code, but its definitely not useful if there are 3 or more items that are added in the row:

Dim i As Integer
        Dim rdr As Odbc.OdbcDataReader
        rdr = con.readfrom_drug_type_table()
    For i = 0 To 1
        If rdr.HasRows = True Then
            rdr.Read()

            ComboBox2.Items.Add(rdr("Drug_type"))
        End If
    Next i

I want to read all the data from that the Drug_type row Please help, thanks

A: 

If you want to read only first row than just use

If rdr.Read() Then 
     ComboBox2.Items.Add(rdr("Drug_type")) 
End If 

Update

Try
    myConnection = New SqlConnection("server=localhost;uid=sa;pwd=;database=pubs")
    'you need to provide password for sql server
    myConnection.Open()
    myCommand = New SqlCommand("Select * from discounts", myConnection)
    dr = myCommand.ExecuteReader
    Do
        While dr.Read()
            WriteLine(dr(0))
            WriteLine(dr(1))
            WriteLine(dr(2))
            WriteLine(dr(3))
            WriteLine(dr(4))
            ' writing to console
        End While
Catch
End Try
dr.Close()
myConnection.Close()
Pranay Rana
I want to read all the data from the Drug_type row
Check the updaetd answer now
Pranay Rana
Thanks but doesn't work, it seems that you only copied and pasted it from somewhere else. As you can see I'm using odbc to connect to mysql database. I'm not using mysql connector.net
yeah to show you its very easy you can also find by googling antoher fact is i am for c# backgroud so cannt provice you code in vb.net
Pranay Rana
+1  A: 

@pranay You don't need the nested loops.

Try
    myConnection = New SqlConnection("server=localhost;uid=sa;pwd=;database=pubs")
    myConnection.Open()
    myCommand = New SqlCommand("Select * from discounts", myConnection)
    dr = myCommand.ExecuteReader()
    While dr.Read()
        WriteLine(dr(0))
        WriteLine(dr(1))
        WriteLine(dr(2))
        WriteLine(dr(3))
        WriteLine(dr(4))
    End While
    dr.Close()
Finally
    myConnection.Close()
End Try
JWL_