473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Error while using "State Bags".

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.
Nov 18 '05 #1
4 1185

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.

Nov 18 '05 #2
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.

Nov 18 '05 #3

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.

Nov 18 '05 #4
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.

Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Askari | last post by:
Hi, How do for do a "select()" on a CheckButton in a menu (make with add_checkbutton(....) )? I can modify title, state, etc but not the "check state". :-( Askari
3
by: Just Me | last post by:
In code I check to see if the Floppy drive is ready and display it's state. But if the user inserts or removes a floppy disk I need an event or a Wndproc message to cause the code to display the...
1
by: Paul Tomlinson | last post by:
Question about a System.Threading.Timer object and the "state" object you pass to it... Timer stateTimer = new Timer( = new TimerCallback( OnTimer ), o, 1000, 1000); I have an array of timer...
0
by: Ismail Fatih Yıldırım | last post by:
I modified the RSACSPSample from MSDN to try out a simple commutative encryption model using RSA encryption but when i run the progrem the first encryption command works but during the second...
0
by: projectbeach | last post by:
Hi, I've been struggling to create a valid XML schema that implements the following innocent-looking concept: A box may contain a bunch of bags (possibly 0). A bag must contain a bunch of...
4
by: louvino | last post by:
Hi, I have some links. When I click on one, a window opens but during the loading of this window, I would like the cursor is in state "wait" (using CSS : cursor : wait; ) Help me :-)
10
by: Jason Doucette | last post by:
Situation: I have a simple struct that, say, holds a color (R, G, and B). I created my own constructors to ease its creation. As a result, I lose the default constructor. I dislike this, but...
4
by: frostbb | last post by:
I have a C# WinForms treeview with 20 or so 'level 0' nodes. My users will normally have 2 of the 'level 0' branches open down to a '5th level' selected node. The users will make updates to...
1
by: Gwindor | last post by:
I have a client who that was using an mdb file, and they appear to have accidentally attempted to compact/repair or convert it, and then tried to stop this activity in mid-process. Oy vey. If I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.