471,339 Members | 1,254 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Public varaible QueryString issue

VK
Folks,

An aspx page has some querystring variables in the URL.

In the code beind file, I tried requesting these variables and setting
as public variables, so it is not specific to a sub.

Public instance_id As Decimal = Request.QueryString("i_id")

This is defined right above:Private Sub Page_Load

It doesn't work - is something missing??

Thanks.

*** Sent via Developersdex http://www.developersdex.com ***
Jul 22 '05 #1
11 1809
Does it error or simply not set the value?
"VK" wrote:
Folks,

An aspx page has some querystring variables in the URL.

In the code beind file, I tried requesting these variables and setting
as public variables, so it is not specific to a sub.

Public instance_id As Decimal = Request.QueryString("i_id")

This is defined right above:Private Sub Page_Load

It doesn't work - is something missing??

Thanks.

*** Sent via Developersdex http://www.developersdex.com ***

Jul 22 '05 #2
Why not set the variable in the Load or Init events?

"VK" <vk@vk.com> wrote in message
news:OG**************@tk2msftngp13.phx.gbl...
Folks,

An aspx page has some querystring variables in the URL.

In the code beind file, I tried requesting these variables and setting
as public variables, so it is not specific to a sub.

Public instance_id As Decimal = Request.QueryString("i_id")

This is defined right above:Private Sub Page_Load

It doesn't work - is something missing??

Thanks.

*** Sent via Developersdex http://www.developersdex.com ***

Jul 22 '05 #3
VK
The error message:
Request is not available in this context
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.Web.HttpException: Request is not available in
this context

Source Error:
Line 40: Public myConn As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings("O nlineToolkitv2"))
Line 41: Public tool_id As Decimal = 1
Line 42: Public instance_id As Integer = Request.QueryString("i_id")
Line 43: Public number_of_questions As Integer

- I can't use it in Load since it cannot be used in other subs or
functions, its scope would exist with in Load only.

thanks.

*** Sent via Developersdex http://www.developersdex.com ***
Jul 22 '05 #4

Hard to tell from the chunks of code you posted, but it seems to me like you
are NOT putting this declarations inside the Page_Load Event. It looks to me
like you are placing them as classwide scope variables and trying to
initialize them to a value, which would explain why the request object is not
available.

Place the variable definitions classwide scope and initalize the values in
the page_load event.

Good luck!

--
Juan Romero
-----------------------------------------
The successful person has the habit of doing the things failures don't like
to do.
E.M. Gray
"VK" wrote:
The error message:
Request is not available in this context
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.Web.HttpException: Request is not available in
this context

Source Error:
Line 40: Public myConn As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings("O nlineToolkitv2"))
Line 41: Public tool_id As Decimal = 1
Line 42: Public instance_id As Integer = Request.QueryString("i_id")
Line 43: Public number_of_questions As Integer

- I can't use it in Load since it cannot be used in other subs or
functions, its scope would exist with in Load only.

thanks.

*** Sent via Developersdex http://www.developersdex.com ***

Jul 22 '05 #5
VK
I set this:
Public instance_id As Integer = Request.QueryString("i_id")

right above Page_load, so that this variable could be used in all subs.

It fails near the request.querystring.

Will this not work?

Thanks

*** Sent via Developersdex http://www.developersdex.com ***
Jul 22 '05 #6
The Request object may not have been fully initialized when the class is
constructed. You should only set variables like this in the Init or Load
events. If your design is requiring that you do it this way, you may need
to reevaluate your design. There is a reason that ASP.NET defines a life
cycle for the pages (Init-->LoadViewState-->Load-->(control
events)-->SaveViewState-->PreRender-->Unload). At each stage, there is
something new available to your system.

"VK" <vk@vk.com> wrote in message
news:uE**************@tk2msftngp13.phx.gbl...
I set this:
Public instance_id As Integer = Request.QueryString("i_id")

right above Page_load, so that this variable could be used in all subs.

It fails near the request.querystring.

Will this not work?

Thanks

*** Sent via Developersdex http://www.developersdex.com ***

Jul 22 '05 #7
No. It will not.

Just because you want the variable to be available to all subs does not mean
that you have to INITIALIZE it there.

Leave the variable where it is but initialize the value in the page_load
event.

E.G.:

Public instance_id As Integer

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
instance_id = request.querystring("i_id")
End If
End Sub

Also, keep in mind that your variable will not retain its value across
postbacks unless you store in a persistant medium such as the session or the
viewstate.

Good luck!
--
Juan Romero
-----------------------------------------
The successful person has the habit of doing the things failures don't like
to do.
E.M. Gray
"VK" wrote:
I set this:
Public instance_id As Integer = Request.QueryString("i_id")

right above Page_load, so that this variable could be used in all subs.

It fails near the request.querystring.

Will this not work?

Thanks

*** Sent via Developersdex http://www.developersdex.com ***

Jul 22 '05 #8
VK
Actually it would maintain its value by defining the following way.

=====

Public instance_id As Integer

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
instance_id = request.querystring("i_id")
If Not IsPostBack Then
' code
End If
End Sub

====

It is working!

thanks. Vani

*** Sent via Developersdex http://www.developersdex.com ***
Jul 22 '05 #9
Hi,

I am glad to hear it's working. Are you sure it mantains its value across
postbacks?... I don't think so, but I may be wrong. Check it out.

Good luck!
--
Juan Romero
-----------------------------------------
The successful person has the habit of doing the things failures don't like
to do.
E.M. Gray
"VK" wrote:
Actually it would maintain its value by defining the following way.

=====

Public instance_id As Integer

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
instance_id = request.querystring("i_id")
If Not IsPostBack Then
' code
End If
End Sub

====

It is working!

thanks. Vani

*** Sent via Developersdex http://www.developersdex.com ***

Jul 22 '05 #10
VK
I set this:
Public instance_id As Integer = Request.QueryString("i_id")

right above Page_load, so that this variable could be used in all subs.

It fails near the request.querystring.

Will this not work?

Thanks

*** Sent via Developersdex http://www.developersdex.com ***
Nov 22 '05 #11
The Request object may not have been fully initialized when the class is
constructed. You should only set variables like this in the Init or Load
events. If your design is requiring that you do it this way, you may need
to reevaluate your design. There is a reason that ASP.NET defines a life
cycle for the pages (Init-->LoadViewState-->Load-->(control
events)-->SaveViewState-->PreRender-->Unload). At each stage, there is
something new available to your system.

"VK" <vk@vk.com> wrote in message
news:uE**************@tk2msftngp13.phx.gbl...
I set this:
Public instance_id As Integer = Request.QueryString("i_id")

right above Page_load, so that this variable could be used in all subs.

It fails near the request.querystring.

Will this not work?

Thanks

*** Sent via Developersdex http://www.developersdex.com ***

Nov 22 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by braindead | last post: by
12 posts views Thread by Alex | last post: by
7 posts views Thread by fox | last post: by
10 posts views Thread by Phillip Vong | last post: by
reply views Thread by rosydwin | last post: by

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.