Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

this is the code i wrote to get the text value from a textbox which i created dynamically inside a repeater.i wrote the following code in the repeater item bound event.
C#
if (((TextBox)rptItem.FindControl(acheivedTxtContolID)).Text != null)
 {
    TextBox txtAchivements = (TextBox)rptItem.FindControl(acheivedTxtContolID);
 }                           

acheivedTxtContolID will give my textbox's controlId which wis unique.when i am debugging, the first line in the code is throwing error saying object reference not set to an instance of an object.that is when i am checking whether the textbox is null its giving the error.

C#
if (((TextBox)rptItem.FindControl(acheivedTxtContolID)).Text != null)

i am putting the textbox as null but the code should go out of the condition if the textbox is null.instead its throwing error.what may be the cause.Can anyone provide some solutions to overcome this error.
Posted
Updated 28-Nov-12 19:57pm
v3

Hi

In your case problem is when control could not find.

you need to check if control is not null then only check for the Text property.

like,
C#
if (((TextBox)rptItem.FindControl(acheivedTxtContolID)) != null)

best luck.
 
Share this answer
 
C#
if (((TextBox)rptItem.FindControl(acheivedTxtContolID)) != null)
 {
    TextBox txtAchivements = (TextBox)rptItem.FindControl(acheivedTxtContolID);
 }
 
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