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
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
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
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
Member
51 Points
186 Posts
Error: System.IndexOutOfRangeException
Aug 12, 2019 10:49 AM|E.RU|LINK
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")
Contributor
3370 Points
1409 Posts
Re: Error: System.IndexOutOfRangeException
Aug 13, 2019 02:51 AM|samwu|LINK
Hi E.RU,
Based on the code you provided, I cannot reproduce your problem.
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
Member
51 Points
186 Posts
Re: Error: System.IndexOutOfRangeException
Aug 13, 2019 11:03 AM|E.RU|LINK
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