Error in foreach

5

As I recently started programming in .NET C # I still do not have much idea of how it works ...

I have a table with 7 records in a database. But when I do a SELECT of that table from my class, it only takes the values of the last record ...

This is the code of the method to retrieve the data from the table:

    public static List<Facturas> BuscarFacturas(int NroVendedor)
    {
        int NroVendedor = 5;

        List<Facturas> Listafacturas = new List<Facturas>();
        Facturas factura = new Facturas();

        string ConnStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
        using (SqlConnection connection = new SqlConnection(ConnStr))
        {
            Dictionary<string, object> parameters = new Dictionary<string, object>();

            string queryDB = "SELECT * FROM Facturas WHERE [email protected]";
            parameters.Add("@NroVendedor", NroVendedor);

            DataTable dtDB = DefineBD.dba_DataTable_ParamHash(queryDB, parameters);

            foreach (DataRow fac in dtDB.Rows)
            {
                factura.NroFactura = fac.Field<int>("NroFactura");
                factura.Concepto = fac.Field<String>("Concepto");
                factura.Importe = fac.Field<decimal>("Importe");
                factura.Fecha = fac.Field<DateTime>("Fecha");
                factura.NroCliente = fac.Field<int>("NroCliente");
                factura.NroVendedor = fac.Field<int>("NroVendedor");

                Listafacturas.Add(factura);
            }
        }

        return Listafacturas;
    }

What I want to do is that this method returns a list of the Invoices table but what it returns is a list with the last record ...

This is what it returns:

  

  7   Product 7   5   2017-03-30T00: 00: 00   3   5   

     

  7   Product 7   5   2017-03-30T00: 00: 00   3   5   

     

  7   Product 7   5   2017-03-30T00: 00: 00   3   5   

     

  7   Product 7   5   2017-03-30T00: 00: 00   3   5   

     

  7   Product 7   5   2017-03-30T00: 00: 00   3   5   

     

  7   Product 7   5   2017-03-30T00: 00: 00   3   5   

     

  7   Product 7   5   2017-03-30T00: 00: 00   3   5   

And this is what I should return:

  

  one   Product 1   25   2017-03-30T00: 00: 00   one   5   

     

  two   Product 2   32   2017-03-30T00: 00: 00   one   5   

     

  3   Product 3   fifteen   2017-03-30T00: 00: 00   two   5   

     

  4   Product 4   500   2017-03-30T00: 00: 00   two   5   

     

  5   Product 5   200   2017-03-30T00: 00: 00   two   5   

     

  6   Product 6   10   2017-03-30T00: 00: 00   3   5   

     

  7   Product 7   5   2017-03-30T00: 00: 00   3   5   

I can not find where the error is, could someone help me?

Thank you very much in advance!

    
asked by Matias 31.03.2017 в 16:55
source

2 answers

9

The problem is that within the cycle, you are always overwriting the same instance of Facturas and adding that same instance to the list several times. So in the end, your list simply contains several references to the same instance of Facturas with the data you overwrote last time.

To correct your error, you must create a new instance of Facturas at the beginning of each cycle.

So remove this statement:

Facturas factura = new Facturas();

... and move it within the cycle in this way:

foreach (DataRow fac in dtDB.Rows)
{
    Facturas factura = new Facturas(); // <-- mueve esta sentencia dentro del ciclo.

    factura.NroFactura = fac.Field<int>("NroFactura");
    factura.Concepto = fac.Field<String>("Concepto");
    factura.Importe = fac.Field<decimal>("Importe");
    factura.Fecha = fac.Field<DateTime>("Fecha");
    factura.NroCliente = fac.Field<int>("NroCliente");
    factura.NroVendedor = fac.Field<int>("NroVendedor");

    Listafacturas.Add(factura);
}
    
answered by 31.03.2017 / 16:59
source
6

One solution that you can try (and that I personally consider more elegant) is to modify your cycle (and not even declare your object factura ) with the following cycle:

foreach (DataRow fac in dtDB.Rows)
{
    Listafacturas.Add(new Facturas()
    {
        NroFactura = fac.Field<int>("NroFactura"),
        Concepto = fac.Field<String>("Concepto"),
        Importe = fac.Field<decimal>("Importe"),
        Fecha = fac.Field<DateTime>("Fecha"),
        NroCliente = fac.Field<int>("NroCliente"),
        NroVendedor = fac.Field<int>("NroVendedor")
    });
}
    
answered by 31.03.2017 в 17:20