I'll be brief, I'm making a simple C# desktop application for grocery store. And here, in my problem, The scenario is that:
if the user mentions the quantity that is less than the quantity in the stock, the application performs computations; and if user asks for the quantity that is more than the quantity in the current stock, so the program notifies the user that this is the case and do u still want to continue? if Yes: application performs same computations; and if NO, the application don't performs and clears the textboxes.
all of the scenario works fine except the YES block. if says System.NullReferenceException: Object reference not set to an instance of an object. And refers to the Line: 708. (I have pasted the code below). This line is exact same copy as in the IF block. But it works fine there; not here. I wonder why.
if (data.Rows.Count > 0)
{
quantity = Convert.ToInt32(data.Rows[0]["item_stock_qty"]);
}
if (quantity>=Convert.ToInt32(txtBoxQuantity.Text))
{
AddDataToGridView1((++srNo).ToString(), comboBoxItems.SelectedItem.ToString(),
txtBoxUnitPrice.Text.ToString(), txtBoxDiscountPerItem.Text.ToString(),
txtBoxQuantity.Text.ToString(), txtBoxSubTotal.Text.ToString(),
txtBoxTax.Text.ToString(), txtBoxTotalCost.Text.ToString());
ResetControls();
CalculateFinalCost();
}
else
{
DialogResult dialogResult= MessageBox.Show("The remaining stock of this item is "+quantity+
" item/s. Do you still want to Continue?", "Low Stock", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if(dialogResult== DialogResult.Yes)
{
AddDataToGridView1((++srNo).ToString(), comboBoxItems.SelectedItem.ToString(),
txtBoxUnitPrice.Text.ToString(), txtBoxDiscountPerItem.Text.ToString(),
txtBoxQuantity.Text.ToString(), txtBoxSubTotal.Text.ToString(),
txtBoxTax.Text.ToString(), txtBoxTotalCost.Text.ToString());
ResetControls();
CalculateFinalCost();
}
else if(dialogResult== DialogResult.No)
{
ResetControls();
}
else { }
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
What I have tried:
I have been trying to solve this problem sleeplessly but haven't been able to do so. please help. Thank you