Click here to Skip to main content
15,905,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is the following code is correct for calculate totalprice?but i got error....

can anyone help?

C#
protected string TP()

   {
       float totalprice = 0;
       foreach (GridViewRow rowitem in GridView1.Rows)
       {
           Label price = (Label)(rowitem.Cells[4].FindControl("price"));
           TextBox txtquantity = rowitem.Cells[3].FindControl("quantity");
           float price = float.Parse(price.Text);
           int quantity = int.Parse(txtquantity.Text);
           totalprice = price * quantity;
       }

       lbltprice.Text = totalprice.ToString();


   }
Posted
Updated 1-Dec-11 21:56pm
v2
Comments
Prince Antony G 2-Dec-11 3:55am    
which error have you faced, please provide.
D K N T H 2-Dec-11 3:56am    
added code pre tag
D K N T H 2-Dec-11 3:58am    
what kind of error did you got?
sathiyak 2-Dec-11 3:58am    
Error 3 Cannot implicitly convert type 'float' to 'System.Web.UI.WebControls.Label'
this comes................
Prince Antony G 2-Dec-11 5:04am    
becz it cant take the value from label so only that error comes..If u check with breakpoint,u will identify that error.

try this,

C#
protected string TP()
 
    {
        float totalprice = 0;
        foreach (GridViewRow rowitem in GridView1.Rows)
        {
            Label price = (Label)(rowitem.Cells[4].FindControl("price"));
            TextBox txtquantity = rowitem.Cells[3].FindControl("quantity");
            float price = float.Parse(price.Text);
            int quantity = int.Parse(txtquantity.Text);
           totalprice += price * quantity;
        }
 
        lbltprice.Text = totalprice.ToString();
 

    }




hope it helps

thanks
 
Share this answer
 
Comments
sathiyak 2-Dec-11 4:06am    
again same error only.............
There are 2 variables with same name price, so rename any one
C#
protected string TP()

   {
       float totalprice = 0;
       foreach (GridViewRow rowitem in GridView1.Rows)
       {
           Label price1 = (Label)(rowitem.Cells[4].FindControl("price"));//price1
           TextBox txtquantity = rowitem.Cells[3].FindControl("quantity");
           float price = float.Parse(price1.Text);//price1
           int quantity = int.Parse(txtquantity.Text);
           totalprice = price * quantity;
       }

       lbltprice.Text = totalprice.ToString();


   }
 
Share this answer
 
Comments
sathiyak 2-Dec-11 4:25am    
again same err only.....
thatraja 2-Dec-11 4:27am    
Can you able to compile the code?
Try this
1.Change the Variable name price to price1.
2.Cells[4] and Cells[3] changed into Cells[0].

For myself I got the Output.
C#
protected void  TP()
   {
       float totalprice = 0;
       foreach (GridViewRow rowitem in GridView1.Rows)
       {
           Label price1 = (Label)(rowitem.Cells[0].FindControl("price"));
           TextBox txtquantity =(TextBox) (rowitem.Cells[0].FindControl("quantity"));
           float price = float.Parse(price1.Text);
           int quantity = int.Parse(txtquantity.Text);
           totalprice = price * quantity;
       }

       lbltprice.Text = totalprice.ToString();


   }
 
Share this answer
 
v2

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