Search code examples
sqlasp.netsqldatasource

SQLDataSource Parameter not working


I have the following SQLDataSource:

<asp:SqlDataSource runat="server" ID="MySqlDataSource" ConnectionString='<%$ ConnectionStrings:RmaReportingConnectionString %>' 
     SelectCommand="SELECT DISTINCT [Team] FROM [Locations] WHERE ([Team] IN (@Teams))">
        <SelectParameters>
             <asp:Parameter Name="Teams" Type="String" />
        </SelectParameters>
 </asp:SqlDataSource>

In code behind calling it like this:

MySqlDataSource.SelectParameters["Teams"].DefaultValue = "'Team 1','Team 2'";
MySqlDataSource.DataBind();

The issue is that I am not getting any results and I have a feeling it is due to syntax because if I run the raw SQL without the Parameter it works fine.


Solution

  • This could be because of the escape characters (') in your string.

    Try adding an @ sign before your variable string:

    MySqlDataSource.SelectParameters["Teams"].DefaultValue = @"'Team 1','Team 2'";