Error while using "State Bags".

Discussion in 'ASP .Net' started by =?Utf-8?B?SHJpc2hpIFI=?=, Nov 16, 2004.

  1. Hi:

    The following ASP.NET code segment throws an "Object reference not set to an
    instance of an object" exception.
    ------------------
    Public Sub Page_Load(Source As Object, E As EventArgs)

    Dim viewCount As Integer

    If ViewState("viewCount").ToString() = "" Then
    viewCount = 1
    Else
    viewCount = CType(ViewState("viewCount"), Integer) + 1
    End If
    labelViews.Text = "Times page has been viewed: " & viewCount.ToString()

    ViewState("viewCount") = viewCount
    -------------
    Please help, i can find many references to this exception on MSDN but none
    seems to help.

    Thanks.
    =?Utf-8?B?SHJpc2hpIFI=?=, Nov 16, 2004
    #1
    1. Advertising

  2. Hi,

    Perhaps the Viewstate("viewCount") is null. Try this; (Syntax might be
    different, I'm not very fluent in VB)


    Public Sub Page_Load(Source As Object, E As EventArgs)

    Dim viewCount As Integer

    If ViewState("viewCount") = Nothing Then
    viewCount = 1
    Else
    viewCount = CType(ViewState("viewCount"), Integer) + 1
    End If
    labelViews.Text = "Times page has been viewed: " & viewCount.ToString()
    ViewState("viewCount") = viewCount


    I would also recommend to put

    ViewState("viewCount") = viewCount

    in the PreRender event instead of Page_Load, since this is the last event
    called before the page renders and which eventually will have the latest
    value of viewCount.


    Hope this helps,

    Ethem Azun


    "Hrishi R" wrote:

    > Hi:
    >
    > The following ASP.NET code segment throws an "Object reference not set to an
    > instance of an object" exception.
    > ------------------
    > Public Sub Page_Load(Source As Object, E As EventArgs)
    >
    > Dim viewCount As Integer
    >
    > If ViewState("viewCount").ToString() = "" Then
    > viewCount = 1
    > Else
    > viewCount = CType(ViewState("viewCount"), Integer) + 1
    > End If
    > labelViews.Text = "Times page has been viewed: " & viewCount.ToString()
    >
    > ViewState("viewCount") = viewCount
    > -------------
    > Please help, i can find many references to this exception on MSDN but none
    > seems to help.
    >
    > Thanks.
    =?Utf-8?B?RXRoZW0gQXp1bg==?=, Nov 16, 2004
    #2
    1. Advertising

  3. Thanks! That did work. The value was null.

    "Ethem Azun" wrote:

    >
    > Hi,
    >
    > Perhaps the Viewstate("viewCount") is null. Try this; (Syntax might be
    > different, I'm not very fluent in VB)
    >
    >
    > Public Sub Page_Load(Source As Object, E As EventArgs)
    >
    > Dim viewCount As Integer
    >
    > If ViewState("viewCount") = Nothing Then
    > viewCount = 1
    > Else
    > viewCount = CType(ViewState("viewCount"), Integer) + 1
    > End If
    > labelViews.Text = "Times page has been viewed: " & viewCount.ToString()
    > ViewState("viewCount") = viewCount
    >
    >
    > I would also recommend to put
    >
    > ViewState("viewCount") = viewCount
    >
    > in the PreRender event instead of Page_Load, since this is the last event
    > called before the page renders and which eventually will have the latest
    > value of viewCount.
    >
    >
    > Hope this helps,
    >
    > Ethem Azun
    >
    >
    > "Hrishi R" wrote:
    >
    > > Hi:
    > >
    > > The following ASP.NET code segment throws an "Object reference not set to an
    > > instance of an object" exception.
    > > ------------------
    > > Public Sub Page_Load(Source As Object, E As EventArgs)
    > >
    > > Dim viewCount As Integer
    > >
    > > If ViewState("viewCount").ToString() = "" Then
    > > viewCount = 1
    > > Else
    > > viewCount = CType(ViewState("viewCount"), Integer) + 1
    > > End If
    > > labelViews.Text = "Times page has been viewed: " & viewCount.ToString()
    > >
    > > ViewState("viewCount") = viewCount
    > > -------------
    > > Please help, i can find many references to this exception on MSDN but none
    > > seems to help.
    > >
    > > Thanks.
    =?Utf-8?B?SHJpc2hpIFI=?=, Nov 16, 2004
    #3
  4. By the way, ViewState is not the right place to have an application wide
    view counter on the page. ViewState is only defined per connection on the
    page level.

    I would recommend keeping this information either on application level, or
    on another persistent datasource like a database (especially if you have many
    pages that you want to show this info about.)

    Ethem




    "Hrishi R" wrote:

    > Thanks! That did work. The value was null.
    >
    > "Ethem Azun" wrote:
    >
    > >
    > > Hi,
    > >
    > > Perhaps the Viewstate("viewCount") is null. Try this; (Syntax might be
    > > different, I'm not very fluent in VB)
    > >
    > >
    > > Public Sub Page_Load(Source As Object, E As EventArgs)
    > >
    > > Dim viewCount As Integer
    > >
    > > If ViewState("viewCount") = Nothing Then
    > > viewCount = 1
    > > Else
    > > viewCount = CType(ViewState("viewCount"), Integer) + 1
    > > End If
    > > labelViews.Text = "Times page has been viewed: " & viewCount.ToString()
    > > ViewState("viewCount") = viewCount
    > >
    > >
    > > I would also recommend to put
    > >
    > > ViewState("viewCount") = viewCount
    > >
    > > in the PreRender event instead of Page_Load, since this is the last event
    > > called before the page renders and which eventually will have the latest
    > > value of viewCount.
    > >
    > >
    > > Hope this helps,
    > >
    > > Ethem Azun
    > >
    > >
    > > "Hrishi R" wrote:
    > >
    > > > Hi:
    > > >
    > > > The following ASP.NET code segment throws an "Object reference not set to an
    > > > instance of an object" exception.
    > > > ------------------
    > > > Public Sub Page_Load(Source As Object, E As EventArgs)
    > > >
    > > > Dim viewCount As Integer
    > > >
    > > > If ViewState("viewCount").ToString() = "" Then
    > > > viewCount = 1
    > > > Else
    > > > viewCount = CType(ViewState("viewCount"), Integer) + 1
    > > > End If
    > > > labelViews.Text = "Times page has been viewed: " & viewCount.ToString()
    > > >
    > > > ViewState("viewCount") = viewCount
    > > > -------------
    > > > Please help, i can find many references to this exception on MSDN but none
    > > > seems to help.
    > > >
    > > > Thanks.
    =?Utf-8?B?RXRoZW0gQXp1bg==?=, Nov 16, 2004
    #4
  5. Thanks:

    With this source code I was trying to understand State Bags. The actual
    purpose was not to implement a page counter.

    All that I did was to understand how a key could be added to the ViewState
    Property collection such that the value is persisted along with all other
    server controls on the page.


    "Ethem Azun" wrote:

    >
    > By the way, ViewState is not the right place to have an application wide
    > view counter on the page. ViewState is only defined per connection on the
    > page level.
    >
    > I would recommend keeping this information either on application level, or
    > on another persistent datasource like a database (especially if you have many
    > pages that you want to show this info about.)
    >
    > Ethem
    >
    >
    >
    >
    > "Hrishi R" wrote:
    >
    > > Thanks! That did work. The value was null.
    > >
    > > "Ethem Azun" wrote:
    > >
    > > >
    > > > Hi,
    > > >
    > > > Perhaps the Viewstate("viewCount") is null. Try this; (Syntax might be
    > > > different, I'm not very fluent in VB)
    > > >
    > > >
    > > > Public Sub Page_Load(Source As Object, E As EventArgs)
    > > >
    > > > Dim viewCount As Integer
    > > >
    > > > If ViewState("viewCount") = Nothing Then
    > > > viewCount = 1
    > > > Else
    > > > viewCount = CType(ViewState("viewCount"), Integer) + 1
    > > > End If
    > > > labelViews.Text = "Times page has been viewed: " & viewCount.ToString()
    > > > ViewState("viewCount") = viewCount
    > > >
    > > >
    > > > I would also recommend to put
    > > >
    > > > ViewState("viewCount") = viewCount
    > > >
    > > > in the PreRender event instead of Page_Load, since this is the last event
    > > > called before the page renders and which eventually will have the latest
    > > > value of viewCount.
    > > >
    > > >
    > > > Hope this helps,
    > > >
    > > > Ethem Azun
    > > >
    > > >
    > > > "Hrishi R" wrote:
    > > >
    > > > > Hi:
    > > > >
    > > > > The following ASP.NET code segment throws an "Object reference not set to an
    > > > > instance of an object" exception.
    > > > > ------------------
    > > > > Public Sub Page_Load(Source As Object, E As EventArgs)
    > > > >
    > > > > Dim viewCount As Integer
    > > > >
    > > > > If ViewState("viewCount").ToString() = "" Then
    > > > > viewCount = 1
    > > > > Else
    > > > > viewCount = CType(ViewState("viewCount"), Integer) + 1
    > > > > End If
    > > > > labelViews.Text = "Times page has been viewed: " & viewCount.ToString()
    > > > >
    > > > > ViewState("viewCount") = viewCount
    > > > > -------------
    > > > > Please help, i can find many references to this exception on MSDN but none
    > > > > seems to help.
    > > > >
    > > > > Thanks.
    =?Utf-8?B?SHJpc2hpIFI=?=, Nov 16, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ^CrazyCoder^
    Replies:
    3
    Views:
    7,238
    Terrence Benade
    Sep 15, 2003
  2. David
    Replies:
    1
    Views:
    475
    David
    Jul 19, 2004
  3. Sam
    Replies:
    0
    Views:
    408
  4. Replies:
    1
    Views:
    1,433
    Ian Collins
    Jun 20, 2007
  5. Tinku
    Replies:
    5
    Views:
    336
    Chris Dollin
    Jun 10, 2008
Loading...

Share This Page