Search code examples
asp.net.nethttpwebrequest

check and get parameter from url of current page asp.net


am trying to get the parameter "id" from the query string of the requested page like this

If Request.QueryString IsNot Nothing AndAlso _
            Request.QueryString.GetKey("id") IsNot Nothing Then
            DeleteVehicle(Request.QueryString.GetKey("id"))
 End If

but i get this error

System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 

Line 16:         If Request.QueryString IsNot Nothing AndAlso _
Line 17:             Request.QueryString.GetKey("id") IsNot Nothing Then

Source File: G:\projects_backup\Toaab\Toaa\admin\vehicle\view.aspx.vb    Line: 16 

please can you help me

EDIT

Am calling this in the page_load event

the same page (which has autogenerated link) is called through a hyperlink

i also change the code to

If Request.QueryString("id") IsNot Nothing OrElse Request.QueryString("id") IsNot String.Empty Then
 DeleteVehicle(Request.QueryString("id").ToString)
End If

Solution

  • To see if a value exists in the QueryString, check to see whether the value equates to an empty string rather than null:

    Try this instead:

    If String.IsNullOrEmpty(Request.QueryString("id")) = False Then
        DeleteVehicle(Request.QueryString("id"))
    End If