Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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.
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.
 
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 28-Nov-12 19:44pm
Edited 28-Nov-12 19:57pm
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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,
if (((TextBox)rptItem.FindControl(acheivedTxtContolID)) != null)
best luck.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

if (((TextBox)rptItem.FindControl(acheivedTxtContolID)) != null)
 {
    TextBox txtAchivements = (TextBox)rptItem.FindControl(acheivedTxtContolID);
 }
  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 389
1 Sergey Alexandrovich Kryukov 375
2 Maciej Los 295
3 King_Fisher 160
4 Garth J Lancaster 158
0 Sergey Alexandrovich Kryukov 11,069
1 OriginalGriff 5,743
2 Maciej Los 5,649
3 CPallini 3,879
4 BillWoodruff 3,584


Advertise | Privacy | Mobile
Web02 | 2.8.141030.1 | Last Updated 29 Nov 2012
Copyright © CodeProject, 1999-2014
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