Brendan
Brendan

Reputation: 20

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

Upvotes: 0

Views: 715

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039368

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")

Upvotes: 1

Related Questions