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

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

  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
    Likes Received:
    0
    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. Advertisements

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. Rahul Aggarwal
    Replies:
    3
    Views:
    4,649
    Netsharp Ninja
    Apr 6, 2004
  2. Charlie
    Replies:
    0
    Views:
    557
    Charlie
    Aug 11, 2003
  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:
    689
    =?Utf-8?B?SnVsaWEgQg==?=
    Aug 29, 2007
  4. Replies:
    0
    Views:
    1,587
  5. Bobby Edward
    Replies:
    1
    Views:
    431
    imransyed63
    Apr 24, 2009
  6. Elton W

    Query string and Parameter Passing Problem

    Elton W, May 12, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    229
    Elton W
    May 12, 2005
  7. roadrunner
    Replies:
    1
    Views:
    419
    Gunnar Hjalmarsson
    Feb 8, 2006
  8. nick
    Replies:
    1
    Views:
    700
    David Mark
    Feb 13, 2011
Loading...