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

Posted on 2014-08-27
Last Modified: 2014-09-04
I'm using response.writefile("") on an .aspx web page.  
"" 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
    LVL 1

    Author Comment

    Thanks Scott
    LVL 74

    Accepted Solution

    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
    You need a recursive FindControl function. Have a look at the answer of this question:
    LVL 1

    Assisted Solution

    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!
    LVL 1

    Author Closing Comment

    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

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    New Relic: Our company recently started researching several products to figure out what were the best ways for us to increase our web page speed and to quickly identify performance problems that we may be having. One of the products we evaluated wa…
    Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
    This video teaches users how to migrate an existing Wordpress website to a new domain.
    Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now