Public varaible QueryString issue

  • Thread starter Thread starter VK
  • Start date Start date
V

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.
 
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("OnlineToolkitv2"))
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.
 
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!
 
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
 
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.
 
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!
 
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
 
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!
 
Back
Top