Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

C#
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

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.
 
Share this answer
 
C#
var nameValueCollection = HttpUtility.ParseQueryString(HttpContext.Current.Request.QueryString.ToString());
nameValueCollection.Remove(tenantPropertyName);
string url = HttpContext.Current.Request.Path + "?" + nameValueCollection;
 
Share this answer
 
Comments
sahmed3 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
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900