locked
Error: System.IndexOutOfRangeException RRS feed

  • Question

  • User1717218719 posted

    I have the following code which when I run I get the following error "System.IndexOutOfRangeException" on the line

    any help as to why this error is happening would be great.

    txtbox.Text = sdr("Typ") & sdr("Amt")
        Protected Sub GetDataBase(ByVal argparam As Integer)
            conConn = New SqlConnection(GblSqlCon)
    
            '--Read from Database
            comComm = New SqlCommand
            With comComm
                .Connection = conConn
                .CommandType = CommandType.Text
                .CommandText = "SELECT stuff(( " &
                 "SELECT ',' + LTRIM(RTRIM(Typ)) + ' ' + LTRIM(RTRIM(Amt)) " &
                "FROM tbl_DocAmtT INNER JOIN " &
                "tbl_DocAmt ON tbl_DocAmtT.TktDocID = tbl_DocAmt.TktDocID " &
                "INNER JOIN tbl_Tkts ON tbl_DocAmt.ID = tbl_Tkts.ID " &
                "WHERE tbl_DocAmt.ID = tbl_Tkts.ID " &
                "FOR XML PATH('')), 1, 2, '') As Lst ;"
    
                .Parameters.AddWithValue("@ID", argparam)
            End With
    
            conConn.Open()
            Using sdr As SqlDataReader = comComm.ExecuteReader(CommandBehavior.SingleRow)
    
                sdr.Read()
    
                If sdr.HasRows Then
    
                    Call SetVals(sdr)
    
                Else
                    Response.Write("no records")
                End If
            End Using
            conConn.Close()
    
        End Sub
    
    
        Protected Sub SetTaxesVals(ByVal sdr As SqlDataReader)
    
            txtbox.Text = sdr("Typ") & sdr("Amt")
    
        End Sub
    
    
    
    
      Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
       Call GetDataBase(1)
      End Sub

    Monday, August 12, 2019 10:49 AM

Answers

  • User288213138 posted

    Hi E.RU,

    Based on the code you provided, I cannot reproduce your problem.

    E.RU

    Protected Sub SetTaxesVals(ByVal sdr As SqlDataReader) txtbox.Text = sdr("Typ") & sdr("Amt") End Sub

    Where did you trigger the SetTaxesVals () method?

    The error : syste.IndexOutOfRangeException is usually that the column name does not exist in the database.

    So plesde check if the columns Typ and Amt in the database exist.

    I suggest you access by index instead of column name.

    Public str As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Private query As String = "select * from Customer"
    
        Protected Sub GetDataBase(ByVal argparam As Integer)
            Dim conConn As SqlConnection = New SqlConnection()
            Dim comComm As SqlCommand = New SqlCommand()
            conConn = New SqlConnection(str)
            comComm = New SqlCommand()
    
            If True Then
                Dim withBlock = comComm
                withBlock.Connection = conConn
                withBlock.CommandType = CommandType.Text
                withBlock.CommandText = query
                withBlock.Parameters.AddWithValue("@CustomerID", argparam)
            End If
    
            conConn.Open()
    
            Using sdr As SqlDataReader = comComm.ExecuteReader(CommandBehavior.SingleRow)
    
                If sdr.Read() Then
    
                    If sdr.HasRows Then
                        SetTaxesVals(sdr)
                    Else
                        Response.Write("no records")
                    End If
                End If
            End Using
    
            conConn.Close()
        End Sub
    
        Protected Sub SetTaxesVals(ByVal sdr As SqlDataReader)
            TextBox1.Text = Convert.ToString(sdr(1)) + Convert.ToString(sdr(2))
        End Sub
    
        Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            GetDataBase(1)
        End Sub

    The result:

    Best regards,
    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 13, 2019 2:51 AM

All replies

  • User288213138 posted

    Hi E.RU,

    Based on the code you provided, I cannot reproduce your problem.

    E.RU

    Protected Sub SetTaxesVals(ByVal sdr As SqlDataReader) txtbox.Text = sdr("Typ") & sdr("Amt") End Sub

    Where did you trigger the SetTaxesVals () method?

    The error : syste.IndexOutOfRangeException is usually that the column name does not exist in the database.

    So plesde check if the columns Typ and Amt in the database exist.

    I suggest you access by index instead of column name.

    Public str As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Private query As String = "select * from Customer"
    
        Protected Sub GetDataBase(ByVal argparam As Integer)
            Dim conConn As SqlConnection = New SqlConnection()
            Dim comComm As SqlCommand = New SqlCommand()
            conConn = New SqlConnection(str)
            comComm = New SqlCommand()
    
            If True Then
                Dim withBlock = comComm
                withBlock.Connection = conConn
                withBlock.CommandType = CommandType.Text
                withBlock.CommandText = query
                withBlock.Parameters.AddWithValue("@CustomerID", argparam)
            End If
    
            conConn.Open()
    
            Using sdr As SqlDataReader = comComm.ExecuteReader(CommandBehavior.SingleRow)
    
                If sdr.Read() Then
    
                    If sdr.HasRows Then
                        SetTaxesVals(sdr)
                    Else
                        Response.Write("no records")
                    End If
                End If
            End Using
    
            conConn.Close()
        End Sub
    
        Protected Sub SetTaxesVals(ByVal sdr As SqlDataReader)
            TextBox1.Text = Convert.ToString(sdr(1)) + Convert.ToString(sdr(2))
        End Sub
    
        Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            GetDataBase(1)
        End Sub

    The result:

    Best regards,
    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 13, 2019 2:51 AM
  • User1717218719 posted

    Thank you for your reply I managed to solve the problem. the code which specified which colums to read was not working and as a result the code was failing. I set the index to 0 also

    Tuesday, August 13, 2019 11:03 AM