How to escape backslashes in querystring

dam picture dam · Mar 9, 2012 · Viewed 6.9k times · Source

I have an SqlDataSource that takes a value from querystring and bind it with a gridview. Everything works nicely unless the querystring value has a backslash in it. How do I escape the backslash?

Example:

www.example.com?id=testuser -> works.

www.example.com?id=test\user -> doesn't work even though "test\user" exists.

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>" 

    SelectCommand="SELECT * FROM User WHERE userID = @id">
    <SelectParameters>
        <asp:QueryStringParameter Name="id" 
            QueryStringField="id" DbType="String" />
    </SelectParameters>
</asp:SqlDataSource>


<form id="form1" runat="server">
  <div>
    <asp:GridView ID="GridViewTask" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1">
        <Columns>                
            <asp:BoundField DataField="UnitId" HeaderText="UnitId" ReadOnly="True" 
                SortExpression="UnitId" />
            <asp:BoundField DataField="UserName" HeaderText="UserName" ReadOnly="True" 
                SortExpression="UserName" />           
        </Columns>
    </asp:GridView>
  </div>
</form>

Thanks for your help.

Answer

John Pick picture John Pick · Mar 9, 2012

Backslash is %5C in a query string.