Dear Forum,
I am developing a sales app project from two related tables (i.e., Stocks and Sales)
I am having a GrossQty column that sums up records in the Qty column and GrossQtySold column sums up records in the QtySold column. Now, I want to subtract the GrossQtySold from the QtySold into the QtyAvailable column in the stock table.
The GrossQty and the QtyAvailable are in the Stocks table while the GrossQtySold is in the Sales table.
These are the codes I used to sum up records in both the tables: -
private void btnGrossCalcu_Click(object sender, EventArgs e)
{
grossQtySoldTextBox.Text = (from DataGridViewRow row in drugSalesDataGridView.Rows
where row.Cells[5].FormattedValue.ToString() != string.Empty
select Convert.ToDecimal(row.Cells[5].FormattedValue)).Sum().ToString();
}
private void btnGross_Click(object sender, EventArgs e)
{
grossQtyTextBox.Text = (from DataGridViewRow row in drugStockDataGridView.Rows
where row.Cells[6].FormattedValue.ToString() != string.Empty
select Convert.ToDecimal(row.Cells[6].FormattedValue)).Sum().ToString();
}I will appreciate if someone would help me to achieve my goal successfully
Thanks


Amit MohantyRank #112 · 20K reputationPosted Nov 17, 2023, 1:02 AM EST
Could you please specify whether the application in question is designed for Windows or if it's a web-based application? Additionally, how does this application interact with the database? Does it utilize a database context or any other methods for database interaction? Providing more information about the project will help others gain a better understanding of the problem or context.
David GodisTei21 reputationPosted Nov 17, 2023, 3:34 PM EST
Hi Amit,
The project is for desktop windows form, designed in Visual Studio 2019, connected to Microsoft SQL Server 2016, using system.data.sqlclient and configuration manager. There is a successful connection between the application and the server. I have also almost finished the development, except a code to extract record from one table to calculate records in the other table. I am really stucked. I wish to insert the codes from the two forms here for studying of the problem:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Printing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace DrugStoresApp { public partial class Inventory : Form { int qty; int qtyavailable; int grossqty; int grossqtysold; decimal unitcost, totalcost; private System.IO.Stream streamToPrint; string streamType; public Inventory() { InitializeComponent(); } private void drugStockBindingNavigatorSaveItem_Click(object sender, EventArgs e) { this.Validate(); this.drugStockBindingSource.EndEdit(); this.tableAdapterManager.UpdateAll(this.drugsDataSet); } private void Inventory_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'drugsDataSet.DrugSales' table. You can move, or remove it, as needed. this.drugSalesTableAdapter.Fill(this.drugsDataSet.DrugSales); // TODO: This line of code loads data into the 'drugsDataSet.DrugStock' table. You can move, or remove it, as needed. this.drugStockTableAdapter.Fill(this.drugsDataSet.DrugStock); for (int i = 2020; i <= DateTime.Now.Year; i++) { yearComboBox.Items.Add(i.ToString()); } for (int i = 1001; i <= 9009; i++) { stockIDComboBox.Items.Add(i.ToString()); } } private void qtyTextBox_KeyUp(object sender, KeyEventArgs e) { int.TryParse(qtyTextBox.Text, out qty); qtyTextBox.Text = qty.ToString(); totalcost = qty * unitcost; totalCostTextBox.Text = totalcost.ToString(); qtyavailable = grossqty - grossqtysold; qtyAvailableTextBox.Text = qtyavailable.ToString(); } private void unitPriceTextBox_KeyUp(object sender, KeyEventArgs e) { decimal.TryParse(unitPriceTextBox.Text, out unitcost); unitPriceTextBox.Text = unitcost.ToString(); totalcost = qty * unitcost; totalCostTextBox.Text = totalcost.ToString(); qtyavailable = grossqty - grossqtysold; qtyAvailableTextBox.Text = qtyavailable.ToString(); } private void btnGross_Click(object sender, EventArgs e) { grossQtyTextBox.Text = (from DataGridViewRow row in drugStockDataGridView.Rows where row.Cells[6].FormattedValue.ToString() != string.Empty select Convert.ToDecimal(row.Cells[6].FormattedValue)).Sum().ToString(); grossCostTextBox.Text = (from DataGridViewRow row in drugStockDataGridView.Rows where row.Cells[8].FormattedValue.ToString() != string.Empty select Convert.ToDecimal(row.Cells[8].FormattedValue)).Sum().ToString(); } using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Drawing; using System.Drawing.Printing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace DrugStoresApp { public partial class DrugSales : Form { int qtysold; decimal unitsale, totalsales; private System.IO.Stream streamToPrint; string streamType; public DrugSales() { InitializeComponent(); } SqlConnection con = new SqlConnection(); SqlDataAdapter da = new SqlDataAdapter(); SqlCommand cmd = new SqlCommand(); DataTable dt = new DataTable(); private void drugSalesBindingNavigatorSaveItem_Click(object sender, EventArgs e) { this.Validate(); this.drugSalesBindingSource.EndEdit(); this.tableAdapterManager.UpdateAll(this.drugsDataSet); } private void DrugSales_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'drugsDataSet.DrugSales' table. You can move, or remove it, as needed. this.drugSalesTableAdapter.Fill(this.drugsDataSet.DrugSales); for(int i = 2020; i <= DateTime.Now.Year; i++) { yearComboBox.Items.Add(i.ToString()); } for(int i = 1001; i <= 9009; i++) { stockIDComboBox.Items.Add(i.ToString()); } FillAllRecords(); } private void qtySoldTextBox_KeyUp(object sender, KeyEventArgs e) { int.TryParse(qtySoldTextBox.Text, out qtysold); qtySoldTextBox.Text = qtysold.ToString(); totalsales = qtysold * unitsale; totalSalesTextBox.Text = totalsales.ToString(); } private void unitSaleTextBox_KeyUp(object sender, KeyEventArgs e) { decimal.TryParse(unitSaleTextBox.Text, out unitsale); unitSaleTextBox.Text = unitsale.ToString(); totalsales = qtysold * unitsale; totalSalesTextBox.Text = totalsales.ToString(); } private void btnGrossCalcu_Click(object sender, EventArgs e) { grossQtySoldTextBox.Text = (from DataGridViewRow row in drugSalesDataGridView.Rows where row.Cells[5].FormattedValue.ToString() != string.Empty select Convert.ToDecimal(row.Cells[5].FormattedValue)).Sum().ToString(); grossSalesTextBox.Text = (from DataGridViewRow row in drugSalesDataGridView.Rows where row.Cells[7].FormattedValue.ToString() != string.Empty select Convert.ToDecimal(row.Cells[7].FormattedValue)).Sum().ToString(); }I will appreciate a help to fix the problem.
Thanks