Link to home
Start Free TrialLog in
Avatar of K Feening
K FeeningFlag for Australia

asked on

Vb.net Reading sql server table

Hi Experts

Using while reader.read to get the records from a sql Database table
there are 4 records but using the following I am getting only the first record

Dim sql as String = "Select Description from testfilename"
Dim Count as Integer = 0

Read test File

Do while Reader.read
      Dim FieldName as Reader.getstring(count)
     count = count + 1
     combobox.items.add(fieldname)
loop

Thanks for the help
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

>Dim FieldName as Reader.getstring(count)
is likely
Dim FieldName as String = Reader.getstring(0)
Avatar of Akilandeshwari N
Akilandeshwari N

Declare the variable FieldName outside the loop
Avatar of K Feening

ASKER

Sorry to Guy Hengel Dim FieldName as Reader.getstring(count) was actually
Dim FieldName as String = Reader.getstring(count)  

Declaring the variable outside the loop still didn't change the problem

I want all records so I setup the reader as company.datacollector.getreader(sql1, Nothing)
as it wouldn't accept .getreader(sql1) it needed .getreader(sql1, Nothing)

Is that causing the single record

Hope this makes sense
>Reader.getstring(count)  
is incorrect if you get 4 records with 1 field;
if you get 1 record with 4 fields, you shall not "loop" on the Reader.Read, but just do a "Read once", and then loop on the field count
in which case the code would be:
if Reader.read
      do while count < Reader.Columns.Count
         Dim FieldName as Reader.getstring(count)
        count = count + 1
       combobox.items.add(fieldname)
   end loop
end if 

Open in new window

Thanks again

Not 1 record with 4 fields 4 records but only getting return of 1 record

What is the code needed to return all 4 records

The table has an Index and a description I need all 4 descriptions
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you

I hope to be able to use your help again it was excellent