Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can Anyone rectify this error..for this wht i do


Error:


Object reference not set to an instance of an object.


Error coding:


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







please anyone rectify this error................
Posted
Updated 2-Dec-11 20:30pm
v2
Comments
uspatel 3-Dec-11 1:08am    
where you call totalprice() method?
M.Narmatha 3-Dec-11 1:09am    
in pageload()
[no name] 3-Dec-11 21:55pm    
You must be having a null value in your gridview

Same exact stuff again and again (sigh)…You FindControl returned null as a control was not found. Exception is thrown at the attempt to read the property Text on null object.

Now, your approach is very wrong in tree aspects. First, why asking a question like that without trying to run it under Debugger. You should always do that. It would make it all clear. Do not develop anything before you are comfortable with the Debugger and readily use it on any smallest problem.

Second problem is: FindControl is not intended to be used with regular UI applications. You should not use Name property at all; this is all just for Designer. Why searching for anything is all your controls are declared in your Form or Window class and accessible via some class members? It makes no sense; and using this method is a major flaw.

Third: never use immediate constants, especially like "txtquantify" (wannabe Name :-)). You destroy all your maintenance. If you misspell it, a computer would not detect a problem. Besides, what it you need to use it in two or more different places? (It actually happens because it also appears in the auto-generated code.) Never ever repeat anything. Do I have to explain why? Here: http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[^].

—SA
 
Share this answer
 
v2
Comments
S.P.Tiwari 3-Dec-11 1:45am    
really this is a very small stuf.
M.Narmatha 3-Dec-11 1:47am    
wht is it..
Uday P.Singh 3-Dec-11 2:32am    
Excellent answer :)
Sergey Alexandrovich Kryukov 3-Dec-11 21:31pm    
Thank you, Uday.
--SA
thatraja 3-Dec-11 14:45pm    
Right, 5!
Dear Narmatha,

you may getting this error because, "txtquantity" and "price" textboxes may not be a part of all the rows of the gridview but you are trying to access the textboxes in all the rows of the gridview. So, control is trying to find a control which is not at all existing.
So, Check the templates in gridview in which you have specified your controls and check whether the controls IDs are correctly spelled.

later if any issues, just put it across.
 
Share this answer
 
Comments
M.Narmatha 3-Dec-11 1:13am    
price is not a label its a templatefield what can i do..
S.P.Tiwari 3-Dec-11 1:29am    
its ok its a template field there should be a thing that contains price value.
it may be label,textbox,hidden field....

where is price stored...?
S.P.Tiwari 3-Dec-11 1:30am    
can you send me the .aspx page code.. where the price is..?
M.Narmatha 3-Dec-11 1:48am    
<asp:TemplateField HeaderText="price">
<itemtemplate>
<%#Eval("price")%>

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