Greetings to all Forum members.
I am designing a sales database in C# Windows Form Application where I need to create a stored procedure to link the stock and sales tables to each other.
In the project I have textboxes named “grossStockQty” from the stock table, “grossSalesQty” from the sales table and “AvailableQty” stock table, respectfully.
I used the following code to sum up record from the “stockQty” in the stock table into the “grossStockQty” textbox:-
grossStockQtyTextBox.Text = (from DataGridViewRow row in stockDataGridView.Rows where row.Cells[7].FormattedValue.ToString() != string.Empty
select Convert.ToDecimal(row.Cells[7].FormattedValue)).Sum().ToString();
I also used the following code to sum up record from the “salesQty” in the sales table into the “grossSalesQty” textbox:-
grossSalesQtyTextBox.Text = (from DataGridViewRow row in salesDataGridView.Rows where row.Cells[17].FormattedValue.ToString() != string.Empty
select Convert.ToDecimal(row.Cells[17].FormattedValue)).Sum().ToString();
I build the AvailableQty stored procedure on the database server side but included it in the dataset from the project DataSource. I drew the stored procedure as controls on the Windows Form UI to create the interface to operate on the database server.
I closely observed that the only way (I don't know if there are different other ways) I can pull data from the two table on one interface is by using the stored procedure. I have successfully achieved that, through which I can pull data from the source.
Now, what I want to do is after pulling the data onto the form UI I can use a button to manipulate the data into the availableQty textbox control. All I need is a code in the button to subtract the gross sales qty from the gross stock qty to display the difference in the availableQty textbox.
Thanks


Amit MohantyRank #112 · 20K reputationPosted Jun 17, 2024, 1:29 AM EDT
Create an event handler for the button that will perform the subtraction and display the result in the AvailableQty textbox.
private void calculateAvailableQtyButton_Click(object sender, EventArgs e) { try { decimal grossStockQty = Convert.ToDecimal(grossStockQtyTextBox.Text); decimal grossSalesQty = Convert.ToDecimal(grossSalesQtyTextBox.Text); decimal availableQty = grossStockQty - grossSalesQty; availableQtyTextBox.Text = availableQty.ToString(); } catch (FormatException) { MessageBox.Show("Please ensure that the gross stock and sales quantities are valid numbers.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception ex) { MessageBox.Show("An unexpected error occurred: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }