Click here to Skip to main content
15,921,113 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Can anyone know?

Input string was not in a correct format.how to solve?

C#
protected void totalprice()
    {

       int quantity = 0;
     int price = 0;
int rowTotal = 0;
int subTotal = 0;
int totalprice = 0;
foreach(GridViewRow row in GridView1.Rows)
{
   quantity = Convert.ToInt32(row.Cells[3].Text); //replace 0 with row cell index of qty
   price = Convert.ToInt32(row.Cells[4].Text); //replace 1 with row cell index of amount
   rowTotal = quantity * price;
   subTotal += rowTotal;
}
totalprice = subTotal;
lbltprice.Text = totalprice.ToString();
    }
Posted
Comments
Prince Antony G 3-Dec-11 0:59am    
which line of code gives error..Dont repost Question again and again...
sathiyak 3-Dec-11 1:04am    
quantity = Convert.ToInt32(row.Cells[3].Text); //here

1 solution

Instead of Convert.ToInt32, try int.TryParse[^] - if it fails, you can do something sensible with the result yourself, instead of getting an exception:
C#
foreach(GridViewRow row in GridView1.Rows)
{
   if (!int.TryParse(row.Cells[3].Text, out quantity))
   {
      quantity = 0; // Or report the error to the user and abandon the job
   }
   ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900