Brendan Brendan - 5 years ago 201
Vb.net Question

Request.QueryString cannot be used in this way

I'm trying to retrieve the values from the Url using the second function. the first function function passes in the checkedValue variable and the second should check to see if the category is there and if it is return the value.

Imports System.Web.HttpRequest

Public Class ReviewPageDefault
Inherits Page

Shared Function GetProductId()

Dim util As New Utilities

Dim product = ""

If util.CheckString("schoolid") = "" And util.CheckString("stockid") = "" Then
product = (util.CheckString("stock"))
ElseIf util.CheckString("stock") = "" And util.CheckString("stockid") = "" Then
product = (util.CheckString("FN"))
Else
Dim stockId = util.CheckString("stockid")
product = stockId
End If
Return product

End Function

End Class

Public Class Utilities
Inherits Page

Public Function CheckString(checkedValue As String)

Dim check = ""
If Request.QueryString(checkedValue) Is Nothing Then
Else
check = Request.QueryString(checkedValue)
End If
Return check

End Function

End Class


However whenever I try to test the page I get the error

System.Web.HttpException: Request is not available in this context


The code is located in the code behind for an asp.net page, that attempts to retrieve the product value

<script>
var product = '<%= ReviewPageDefault.GetProductId()%>';
</script>


I've searched the internet over and have found nothing, any help or advice is appreciated

Answer Source

Have your Utilities class take the Request as parameter instead of inheriting from Page:

Public Class Utilities

    Public Function CheckString(checkedValue As String, request as HttpRequest)

        Dim check = ""
        If request.QueryString(checkedValue) Is Nothing Then
        Else
            check = request.QueryString(checkedValue)
        End If
        Return check

    End Function

End Class

and when calling it pass the Request from the main page:

util.CheckString("stock", Request)

or make it an extension method on the HttpRequest class so that you can use it like this:

Request.CheckString("stock")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download