Ng Yang Yang
Ng Yang Yang

Reputation: 35

unable to read data from more than 1 row

i have a problem reading data from the table. This is the sample of my coding

Dim SqlQry As String = "SELECT Distinct(RechargeAmt) from KioskItem where TelcoID='" & TelcoID & "'"
        Dim SqlCmd As New SqlCommand(SqlQry, Conn)
        Dim dr As SqlDataReader = SqlCmd.ExecuteReader
        dr.Read()
        If dr.HasRows Then
            While dr.Read()
                Dim SqlQry As String = "SELECT Distinct(RechargeAmt) from KioskItem where TelcoID='" & TelcoID & "'"
        Dim SqlCmd As New SqlCommand(SqlQry, Conn)
        Dim dr As SqlDataReader = SqlCmd.ExecuteReader
        dr.Read()
        If dr.HasRows Then
            While dr.Read()
                RechargeAmt = dr(0)
                a = a & RechargeAmt & ";"
            End While
        End If
        If a = 0 Then
            Return RechargeAmt
        Else
            Return a
        End If           

My problem is if the data has more than one row, it has no problem saving the data into a string but when my data in the table has only one rowm, it is unable to read the data. i try this

If dr.HasRows Then
            RechargeAmt = dr(0)
            While dr.Read()
                a = a & RechargeAmt & ";"
            End While
        End If
        If a = 0 Then
            Return RechargeAmt
        Else
            Return a
        End If      

but it still unable to read the data from the table

Upvotes: 0

Views: 134

Answers (1)

nunespascal
nunespascal

Reputation: 17724

That is because you are always missing the first row.

See here

 dr.Read()               //Gets the first row
    If dr.HasRows Then
        While dr.Read()  //Gets the second row onwards

You are always skipping the first row.

The initial read is not required. You could start directly with

 While dr.Read()

Control would go inside the loop only if there were rows.

Upvotes: 1

Related Questions