Unbelievably annoying user control error

VBAHole22

Contributor
Joined
Oct 21, 2003
Messages
432
Location
VA
I have an ASP.NET page (VB) that has a user control banner at the top and a data grid that pulls from an Access table. What is happening to it makes no sense whatsoever to me because it is unpredictable (the kinds of things that drive programmers up the wall, an error I can deal with, but one that happens only once in a while is insane)

I add the user control by dragging it to the form.
I add a reference to it in my code behind under the 'designer' area as follows
Code:
 Protected WithEvents MyMiniBanner As MiniBanner

Then I set some Public properties that I have exposed on the control
Code:
  Me.MyMiniBanner.BannerText = "GO VCU Rams!"
            Me.MyMiniBanner.BannerVisible = True
            Me.MyMiniBanner.BannerStyle = "Banner1"

When I view the page in debug mode (F5) everything comes up, no problem. When I view that page through my browser, everything comes up, no problem. When I hit F5 to refresh I get 'object reference not set to the instance of an object on the first line that references MyMiniBanner. The page won't load. So I sandwiched that in a try block and sent the message to a text box. Now I get the message and the page loads but I don't get my data grid.

Why does it work sometimes and not others? What does that object ref have to do with my datagrid not loading?
This one is annoying!

Any suggestions at all greatly appreciated. I can post the user control code if it would help.
 
More info:

This happens everytime I try to refresh the page. I have tried moving the Property calls to the user control outside of the Not PostBack if and it doesn't make a difference.
Weird.
 
The plot thickens

From debugging I can see what is happening. When the page loads and I try to call my first property:
Code:
Me.MyMiniBanner.BannerText = "Go VCU Rams!"

It then takes me to my user control class code which is as follows:
Code:
Public MustInherit Class MiniBanner
    Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents HyperLink1 As System.Web.UI.WebControls.HyperLink
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label

    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        InitializeComponent()
    End Sub

#End Region
    Private _BannerText As String
    Private _BannerStyle As String
    Private _BannerVisible As Boolean

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub
    Public WriteOnly Property BannerText() As String
        Set(ByVal Value As String)
            _BannerText = Value
            Me.Label1.Text = _BannerText
        End Set
    End Property
    Public WriteOnly Property BannerStyle() As String
        Set(ByVal Value As String)
            _BannerStyle = Value
            Me.Label1.CssClass = _BannerStyle
        End Set
    End Property
    Public WriteOnly Property BannerVisible() As Boolean
        Set(ByVal Value As Boolean)
            _BannerVisible = Value
            Me.Label1.Visible = _BannerVisible
        End Set
    End Property
End Class

When it tries to assign the BannerText at the following line:
Me.Label1.Text = _BannerText

Label1 is set to Nothing and that is where I think I am getting the object reference error.
How do I get around this?
Why does it work the first time and then not on a refresh?
Strange indeed.
 
Yeah, I gave that a shot but no luck. I figured, as you prolly did, that it had something to do with a new object not be instantiated for the control. But then why would it work once and not on the refresh?
I'll give it another shot but i didn't think that was it.
But I do appreciate the suggestion, thank you
 
No dice. I added 3 of those statements in the page load for each of the properties I am trying to expose

Code:
Public MustInherit Class MiniBanner
    Inherits System.Web.UI.UserControl

    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Private _BannerText As String
    Private _BannerCssClass As String
    Private _BannerVisible As Boolean
  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Label1.Text = _BannerText
        Me.Label1.CssClass = _BannerCssClass
        Me.Label1.Visible = _BannerVisible
    End Sub
    Public WriteOnly Property BannerText() As String
        Set(ByVal Value As String)
            _BannerText = Value
            Me.Label1.Text = _BannerText
        End Set
    End Property
    Public WriteOnly Property BannerCssClass() As String
        Set(ByVal Value As String)
            _BannerCssClass = Value
            Me.Label1.CssClass = _BannerCssClass
        End Set
    End Property
    Public WriteOnly Property BannerVisible() As Boolean
        Set(ByVal Value As Boolean)
            _BannerVisible = Value
            Me.Label1.Visible = _BannerVisible
        End Set
    End Property
End Class

I still get the object reference not set. But only on a refresh. And with my try catch block the page loads but the data grid is gone. The data grid works fine though without the user control. Baffling.
 
Well, it isn't a solution but I found a work around.

Before I had the following on my page
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Try
            Me.MyMiniBanner.BannerText = "Current VAMLIS Members"
            Me.MyMiniBanner.BannerCssClass = "Banner1"
            Me.MyMiniBanner.BannerVisible = True

        If Not Page.IsPostBack Then

            viewstate("sortField") = "LastName"
            viewstate("sortDirection") = "ASC"
            BindGrid()
        End If
        Catch ex As Exception
            Me.txtErr.Text = ex.Message
        End Try
   End Sub

And it would bomb out on the first line after the try. This is why the grid was not getting filled because it couldn't get to BindGrid().

So I changed it to look like this
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'FOR THE IMAGE IN THE BANNER, IF YOU WANT ONE
        '  Me.MiniBanner1.strLinkURL = "../images/member_list.gif"
        '  Me.MiniBanner1.bLinkVisible = True
        Try
            Me.MyMiniBanner.BannerText = "Current VAMLIS Members"
            Me.MyMiniBanner.BannerCssClass = "Banner1"
            Me.MyMiniBanner.BannerVisible = True
        Catch ex As Exception
            Me.txtErr.Text = ex.Message
        End Try

        If Not Page.IsPostBack Then

            viewstate("sortField") = "LastName"
            viewstate("sortDirection") = "ASC"
            BindGrid()
        End If
     End Sub

Now the same error occurs but it gets handled and the message gets written out but the code continues. Still, why is this happening?
It's as if the user control object is not being permeated or not be reinitialized each time the page posts. Could it be a caching issue?
 
Back
Top