.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

Object reference not set to an instance of an object.

Posted By: Narmatha     Posted Date: December 04, 2011    Points:5   Category :ASP.Net
Can Anyone Rectify this Error:


Object reference not set to an instance of an object.

Error Coding:

public void totalprice()
int quantity = 0;
float price = 0;
float rowtotal = 0;
float subtotal = 0;
float totalprice = 0;
foreach (GridViewRow row in GridView1.Rows)
//price = float.Parse((row.Cells[2].FindControl("price")).ToString());
//quantity = Convert.ToInt32(row.Cells[3].Text);
quantity = Convert.ToInt32(((TextBox)row.Cells[3].FindControl("txtquantity")).Text); // Error
//quantity = Convert.ToInt32((((TextBox)row.FindControl("txtquantity")).Text));
price = float.Parse(((row.FindControl("price")).ToString())); // Error
rowtotal = quantity * price;
subtotal += rowtotal;
totalprice = subtotal;
lbltprice.Text = totalprice.ToString();

Author: priya             
Posted Date: December 05, 2011     Points: 20   

Which line are you getting error?

like this

if(((TextBox)row.Cells[3].FindControl("txtquantity")) != null)
// your logic

Its always goof to check the object to null or not.

Author: Sasi Prabhu             
Posted Date: December 06, 2011     Points: 20   


quantity = Convert.ToInt32(((TextBox)row.Cells[3].FindControl("txtquantity")).Text);
price = float.Parse(((row.FindControl("price")).ToString()));

Check the txtquantity and price value shoul not be null.
Pass some values for txtquantity and price.

Thanks & Regards,
Sasi Prabhu.

Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend