Query String Problem\VB

Discussion in 'ASP .Net' started by George, Feb 16, 2004.

  1. George

    George Guest

    I am trying to check whether a query string is being passed to my .aspx page, like so:

    If Not Request.QueryString Is Nothing Then
    If CInt(Request.QueryString.GetValues("values")(0)) <> 1 Then
    Response.Redirect("../somepage.htm")
    End If
    Else
    Response.Redirect("../somepage.htm")
    End If

    If there is a query string, everything is fine, but when there isn't one, the app crashes on the
    second line :

    If CInt(Request.QueryString.GetValues("values")(0)) <> 1 Then

    :with this error:

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

    What I don't understand, is why is it getting passed this line anyway:

    If Not Request.QueryString Is Nothing Then

    :when there wasn't even a query string passed? Why didn't it jump down to the Else part, instead?

    Thanks,
    George
     
    George, Feb 16, 2004
    #1
    1. Advertising

  2. George

    Anatoly Guest

    Check for Request.QueryString.ToString() = string.empty

    "George" <------@----.---> wrote in message
    news:Sd%Xb.6715$...
    > I am trying to check whether a query string is being passed to my .aspx

    page, like so:
    >
    > If Not Request.QueryString Is Nothing Then
    > If CInt(Request.QueryString.GetValues("values")(0)) <> 1 Then
    > Response.Redirect("../somepage.htm")
    > End If
    > Else
    > Response.Redirect("../somepage.htm")
    > End If
    >
    > If there is a query string, everything is fine, but when there isn't one,

    the app crashes on the
    > second line :
    >
    > If CInt(Request.QueryString.GetValues("values")(0)) <> 1 Then
    >
    > :with this error:
    >
    > Exception Details: System.NullReferenceException: Object reference not set

    to an instance of an
    > object.
    >
    > What I don't understand, is why is it getting passed this line anyway:
    >
    > If Not Request.QueryString Is Nothing Then
    >
    > :when there wasn't even a query string passed? Why didn't it jump down to

    the Else part, instead?
    >
    > Thanks,
    > George
    >
    >
     
    Anatoly, Feb 16, 2004
    #2
    1. Advertising

  3. Hi George,

    The point is that the QueryString property as such is always set and never
    null (nothing). It is an object of NameValueCollection type that can have
    zero elements (if query string is empty) but still is instantiated.

    So, instead of checking the Request.QueryString existance, you should check
    if it has the elemenent (field key) you are looking for:

    string[] arString;
    arString = Request.QueryString.GetValues("values");

    if (arString != null)
    {
    ...
    }

    Sorry for the c# code, but in nicely transposes to your VB.

    --
    Cezary Nolewajka
    mailto:
    remove all "no-sp-am-eh"s to reply


    "George" <------@----.---> wrote in message
    news:Sd%Xb.6715$...
    > I am trying to check whether a query string is being passed to my .aspx

    page, like so:
    >
    > If Not Request.QueryString Is Nothing Then
    > If CInt(Request.QueryString.GetValues("values")(0)) <> 1 Then
    > Response.Redirect("../somepage.htm")
    > End If
    > Else
    > Response.Redirect("../somepage.htm")
    > End If
    >
    > If there is a query string, everything is fine, but when there isn't one,

    the app crashes on the
    > second line :
    >
    > If CInt(Request.QueryString.GetValues("values")(0)) <> 1 Then
    >
    > :with this error:
    >
    > Exception Details: System.NullReferenceException: Object reference not set

    to an instance of an
    > object.
    >
    > What I don't understand, is why is it getting passed this line anyway:
    >
    > If Not Request.QueryString Is Nothing Then
    >
    > :when there wasn't even a query string passed? Why didn't it jump down to

    the Else part, instead?
    >
    > Thanks,
    > George
    >
    >
     
    Cezary Nolewajka, Feb 16, 2004
    #3
  4. George

    George Guest

    Anatoly,

    Apparently, ToString is not available in that context. Thanks for the suggestion, though.

    George

    "Anatoly" <> wrote in message news:...
    > Check for Request.QueryString.ToString() = string.empty
    >
    > "George" <------@----.---> wrote in message
    > news:Sd%Xb.6715$...
    > > I am trying to check whether a query string is being passed to my .aspx

    > page, like so:
    > >
    > > If Not Request.QueryString Is Nothing Then
    > > If CInt(Request.QueryString.GetValues("values")(0)) <> 1 Then
    > > Response.Redirect("../somepage.htm")
    > > End If
    > > Else
    > > Response.Redirect("../somepage.htm")
    > > End If
    > >
    > > If there is a query string, everything is fine, but when there isn't one,

    > the app crashes on the
    > > second line :
    > >
    > > If CInt(Request.QueryString.GetValues("values")(0)) <> 1 Then
    > >
    > > :with this error:
    > >
    > > Exception Details: System.NullReferenceException: Object reference not set

    > to an instance of an
    > > object.
    > >
    > > What I don't understand, is why is it getting passed this line anyway:
    > >
    > > If Not Request.QueryString Is Nothing Then
    > >
    > > :when there wasn't even a query string passed? Why didn't it jump down to

    > the Else part, instead?
    > >
    > > Thanks,
    > > George
    > >
    > >

    >
    >
     
    George, Feb 16, 2004
    #4
  5. George

    George Guest

    Cezary,

    I tried what you suggested and that solved my problem! Learn something new everyday.

    Thanks for your help.
    George


    "Cezary Nolewajka" <> wrote in message
    news:%...
    > Hi George,
    >
    > The point is that the QueryString property as such is always set and never
    > null (nothing). It is an object of NameValueCollection type that can have
    > zero elements (if query string is empty) but still is instantiated.
    >
    > So, instead of checking the Request.QueryString existance, you should check
    > if it has the elemenent (field key) you are looking for:
    >
    > string[] arString;
    > arString = Request.QueryString.GetValues("values");
    >
    > if (arString != null)
    > {
    > ...
    > }
    >
    > Sorry for the c# code, but in nicely transposes to your VB.
    >
    > --
    > Cezary Nolewajka
    > mailto:
    > remove all "no-sp-am-eh"s to reply
    >
    >
    > "George" <------@----.---> wrote in message
    > news:Sd%Xb.6715$...
    > > I am trying to check whether a query string is being passed to my .aspx

    > page, like so:
    > >
    > > If Not Request.QueryString Is Nothing Then
    > > If CInt(Request.QueryString.GetValues("values")(0)) <> 1 Then
    > > Response.Redirect("../somepage.htm")
    > > End If
    > > Else
    > > Response.Redirect("../somepage.htm")
    > > End If
    > >
    > > If there is a query string, everything is fine, but when there isn't one,

    > the app crashes on the
    > > second line :
    > >
    > > If CInt(Request.QueryString.GetValues("values")(0)) <> 1 Then
    > >
    > > :with this error:
    > >
    > > Exception Details: System.NullReferenceException: Object reference not set

    > to an instance of an
    > > object.
    > >
    > > What I don't understand, is why is it getting passed this line anyway:
    > >
    > > If Not Request.QueryString Is Nothing Then
    > >
    > > :when there wasn't even a query string passed? Why didn't it jump down to

    > the Else part, instead?
    > >
    > > Thanks,
    > > George
    > >
    > >

    >
     
    George, Feb 16, 2004
    #5
  6. George

    Srowe1975

    Joined:
    Oct 15, 2008
    Messages:
    3
    response.redirect does not work with querystring

    I am trying response.redirect to do back to the default page. but instead of redirecting to default.aspx it keeps the querystring values in memory and adds them to the new url I am trying to redirect to
    any help?

    Stephanie:damnmate:
     
    Srowe1975, Oct 15, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Learner
    Replies:
    1
    Views:
    1,023
    Marina Levit [MVP]
    Jan 30, 2006
  2. Anonymous
    Replies:
    0
    Views:
    1,544
    Anonymous
    Oct 13, 2005
  3. =?Utf-8?B?SnVsaWEgQg==?=

    Date string problem in query string when posting back

    =?Utf-8?B?SnVsaWEgQg==?=, Aug 29, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    578
    =?Utf-8?B?SnVsaWEgQg==?=
    Aug 29, 2007
  4. roadrunner
    Replies:
    1
    Views:
    263
    Gunnar Hjalmarsson
    Feb 8, 2006
  5. nick
    Replies:
    1
    Views:
    512
    David Mark
    Feb 13, 2011
Loading...

Share This Page