Looping through a datareader in .net 1.1 reading the data into an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kamran1976
    New Member
    • Jun 2007
    • 3

    Looping through a datareader in .net 1.1 reading the data into an array

    I am trying to loop through the fields in a datareader so that i can read the data into an array. It works the first time around i.e the first column, but when i=1 a get an "Object reference not set to an instance of an object" error. Please tell me what i am doing wrong!

    heres the code:

    If rdr.HasRows Then
    rdr.Read()
    For i = 0 To rdr.FieldCount
    arrValues(i) = CStr(rdr.Item(i ))
    Next
    End If
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    Originally posted by kamran1976
    I am trying to loop through the fields in a datareader so that i can read the data into an array. It works the first time around i.e the first column, but when i=1 a get an "Object reference not set to an instance of an object" error. Please tell me what i am doing wrong!

    heres the code:

    If rdr.HasRows Then
    rdr.Read()
    For i = 0 To rdr.FieldCount
    arrValues(i) = CStr(rdr.Item(i ))
    Next
    End If
    You may want to try something like this.

    [CODE=vbnet]Dim i As Integer = 0

    While rdr.Read
    arrValues(i) = CStr(rdr.Item(i ))
    i = (i + 1)

    End While[/CODE]

    Nathan

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Originally posted by nateraaaa
      You may want to try something like this.

      [CODE=vbnet]Dim i As Integer = 0

      While rdr.Read
      arrValues(i) = CStr(rdr.Item(i ))
      i = (i + 1)

      End While[/CODE]

      Nathan
      Nathan's code should work for you but I still recommend putting it within a Try Catch block just in case.

      -Frinny

      Comment

      • kamran1976
        New Member
        • Jun 2007
        • 3

        #4
        That doesnt work - i am still getting the same error. Here is the full code snippet. the datareader should contain 1 row with 35 columns in it:
        [code=vbnet]

        Dim i
        Dim rdr As SqlDataReader
        Dim cn As New SqlConnection(C onfigurationSet tings.AppSettin gs("sConnection String"))
        cn.Open()

        Dim cmd As New SqlCommand("SEL ECT * FROM tblUser WHERE UserID=" & Session("UserID "), cn)
        rdr = cmd.ExecuteRead er
        Dim arrValues() As String

        If rdr.HasRows Then
        rdr.Read()
        For i = 0 To rdr.FieldCount
        arrValues(i) = CStr(rdr.Item(i ))
        Next
        End If
        cn.Close()
        For i = LBound(arrValue s) To UBound(arrValue s)
        Response.Write( arrValues(i))
        Next[/code]

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Check out this .Net Article on how to use a database in your program....there's an example there on how to retrieve your values.

          Try it out and let me know if you still have problems :)

          -Frinny

          Comment

          • kamran1976
            New Member
            • Jun 2007
            • 3

            #6
            it was the array that was causing the problem - i had to resize it for each iteration of the loop. the following code sample now operates as expected:

            Code:
            If rdr.HasRows Then
            rdr.Read()
            For i = 0 To rdr.FieldCount - 1
            ReDim Preserve arrValues(i)
            arrValues(i) = CStr(rdr.Item(i))
            Next
            End If
            thanks for your help!
            Kamran

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              Originally posted by kamran1976
              it was the array that was causing the problem - i had to resize it for each iteration of the loop. the following code sample now operates as expected:

              Code:
              If rdr.HasRows Then
              rdr.Read()
              For i = 0 To rdr.FieldCount - 1
              ReDim Preserve arrValues(i)
              arrValues(i) = CStr(rdr.Item(i))
              Next
              End If
              thanks for your help!
              Kamran

              Good Stuff!

              -Frinny

              Comment

              Working...