Click here to Skip to main content
15,921,226 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to solve tgis error in below coding?


Object reference not set to an instance of an object.


C#
public void totalprice()
   {
       int quantity;
       float price;
       float rowtotal ;
       float subtotal=0;
       float totalprice ;
       foreach (GridViewRow row in GridView1.Rows)
       {
           //int pro_id = Convert.ToInt32(GridView1.DataKeys[row.RowIndex][0]);
           quantity = Convert.ToInt32((row.FindControl("quantity")).ToString());
           price = float.Parse((row.FindControl("price")).ToString());
           rowtotal = quantity * price;
           subtotal += rowtotal;
       }

       totalprice = subtotal;
       lbltprice.Text = totalprice.ToString();
   }
Posted

1 solution

This means that you are getting null value in the ff lines:

C#
quantity = Convert.ToInt32((row.FindControl("quantity")).ToString());
price = float.Parse((row.FindControl("price")).ToString());


Use Int32.TryParse or Float.TryParse method instead. This is safer than directly converting a value into a desired output.

Regards,
Eduard
 
Share this answer
 
Comments
sathiyak 5-Dec-11 2:10am    
Error 1 No overload for method 'TryParse' takes '1' arguments..........it comes
[no name] 5-Dec-11 2:13am    
Of course you need to overload that method

int qty;
Int32.TryParse(row.FindControl("quantity", out qty);

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