0

I have a query string which passes 6 parameters in C# as shown below

string url = "Report.aspx?Desc=" + Desc.SelectedValue + "&PON=" + PNumber.Text + "&InsNme=" + ins.ToUpper().ToString() + "&BackTy=" + cb.SelectedValue + "&StartDate=" + txtDate.Text + "&EndDate=" + txtTodate.Text + "&Name=" + nme;
string s = "window.open('" + url + "', 'popup_window', 'width=1500,height=800,left=200,top=150,resizable=yes');";
ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);

Now, in the above string InsNme contains a value of John Patrice Joanne. Instead of complete value of InsNme Report.aspx contains just John. How to handle this?

Lara
  • 2,821
  • 7
  • 39
  • 72

1 Answers1

2

The spaces in the name are breaking the URL.

If you want to do it yourself, replace spaces with %20. Otherwise a simple, but not anywhere near "good" technique is:

url = "Report.aspx?";
// for each name value pair ...
url += dataLabel + "=" + System.Web.HttpUtility.UrlEncode( dataChunk ) +"&";

The utility is preferred as it will take care of other, similar issues such as literal '&' in a name.

Check this answer for better solutions. How to build a query string for a URL in C#?

Chris Cudmore
  • 29,793
  • 12
  • 57
  • 94
  • After i added, `url = System.Web.HttpUtility.UrlEncode( url ); string s = "window.open('" + url + "', 'popup_window', 'width=1500,height=800,left=200,top=150,resizable=yes');"; ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);` I started getting `HTTP Error 404.11 - Not Found`, why is this? – Lara Jul 26 '19 at 18:19
  • @Lara That's very easy to answer yourself: Look at `url` in the debugger. Does it exist or not? But I believe I answered it for you in my comment above. – 15ee8f99-57ff-4f92-890c-b56153 Jul 26 '19 at 18:20
  • @EdPlunkett I am getting error like `The request filtering module is configured to deny a request that contains a double escape sequence.` – Lara Jul 26 '19 at 18:22
  • @Lara Look. At. `url`. In. The. Debugger. Look at the actual string you've got there. Do you know how to set a breakpoint and use the debugger? – 15ee8f99-57ff-4f92-890c-b56153 Jul 26 '19 at 18:22
  • 1
    Good catch @EdPlunkett I was answering without thinking. – Chris Cudmore Jul 26 '19 at 18:35