Click here to Skip to main content
11,580,580 members (69,269 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
I have the code below

It throws an error Object reference not set to an instance of object

TextBox rateCenterName = (TextBox)row.FindControl("txtRateCenterName");
 
cmd.Parameters.Add("@RateCenterName", OleDbType.VarChar).Value = rateCenterName.Text;


kindly help me out

Thanks,
Arjun
Posted 24-Feb-12 2:32am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

From Control.FindControl documentation[^]:

Return Value
The specified control, or a null reference (Nothing in Visual Basic) if the specified control does not exist.



  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Check the textbox variable for null before using it:

TextBox rateCenterName = (TextBox)row.FindControl("txtRateCenterName");
cmd.Text.Parameters.Add("@RateCenterName", OleDbType.VarChar).Value = (rateCenterName != null) ? rateCenterName.Text : "";
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

textbox's name in it's properties and in the code was different
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

TextBox = (TextBox)row.FindControl("txtRateCenterName");
 if(rateCenterName !=null)
{
cmd.Parameters.Add("@RateCenterName", OleDbType.VarChar).Value =rateCenterName.Text;
}

check "txtRateCenterName" control is exist or not in "row"

if you are using GridView then search the TextBox control in
row.Cells[yourcellIndex]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 1,088
1 Sergey Alexandrovich Kryukov 750
2 Abhinav S 638
3 Suvendu Shekhar Giri 433
4 F-ES Sitecore 420


Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 24 Feb 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100