Request.Querystring

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

Hi All,

This problem is really annoying me, as I am sure there is
a simple solution to it.

If I try to read a querystring value in the Page_Load
event and that querystring does not exist I get the error:

Object reference not set to an instance of an object

How do I check if the querystring is there before I try
to use it in my code?

Thanks,
Steve
 
Indexing into the querystring object doesn't fail. It is something you are
doing with the result of that, that is failing, such as calling Substring,
or using it elsewhere.
 
Before accessing your querystring value use

Request.Params.Get["<Your Parameter>"] != null

to check if those exists.


Thanks,
-Shan
 
I get the error when I try to do this:

Dim sID As String
sID = Request.QueryString("id")
If sID.Length > 0 Then
--- my code ---


Should I be doing it another way?

Steve
 
Well, there you go. You are trying to access the Length property of a
string variable that is set to Nothing. So the QueryString is working just
fine - it does not cause problems, it returns Nothing if 'id' isn't there..
You need to check if sID is nothing or not, before you try to do anything
with it:

If Not IsNothing(sID) Then
....
 
Back
Top