Click here to Skip to main content
13,087,566 members (93,299 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
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
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 24 Feb 2012
Copyright © CodeProject, 1999-2017
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