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 NroVendedor=@NroVendedor";
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!