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

Posted on 2014-08-27
Medium Priority
Last Modified: 2014-09-04
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?

Question by:jshesek
  • 3

Author Comment

ID: 40288194
Thanks Scott
LVL 75

Accepted Solution

käµfm³d   👽 earned 2000 total points
ID: 40289556
Controls in ASP.NET WebForms are placed into various containers on your page. The page itself is also a container. If you call the page's FindControl method, then it is expected that the control you seek is directly on the page itself--i.e. not in a nested container. If your control actually resides within a nested container (e.g. Panel, GroupBox, etc.) on the page, then you have to call FindControl on the container itself. Check to make sure that you are calling FindControl against the correct container.
LVL 55

Expert Comment

by:Jaime Olivares
ID: 40289578
You need a recursive FindControl function. Have a look at the answer of this question: http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_24994181.html

Assisted Solution

jshesek earned 0 total points
ID: 40290869
This is what's working.

Pull all the data, from SQL, for the entire report.  (From the main report page)
 Store the data in a table and save it in the Session.
 Create a User Control for each section
 Pull the info from the Session table for the User Control labels

Good thing is there isn't a lot of SQL calls going back and forth.  And I can reuse the Page sections as need without much work.  It generates pretty quick

Bad thing is sometimes it's difficult to get all the data into 1 SQL string.  But practice makes perfect!

Author Closing Comment

ID: 40302978
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.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question