We help IT Professionals succeed at work.

asp.net vb response.writefile NullReferenceException: Object reference not set to an instance of an object.

642 Views
Last Modified: 2014-09-04
Hello,
I'm using response.writefile("thisfile.inc") on an .aspx web page.  
"thisfile.inc" contains a table and a bunch of labels like this: <asp:Label ID="label1" runat="server" Text="" />
On page load, after I've retrieved the data, I'm trying to put text in those labels.

Coming up with error:
Object reference not set to an instance of an object.
NullReferenceException: Object reference not set to an instance of an object.

Here the code behind
Dim lbl1 As Label
lbl1 = DirectCast(FindControl("label1"), Label)
lbl1.Text = Row("xNames").ToString

I'm creating several reports that the some of the same pages.  
Each report has a big long SQL string call that pulls data from 7 different tables.  All the reports don't need the same data.

Instead of duplicating coding to create the common pages on the report, I'm trying to include them.  
This way I can pull 1 set of data with 1 call and not several for each report.
I tried creating usercontrols but the same problem happens.

I read someplace where you can't use findcontrol on a label.  Is this true?  Is there an alternative?

Is there a better way to do this?

Jess
Comment
Watch Question

Author

Commented:
Thanks Scott
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Jaime OlivaresSoftware Architect
CERTIFIED EXPERT
Top Expert 2008

Commented:
You need a recursive FindControl function. Have a look at the answer of this question: https://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_24994181.html
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Based on Kaufed's answer - I found another solution to the problem.  
Create usercontrols for the individual pages
Create main report that calls all the data once and uses the usercontrol pages as needed.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.