locked
QueryString Ampersand RRS feed

  • Question

  • User-839012917 posted

    Hi All,

    I'm very new to VB so please understand I only partially understand what I'm looking at.

    I am taking user entered data and passing it through a query string to another page. the data will be used in another query that will eventually display on another page. When a user types an ampersand (&) the Request.QueryString does not recognize anything after the ampersand (&). Can someone tell me if there is a work around? Should i be using something else?

    my code looks like the below

    OrgName = Request.QueryString(OrgName)

    Thursday, May 16, 2013 2:39 PM

Answers

  • User1508394307 posted

    & works as a separator between key-value pairs

    ?company=Verizon&date=010113

    so, if you company will be AT&T then you should encode & as %26

    ?company=AT%26T&date=010113

    So, either use simple string.Replace("&", "%26") for every value, or use special method UrlEncode

    String MyURL;
    MyURL = "http://www.contoso.com/page.aspx?company=" + Server.UrlEncode("AT&T");

    http://msdn.microsoft.com/de-de/library/vstudio/zttxte6w.aspx

    Hope this helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 16, 2013 3:26 PM

All replies

  • User-1360095595 posted

    Use UrlEncode() on it. 

    Thursday, May 16, 2013 2:47 PM
  • User-839012917 posted

    Sorry, but can you explain a little further.  Where do i put UrlEncode() ? Im using Visual Studio and Intellisense doesn't recognize.  It recognizes HttpServerUtilit.UrlTokenEncode()  ?? 

    Thursday, May 16, 2013 3:09 PM
  • User1508394307 posted

    & works as a separator between key-value pairs

    ?company=Verizon&date=010113

    so, if you company will be AT&T then you should encode & as %26

    ?company=AT%26T&date=010113

    So, either use simple string.Replace("&", "%26") for every value, or use special method UrlEncode

    String MyURL;
    MyURL = "http://www.contoso.com/page.aspx?company=" + Server.UrlEncode("AT&T");

    http://msdn.microsoft.com/de-de/library/vstudio/zttxte6w.aspx

    Hope this helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 16, 2013 3:26 PM
  • User-839012917 posted

    That worked! Thank You for the help!

    Thursday, May 16, 2013 3:47 PM
  • User-1761965694 posted

    Hi,

    I've the same problem, but if I replace the & with %26, I keep have the same error...

    If I've a url like this:

    http://localhost/?pageId=33&companyName=AT&T&regNum=45

    or if I've a url like this:

    http://localhost/?pageId=33&companyName=AT%26T&regNum=45

    in both cases, when I try to get the companyName parameter, I obtain the AT string and not the AT&T, because both & and %26 delimit my companyName parameter.

    Why?

    thanks in advance!

    P.S. - Sorry for my english! :(

    Thursday, June 27, 2013 1:27 PM
  • User1508394307 posted

    Use

    http://localhost/?pageId=33&companyName=AT%26T&regNum=45

    and then

    Request.QueryString["companyName"] should return what you want.

    Thursday, June 27, 2013 2:07 PM
  • User-1761965694 posted

    Thank you for the quick answer! I had the error also doing as you suggest me, but in really the cause was stupid distraction issue! :(

    So it was only my fault!

    The page I debugged was inside a frame, but in my application all the calls are handled before by the top page that forwards the parameter to the inner page, so the external page gets the correct parameter (using %26) but when it forwards the parameter to the inner page it was been automatically decoded and forwarded as "&" and not as "%26".

    I solved re-encoding all of the parameters in forwarding method.

    Thank you for the attention spent to me! :)

    Friday, June 28, 2013 4:54 AM