0
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.

1
  • 2
    What is the value of lblTotalSaleAmount.Text exactly and what Culture you are using? Commented Jan 13, 2014 at 9:31

1 Answer 1

2

It seems your text is not always formatted correctly, use :

int intTotalSaleAmount = 0;

if (int.TryParse(lblTotalSaleAmount.Text, out intTotalSaleAmount))
{
}
else
{
    // handle error
}
Sign up to request clarification or add additional context in comments.

2 Comments

Although this is a solution to the exception it is not a solution to the real problem.
Side note - you don't need to initialize out variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.