Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 136:
Line 137: //currentTitleOfCourtesy = ((TextBox)r.FindControl("TitleOfCourtesyTextBox")).Text;
Line 138: currentLastName = ((TextBox)r.FindControl("Name")).Text;
Line 139: currentFirstName = ((TextBox)r.FindControl("Location")).Text;
Line 140: //currentTitle = ((TextBox)r.FindControl("TitleTextBox")).Text;


I got Error Near The line

currentLastName = ((TextBox)r.FindControl("Name")).Text;

Thanks for your advance replay
Posted
Comments
Sergey Alexandrovich Kryukov 25-Nov-11 11:46am    
Why using FindControl at all? Such an abuse...
--SA

You don't have a control with ID "Name" on your r control, whatever this r is. Ensure you have TextBox with ID="Name" on r and, if you do, view page source in the browser - ID could change if you use master pages
 
Share this answer
 
hi....

here I think if You are trying to set text before binding your control that may give that error

just try this code after binding your control.


C#
int i = 0;
        foreach (GridViewRow row in GridView1.Rows)
        {
                ((TextBox)row.FindControl("TextBox1")).Text = "Hello";
         

            i++;
        }


This will work perfectly
 
Share this answer
 
Comments
2011999 25-Nov-11 11:44am    
not working for this code
Using FindControl is generally bad and unreliable.

What happens if you misspell a name? A compiler won't find any errors. In a normally designed application you never need to find a control, because you have all the variables in your form classes. At least check up found control for null before using the found reference. You problem is just an attempt to operate on found control when its reference is null.

—SA
 
Share this answer
 
v2
Comments
_Zorro_ 25-Nov-11 12:27pm    
Just out of curiosity. How do you manage to retrieve controls inside a FormView without using FindControl() for example ?
Well, First tell us what is r? Based on that it would be easier to rectify, if you are making valid use of FindControl or not.

If I assume you are making valid use of FindControl
then you may put a condition to check null as below.
C#
if(r != null && r.FindControl("Name") != null)
{
  currentLastName = ((TextBox)r.FindControl("Name")).Text;
}
 
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