Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can Anyone Rectify Below Error:


i D't know how to rectify please rectify...



Error:

Object reference not set to an instance of an object.


Error Coding:

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

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

This is a generic error throws by .NET. it occours due to some NULL object.
in above code which line gives you erro ?
 
Share this answer
 
See Solution 1 my friend.

Object reference not set to an instance of an object.[^]

Regards,
Eduard
 
Share this answer
 
Both your .findControl() methods gives you NULL result. please check your code at that point.
 
Share this answer
 
It occurs when an object refers null. Do u Know on which line it throwing that exception. Check by debugging in these 3 lines.......
C#
int pro_id = Convert.ToInt32(GridView1.DataKeys[row.RowIndex][0]);
          int quantity = Convert.ToInt32((row.FindControl("txtquantity") as TextBox).Text);// Error
          float price = float.Parse((row.FindControl("price")).ToString());


1.If it is in first line then row index is wrong or it returns null value.

2.If it is in second line then txtquantity is not availabe. Remember it is case sensitive.

3.If it is in third line then price is not availabe. Remember it is case sensitive.
 
Share this answer
 
float price = float.Parse((row.FindControl("price")).ToString());

first u should get the reference of the control then from that reference call the values of that control
 
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