Reputation: 5
private void btnFinalize_Click(object sender, EventArgs e)
{
if (dgvProducts.RowCount == 0)
{
DataAccess.InformUser("Please add some product to the Sale....");
return;
}
int intTotalSaleAmount = int.Parse(lblTotalSaleAmount.Text, System.Globalization.NumberStyles.AllowThousands);
string saleId;
saleId = Sales.Add(txtCustomerId.Text, dtpSaleDate.Value.ToString("mm/dd/yyyy"), txtDescription.Text, intTotalSaleAmount.ToString());
// now add all the sale details....
foreach (DataGridViewRow dr in dgvProducts.Rows)
{
string productId = Products.SelectByProductName("ProductId", dr.Cells[0].Value.ToString())[0].ToString();
string quantity = dr.Cells[1].Value.ToString();
string unitPrice = dr.Cells[2].Value.ToString();
Sales.AddDetails(saleId, productId, quantity, unitPrice);
}
DataAccess.InformUser("The sale has been finalized....");
dgvProducts.Rows.Clear();
cmbProducts_SelectedIndexChanged(sender, e);
lblTotalSaleAmount.Text = "0";
}
I'm making a software in C# using visual studio 2010. Everything is OK before executing the program but when I click on the Finalize Button, it shows me the specified error:
Input string was not in correct format
I'm sure that the error is in following line:
int intTotalSaleAmount = int.Parse(lblTotalSaleAmount.Text, System.Globalization.NumberStyles.AllowThousands);
I don't know how to fix it.
Upvotes: 0
Views: 114
Reputation: 30022
It seems your text is not always formatted correctly, use :
int intTotalSaleAmount = 0;
if (int.TryParse(lblTotalSaleAmount.Text, out intTotalSaleAmount))
{
}
else
{
// handle error
}
Upvotes: 2