Question System.indexoutofrangeexception

klharding

Member
Joined
Sep 16, 2008
Messages
8
Location
New Hampshire
Programming Experience
3-5
I have 2 tables, one table to list labels for data and another table that holds the data. I am creating a URL post with all of the elements. In the first function below I am reading all of the records from gns_ListColumns into an array. Then in the second function I am reading records from the appointments table and creating a string containing records from both tables. The System.IndexOfOutRangeException is happening on the second function on the line
VB.NET:
strURL += x & "=" & results(x).ToString() & "&"
It happens on the fifth column for "email". I am wondering if the string is hitting some kind of limit? I am sure it is something silly that I am missing, but I cannot figure it out.

VB.NET:
Private Function GetColumnNames()
        Dim con As New SqlConnection(strConString)
        Dim cmd As New SqlCommand("sp_GetColumnNames", con)
        Dim results As SqlDataReader = Nothing
        cmd.CommandType = CommandType.StoredProcedure

        Dim columnnames As New List(Of String)()

        Try
            con.Open()
            results = cmd.ExecuteReader

            If results.HasRows Then
                Do While results.Read
                    columnnames.Add(results("column_name").ToString())
                Loop
            End If
        Catch ex As Exception
            Messagebox.Show("GetColumnNames Error :" & DateAndTime.Now.ToString & " " & ex.ToString)
        Finally
            results.Close()
            con.Close()
            cmd.Dispose()
        End Try

        Return columnnames
    End Function

VB.NET:
Private Sub GetRecords()
        Dim con As New SqlConnection(strConString)
        Dim cmd As New SqlCommand("sp_GetAppts", con)
        Dim results As SqlDataReader = Nothing
        cmd.CommandType = CommandType.StoredProcedure
        Dim strApptPost As String = ConfigurationManager.AppSettings("ApptURLPost")
        Dim strURL As String = String.Empty
        Dim strGMFID As String = ConfigurationManager.AppSettings("GMFID")
        Dim lead_id As Long = 0

        Try
            con.Open()
            results = cmd.ExecuteReader

            If results.HasRows Then
                Do While results.Read
                    strURL = "?gmfid=" & strGMFID & "&form_id=consult_form&"
                    For Each x As String In GetColumnNames()
                        If x <> "lead_type" Then
                            strURL += x & "=" & results(x).ToString() & "&"
                        End If
                    Next

                    strURL = strURL.Substring(0, strURL.Length - 1)

                    TextBox1.Text += DateAndTime.Now & " URL: " & strApptPost & strURL & vbCrLf & vbCrLf
                Loop
            End If

        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        Finally
            results.Close()
            con.Close()
            cmd.Dispose()
        End Try
 
If you get an IndexOutOfRangeException it is because you are using an index that is out of range. What line does the exception get thrown on? What index is being used on that line? What is the valid range for that index? When you answer those last two questions it should be fairly obvious what the issue is.
 
Back
Top