Hi, I've made a sql query to fill a sqldatreader for after go line by line processing the data. The problem I've is that I'm receiving this error message:
Code:
"System.ArgumentOutOfRangeException: El índice estaba fuera del intervalo. Debe ser un valor no negativo e inferior al tamaño de la colección. Nombre del parámetro: index    en System.ThrowHelper.ThrowArgumentOutOfRangeException()    en System.Collections.Generic.List`1.get_Item(Int32 index)    en Facturacion.Dao.CatalTipoRegistro.SetLineaFicheroTexto(LineaFicheroTexto& _lft) en C:\TORTOISE\_NT\FicheroFacturacion\Facturacion\Dao\CatalTipoRegistro.vb:línea 49"

"System.ArgumentOutOfRangeException: Index out of the interval. I must be non negative value and lower than the size the collection. Parameter name: index in System.ThrowHelper.ThrowArgumentOutOfRangeException() in System.Collections.Generic.List`1.get_Item(Int32 index) in Facturacion.Dao.CatalTipoRegistro.SetLineaFicheroTexto(LineaFicheroTexto& _lft) in C:\TORTOISE\_NT\FicheroFacturacion\Facturacion\Dao\CatalTipoRegistro.vb:línea 49"
My code is:
Code:
            Dim contadorCampo As Short = 0
            Try
                Dim sqlconn As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection()
                sqlconn.ConnectionString = ConfigurationManager.ConnectionStrings("ODP.SCGC").ToString()
                sqlconn.Open()
                Dim sql As String
                sql = " select tcam.campo,tcam.longitud,tcam.formato,tcam.n_decimales,treg.dependencia,treg.tabla,treg.Salvar_Historia "
                sql += " from FACT_TXT_CATAL_TIPO_REGISTRO treg  "
                sql += " inner join FACT_TXT_CATAL_CAMPOS tcam  "
                sql += " on treg.TIPO_REGISTRO = tcam.TIPO_REGISTRO  "
                sql += " where treg.tipo_registro = @tipo "
                sql += " AND treg.cod_estado = 2 and tcam.cod_estado=2 "
                sql += " order by tcam.campo "
                Using sqlconn
                    Dim sqlcommand As SqlClient.SqlCommand = New SqlClient.SqlCommand()
                    sqlcommand.Connection = sqlconn
                    sqlcommand.CommandText = sql
                    sqlcommand.CommandType = CommandType.Text
                    sqlcommand.Parameters.AddWithValue("@tipo", _lft.tipoRegistro)
                    Dim dr As SqlDataReader = sqlcommand.ExecuteReader()
                    While dr.Read()
                        _lft.tabla = dr("tabla")
                        If (dr("dependencia") IsNot DBNull.Value) Then _lft.dependencia = dr("dependencia")
                        If (dr("Salvar_Historia") IsNot DBNull.Value) Then _lft.SalvarHistoria = CBool(dr("Salvar_Historia"))
                        If (dr("campo") IsNot DBNull.Value) Then _lft.ListCampoFichero(contadorCampo).NombreCampoTabla = dr("campo")
                        If (dr("longitud") IsNot DBNull.Value) Then _lft.ListCampoFichero(contadorCampo).Longitud = CShort(dr("longitud"))
                        If (dr("formato") IsNot DBNull.Value) Then _lft.ListCampoFichero(contadorCampo).Formato = dr("formato")
                        If (dr("n_decimales") IsNot DBNull.Value) Then _lft.ListCampoFichero(contadorCampo).nroDecimales = CShort(dr("n_decimales"))
                        contadorCampo += 1
                    End While
                End Using
            Catch ex As Exception
                Dim str As String = ex.ToString
            End Try
It ever fails on iteration 31th on this line:
Code:
 If (dr("campo") IsNot DBNull.Value) Then _lft.ListCampoFichero(contadorCampo).NombreCampoTabla = dr("campo")
I've executed the query on my sqlserver and ever returns 38 results. Until I understand it shouldn't happen because I've more registers. I've also tried to count the rows:
Code:
Dim rows As Integer = 0
If dr.HasRows Then
  While dr.Read()
     rows += 1
  End While
End If
and rows it's equal to 38.
Any idea what's happenning and how could I solve it? Thanks.