Request.Querystring

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
 
M

Marina

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.
 
S

Shan

Before accessing your querystring value use

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

to check if those exists.


Thanks,
-Shan
 
S

Steve

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
 
M

Marina

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
....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top