|
-
Jan 23rd, 2008, 11:41 AM
#1
I wonder how many charact
Re: Global Variable won't increment/decrement
You can define your G_Index in your global.asax Application_Start event, add it to the Application bucket, and all instances of your application will share that integer value.
Code:
'this is Global.asax.vb
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
Application.Add("G_Index", 1)
End Sub
'this is your page
'something like below
Dim r as Integer = Convert.ToInt32(Application("G_Index"))
You can then use that variable or increment anywhere.
-
Jan 24th, 2008, 08:14 AM
#2
Thread Starter
New Member
Re: Global Variable won't increment/decrement
Hi, Thank you for your solutions Harsh, space_monkey and Nemaroller.
First, I tried Space_monkey suggestion and it still did not change the value.
Then, I tried Nemaroller suggestion that introduces an error as following:
" Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 8: 'Public Shared G_INDEX As Integer
Line 9: 'Dim G_INDEX As Integer
Line 10: Dim r As Integer = Convert.ToInt32(Application("G_INDEX"))
Line 11: 'Private G_INDEX_NAME As String = "G_INDEX"
Line 12: "
It seems cause of the error was from "Dim r As Integer = Convert.ToInt32(Application("G_INDEX"))". I have added a 'Global.asax' to the project:
Code:
<%@ Application Language="VB" %>
<script runat="server">
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs on application startup
Application.Add("G_INDEX", 1)
End Sub
Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs on application shutdown
End Sub
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when an unhandled error occurs
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when a new session is started
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when a session ends.
' Note: The Session_End event is raised only when the sessionstate mode
' is set to InProc in the Web.config file. If session mode is set to StateServer
' or SQLServer, the event is not raised.
End Sub
</script>
By the way, why didn't I get a 'Global.asax.vb' file when I add 'Global.asax' to the project? I am using MS Visual Web Developer 2008 Express Edition. At the moment I am using a Val(labelJobNo.text) to store a value of a global variable.
Thank you all...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|