Click here to Skip to main content
11,641,067 members (60,231 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
Hi,
I am trying to clear my Querystring value in my URL but it's not working below is my code..Please find out the solution

SqlConnection cnn = new SqlConnection(Conn);
                SqlCommand cmd = new SqlCommand("Update tbl_Country set Name='"+txtName.Text.Trim()+"',CountryImg='"+Img+"' where Id="+id+"", cnn);
                cnn.Open();
                cmd.ExecuteNonQuery();
                FillGrid();
                txtName.Text = "";
                ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script language="'javascript'">alert('Country Updated successfully')</script>");
                //Request.QueryString.Remove("id");
                //Request.QueryString.Clear();
                PropertyInfo isReadOnly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
                isReadOnly.SetValue(this.Request.QueryString, false, null);
                this.Request.QueryString.Remove("id");
                cnn.Close();
Posted 20-Feb-13 17:44pm
sahmed3351
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can not directly remove query string. It is readonly property for HttpRequest object. What you can just read all querystring from Request object and create new querystring as you needed and Response.Redirect that page with new querystring.
The Link will be helpful for you.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

var nameValueCollection = HttpUtility.ParseQueryString(HttpContext.Current.Request.QueryString.ToString());
nameValueCollection.Remove(tenantPropertyName);
string url = HttpContext.Current.Request.Path + "?" + nameValueCollection;
  Permalink  
Comments
technovate at 21-Feb-13 4:33am
   
Hi Azziet..
I've tried your code but still i am getting the ? mark symbol in my url how to remove ? also..
var nameValueCollection = HttpUtility.ParseQueryString(HttpContext.Current.Request.QueryString.ToString());
nameValueCollection.Remove("id");
string urll = HttpContext.Current.Request.Path + "?" + nameValueCollection;
Response.Redirect(urll);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Mika Wendelius 400
1 OriginalGriff 205
2 DamithSL 203
3 CPallini 145
4 Afzaal Ahmad Zeeshan 129
0 Mika Wendelius 380
1 OriginalGriff 205
2 DamithSL 203
3 CPallini 125
4 Afzaal Ahmad Zeeshan 89


Advertise | Privacy | Mobile
Web03 | 2.8.150731.1 | Last Updated 21 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100