How to pass ampersand in query string parameter in ASP.NET

April 01, 2014 0 Comments

According to our previous article how to use query string in, we use query string parameter for searching value from the database.Now, If you want to pass parameter value with special character like "&" . Let's take an simple example,  how to pass & in QueryString parameter.


Response.Redirect("~/Default3.aspx?userName=" + Server.UrlEncode("you&me") + "&Password=jacob");

Here, In above mentioned code your username contains & symbol between you&me and we use & for separates the field.



Here, we use %26, in the place of & symbol. Any browser consider & symbol as %26.

Complete Code

   Default2.aspx code

<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">LinkButton</asp:LinkButton>

Default2.aspx.cs code

protected void LinkButton1_Click(object sender, EventArgs e)
        Response.Redirect("~/Default3.aspx?userName=" + Server.UrlEncode("you&me") + "&Password=jacob");

Now, your button click event call another webform, where you bind  gridview through QueryString
Default3.aspx code

<form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="sno" DataSourceID="SqlDataSource1">
                <asp:BoundField DataField="sno" HeaderText="sno" InsertVisible="False" 
                    ReadOnly="True" SortExpression="sno" />
                <asp:BoundField DataField="userName" HeaderText="userName" 
                    SortExpression="userName" />
                <asp:BoundField DataField="Password" HeaderText="Password" 
                    SortExpression="Password" />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
            SelectCommand="SELECT * FROM [user] WHERE (([userName] = @userName) AND ([Password] = @Password))">
                <asp:QueryStringParameter Name="userName" QueryStringField="userName" 
                    Type="String" />
                <asp:QueryStringParameter Name="Password" QueryStringField="Password" 
                    Type="String" />


 Code Generate the following output

How to pass ampersand in query string parameter in ASP.NET

How to pass ampersand in query string parameter in ASP.NET

Jacob Lefore

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google