Datatable does not return the first row of the query, C #

0

I'm trying to print two columns of a MySQL query in the console using the following code:

public void cargarHorario()
    {
        DataTable tabla1 = new DataTable();
        maestro.NoMaestro = 23041;
        tabla1.Load(DAOMaestro.seleccionarHorarioMaestro(maestro));

        int noFilas = tabla1.Rows.Count;
        Console.WriteLine(noFilas.ToString());

        for (noFilas = 0; noFilas < tabla1.Rows.Count; noFilas++)
        {                
            Console.WriteLine($"{tabla1.Rows[noFilas]["dia"].ToString()} {tabla1.Rows[noFilas]["hora"].ToString()}");
        } 
    }

The problem is that running the application generates an error of type IndexOutOfRangeException . However, when obtaining the total number of rows with tabla1.Rows.Count I realized that this always returns a value minus the result of my query. If I send one that should give me 18, my count is 17. On the other hand, when I see the results of the console, the first row of the query is not, the impression starts from the second. Does anyone know what may be happening?

My code where I get the query is:

public static MySqlDataReader seleccionarHorarioMaestro(Maestro maestro)
    {
        string instruccionSQL = $@"select * from materias_grupos_horario
                                   inner join
                                   materias_grupo on claveMateriaHorario = claveMateriaGrupo
                                   and grupoMateriaHorario = grupo
                                   inner join
                                   maestros on noMaestroGrupo = noMaestro
                                   where
                                   noMaestro = @noMaestro;";

        MySqlCommand consulta = ejecutarConsulta(instruccionSQL);
        asignarParametros(consulta, maestro);
        MySqlDataReader reader = consulta.ExecuteReader();
        reader.Read();
        return reader;
    }

public static void asignarParametros(MySqlCommand consulta, Maestro maestro)
    {
        consulta.Parameters.AddWithValue("@noMaestro", maestro.NoMaestro);
        consulta.Parameters.AddWithValue("@nombreMaestro", maestro.NombreMaestro);
    }

 public static MySqlCommand ejecutarConsulta(string sentenciaSQL)
    {
        using (MySqlCommand consulta = new MySqlCommand(sentenciaSQL, ConexionBDSingleton.Conexion))
        {
            ConexionBDSingleton.Conexion.ClearAllPoolsAsync();
            return consulta;
        }
    }

And the connection I'm making using a singleton:

class ConexionBDSingleton
{
    private static MySqlConnection instancia = null;
    private static readonly object padlock = new object();

    private ConexionBDSingleton() { }

    public static MySqlConnection Conexion
    {
        get
        {
            lock (padlock)
            {
                if (instancia == null)
                {
                    MySqlConnection conexion = new MySqlConnection(ConfigurationManager.ConnectionStrings["IngenieriaDB"].ToString());

                    conexion.Open();
                    return conexion;
                }
                return instancia;
            }
        }
    }
}

Thank you.

    
asked by Nohemi 22.11.2018 в 21:51
source

1 answer

0

The error IndexOutOfRangeException is that in your iteration at some point you exceed the limits of the matrix.

My suggestion is that doing a foreach will make your code cleaner:

foreach (DataRow row in tabla1.Rows)
    Console.WriteLine($"{row["dia"].ToString()} {row["hora"].ToString()}");

The control structure foreach serves to move through the elements of a data structure, such as a vector, and perform actions for each of the elements already defined, so you do not need to indicate the index within the matrix.

    
answered by 22.11.2018 в 21:59