public List<Invoice> GetClientInvoices(int clientId) { var con = new SqlConnection(_connectionString); var invoices = new List<Invoice>(); using (con) { con.Open(); const string sqlInvoices = "Select * from Invoices where ClientId=@cid"; var cmdInvoices = new SqlCommand(sqlInvoices, con); cmdInvoices.Parameters.AddWithValue("cid", clientId); var rdrInvoices = cmdInvoices.ExecuteReader(); using (rdrInvoices) { while (rdrInvoices.Read()) { var invoice = new Invoice { Date = Convert.ToDateTime(rdrInvoices["Date"]), IndexNumber = Convert.ToInt32(rdrInvoices["IndexNumber"]), TotalAmount = Convert.ToDouble(rdrInvoices["TotalAmount"]), NetAmount = Convert.ToDouble(rdrInvoices["NetAmount"]), VatAmount = Convert.ToDouble(rdrInvoices["VatAmount"]), IsPrinted = Convert.ToBoolean(rdrInvoices["IsPrinted"]), VatPercent = Convert.ToInt32(rdrInvoices["VatPercent"]), ReceiptDescription = rdrInvoices["ReceiptDescription"].ToString(), ReceiptType = 2 }; invoices.Add(invoice); } } foreach (var inv in invoices) { const string sqlSelectItems = "Select * from InvoiceItems where InvoiceId=@invoiceid"; var cmdSelectItems = new SqlCommand(sqlSelectItems, con); cmdSelectItems.Parameters.AddWithValue("@invoiceid", inv.IndexNumber); var rdrSelectItems = cmdSelectItems.ExecuteReader(); inv.InvoiceItems = new List<InvoiceItem>(); using (rdrSelectItems) { while (rdrSelectItems.Read()) { var item = new InvoiceItem { Description = rdrSelectItems["Description"].ToString(), Id = Convert.ToInt32(rdrSelectItems["Id"]), InvoiceId = Convert.ToInt32(rdrSelectItems["InvoiceId"]), Payment = Convert.ToInt32(rdrSelectItems["Payment"]), PaymentClientBehalf = Convert.ToDouble(rdrSelectItems["PaymentClientBehalf"]) }; inv.InvoiceItems.Add(item); } } } } return invoices; }
public Invoice GetInvoice(int invoiceId) { var receipt = new Invoice(); var con = new SqlConnection(_connectionString); using (con) { con.Open(); var sql = "Select * from Invoices where IndexNumber=@rid"; var cmd = new SqlCommand(sql, con); cmd.Parameters.AddWithValue("@rid", invoiceId); var rdr = cmd.ExecuteReader(); using (rdr) { while (rdr.Read()) { receipt.ClientId = Convert.ToInt32(rdr["ClientId"].ToString()); receipt.Date = Convert.ToDateTime(rdr["Date"].ToString()); receipt.IndexNumber = Convert.ToInt32(rdr["IndexNumber"]); receipt.TotalAmount = Convert.ToDouble(rdr["TotalAmount"]); receipt.NetAmount = Convert.ToDouble(rdr["NetAmount"]); receipt.VatAmount = Convert.ToDouble(rdr["VatAmount"]); receipt.VatPercent = Convert.ToInt32(rdr["VatPercent"]); receipt.IsPrinted = Convert.ToBoolean(rdr["IsPrinted"]); receipt.ReceiptDescription = rdr["ReceiptDescription"].ToString(); } } var sqlItems = "Select * from InvoiceItems where InvoiceId=@id"; var cmdItems = new SqlCommand(sqlItems, con); cmdItems.Parameters.AddWithValue("@id", invoiceId); var rdrItems = cmdItems.ExecuteReader(); var items = new List<InvoiceItem>(); using (rdrItems) { while (rdrItems.Read()) { items.Add(new InvoiceItem { Description = rdrItems["Description"].ToString(), Payment = Convert.ToDouble(rdrItems["Payment"]), PaymentClientBehalf = Convert.ToDouble(rdrItems["PaymentClientBehalf"]) }); } } receipt.InvoiceItems = items; } return receipt; }