& in a QueryString

Discussion in 'ASP .Net' started by DaveF, Nov 10, 2004.

  1. DaveF

    DaveF Guest

    I am looping threw the querySting with:
    foreach (string name in Request.QueryString)

    I have a problem with a '&' being in one of the values. How do I deal with
    this?

    page.asp?theVal=2&theVal2=aa&e overdue
    --


    David Fetrow
    Helixpoint LLC.
    http://www.helixpoint.com
     
    DaveF, Nov 10, 2004
    #1
    1. Advertising

  2. DaveF

    John Toop Guest

    Hi Dave,
    I think you should URLEncode the string before you build the hyperlink.
    Basically "&" will create your ampersand ("&")

    jt
    "DaveF" <> wrote in message
    news:...
    >I am looping threw the querySting with:
    > foreach (string name in Request.QueryString)
    >
    > I have a problem with a '&' being in one of the values. How do I deal with
    > this?
    >
    > page.asp?theVal=2&theVal2=aa&e overdue
    > --
    >
    >
    > David Fetrow
    > Helixpoint LLC.
    > http://www.helixpoint.com
    >
    >
    >
     
    John Toop, Nov 10, 2004
    #2
    1. Advertising

  3. DaveF

    Shiva Guest

    '&' in the URL should be urlencoded as %26

    "DaveF" <> wrote in message
    news:...
    I am looping threw the querySting with:
    foreach (string name in Request.QueryString)

    I have a problem with a '&' being in one of the values. How do I deal with
    this?

    page.asp?theVal=2&theVal2=aa&e overdue
    --


    David Fetrow
    Helixpoint LLC.
    http://www.helixpoint.com
     
    Shiva, Nov 10, 2004
    #3
  4. DaveF

    chanmmn Guest

    No... you & is legal. Check for other:

    Example:

    Dim uriString As String = "http://www.contoso.com/search"
    ' Create a new WebClient instance.
    Dim myWebClient As New WebClient()
    ' Create a new NameValueCollection instance to hold the QueryString parameters and values.
    Dim myQueryStringCollection As New NameValueCollection()
    Console.Write(("Enter the word(s), separated by space characters, to search for in " + uriString + ": "))
    ' Read user input phrase to search in uriString.
    Dim searchPhrase As String = Console.ReadLine()
    ' Append necessary parameter/value pairs to the name/value container.
    ' as QueryString = "?q=Microsoft&btnG=Google+Search".
    If searchPhrase.Length > 1 Then
    'Assign the user-defined search phrase.
    myQueryStringCollection.Add("q", searchPhrase)
    ' If error, default to search 'Microsoft'.
    Else
    myQueryStringCollection.Add("q", "Microsoft")
    End If
    ' Assign auxilliary parameters required for the search.
    myQueryStringCollection.Add("btnG", "Google" + ChrW(43) + "Search")
    Console.WriteLine(("Searching " + uriString + " ......."))
    ' Attach QueryString to the WebClient.
    myWebClient.QueryString = myQueryStringCollection
    ' Download the search results Web page into 'searchresult.htm' for inspection.
    myWebClient.DownloadFile(uriString, "searchresult.htm")
    Console.WriteLine((ControlChars.Cr + "Download of " + uriString + " was successful. Please see 'searchresult.htm' for results."))


    "DaveF" <> wrote in message news:...
    >I am looping threw the querySting with:
    > foreach (string name in Request.QueryString)
    >
    > I have a problem with a '&' being in one of the values. How do I deal with
    > this?
    >
    > page.asp?theVal=2&theVal2=aa&e overdue
    > --
    >
    >
    > David Fetrow
    > Helixpoint LLC.
    > http://www.helixpoint.com
    >
    >
    >
     
    chanmmn, Nov 10, 2004
    #4
  5. DaveF

    Matt Berther Guest

    Hello chanmmn,

    & is legal ... as an item delimiter.

    If one of your items contains a &, it needs to be escaped (%26). You can
    do this manually or with Server.UrlEncode(value). If you dont do this, then
    the query string will be goofed up, because the delimiters are in the wrong
    spot.

    --
    Matt Berther
    http://www.mattberther.com

    > No... you & is legal. Check for other:
    >
    > Example:
    >
    > Dim uriString As String = "http://www.contoso.com/search"
    > ' Create a new WebClient instance.
    > Dim myWebClient As New WebClient()
    > ' Create a new NameValueCollection instance to hold the QueryString
    > parameters and values.
    > Dim myQueryStringCollection As New NameValueCollection()
    > Console.Write(("Enter the word(s), separated by space characters, to
    > search for in " + uriString + ": "))
    > ' Read user input phrase to search in uriString.
    > Dim searchPhrase As String = Console.ReadLine()
    > ' Append necessary parameter/value pairs to the name/value container.
    > ' as QueryString = "?q=Microsoft&btnG=Google+Search".
    > If searchPhrase.Length > 1 Then
    > 'Assign the user-defined search phrase.
    > myQueryStringCollection.Add("q", searchPhrase)
    > ' If error, default to search 'Microsoft'.
    > Else
    > myQueryStringCollection.Add("q", "Microsoft")
    > End If
    > ' Assign auxilliary parameters required for the search.
    > myQueryStringCollection.Add("btnG", "Google" + ChrW(43) + "Search")
    > Console.WriteLine(("Searching " + uriString + " ......."))
    > ' Attach QueryString to the WebClient.
    > myWebClient.QueryString = myQueryStringCollection
    > ' Download the search results Web page into 'searchresult.htm' for
    > inspection.
    > myWebClient.DownloadFile(uriString, "searchresult.htm")
    > Console.WriteLine((ControlChars.Cr + "Download of " + uriString + "
    > was
    > successful. Please see 'searchresult.htm' for results."))
    > "DaveF" <> wrote in message
    > news:...
    >
    >> I am looping threw the querySting with:
    >> foreach (string name in Request.QueryString)
    >> I have a problem with a '&' being in one of the values. How do I deal
    >>

    > with
    >
    >> this?
    >>
    >> page.asp?theVal=2&theVal2=aa&e overdue
    >> --
    >> David Fetrow
    >> Helixpoint LLC.
    >> http://www.helixpoint.com
    >>
     
    Matt Berther, Nov 11, 2004
    #5
    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. suzy
    Replies:
    3
    Views:
    3,116
  2. Axel Dahmen

    Pack QueryString

    Axel Dahmen, Jun 29, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    543
    Axel Dahmen
    Jun 29, 2003
  3. Natty Gur

    Re: Pack QueryString

    Natty Gur, Jun 29, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    503
    Natty Gur
    Jun 29, 2003
  4. Mehdi
    Replies:
    6
    Views:
    36,173
    sloan
    Apr 6, 2006
  5. Adeel Ahmad
    Replies:
    1
    Views:
    561
    Anthony Jones
    Mar 7, 2006
Loading...

Share This Page