Click here to Skip to main content
15,884,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

From Control.FindControl documentation[^]:

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



 
Share this answer
 
Check the textbox variable for null before using it:

C#
TextBox rateCenterName = (TextBox)row.FindControl("txtRateCenterName");
cmd.Text.Parameters.Add("@RateCenterName", OleDbType.VarChar).Value = (rateCenterName != null) ? rateCenterName.Text : "";
 
Share this answer
 
textbox's name in it's properties and in the code was different
 
Share this answer
 
C#
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]
 
Share this answer
 

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


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900