if request.querystring('Id") = ""

G

Guoqi Zheng

On my application, I need to have different action based on the pass in
query string. When the query string is not presented, I try to use

If request.querystring("id") ="" THEN ......

This is what I did in trational asp, however if I did abov in ASP.NET, I
always got an error of "Object reference not set to an instance of an
object. "

How should I solve the problem?

--
Kind regards

Guoqi Zheng
guoqi AT meetholland dot com
Http://www.meetholland.com
 
K

Ken Dopierala Jr.

Hi Guoqi,

You can do two things:

1. Use Request("id") = "".
2. Test for Nothing:
If Not (Request.QueryString("id") Is Nothing) Then ....

Good luck! Ken.
 
M

Mark Fitzpatrick

In VBScript there is essentially only one datatype, the variant datatype
which changes behavior based upon how it is referenced. That's why a
variable can be used to perform mathematical functions in one case, then
later be treated as a string. In .Net languages there is no variant so
things behave a little different. The Request.Querystring[key] returns an
object. When the key is not found, it returns null instead. An empty string
is not a null value since a null is a very special case all by itself. You
can test to see if the key is null first. You'll have to forgive me, but I
haven't played with VB in a while so I'll show the C# code instead and you
should be able to recognize how to do it in VB.

if(Request.Querystring["id"] == null)

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 
S

Steve C. Orr [MVP, MCSD]

Try this syntax:

If IsNothing(Request.Querystring("id")) OrElse Request.Querystring("id")=""
Then...
 
K

Karl

To add my $0.02, in .Net a null string evuluates true when compared to an
empty string :
if nothing = "" then
'this will always be true
end if

so checking for both is pointless.

if you simply want to check for not null/nothing:

if NOT Request.QueryString("id") = nothing then
...
end if

if you want to check for not null/nothing as well as not an empty string:

dim id as string = Request.QueryString("id")
IF NOT id IS NOTHING ANDALSO NOT id.length = 0 Then
'id is an actual string
END IF

In VB.Net you absolutely must use AndAlso instead of just And in the above
code, else it'll crash (and you should always be using AndAlso anyways).

checking for a string length = 0 vs comparing to an empty string is a
microsoft recommendation and will show up in FxCop.

Karl
 

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

Similar Threads


Top