0

I need to select the column in a where clause dynamically from the value of a control, Can someone help with that.

I need help with this bit:

SelectCommand="SELECT * FROM [Skills] WHERE (@YearCol = @YearValue)">

YearCol is a dropdown list, I want column in the where clause to be the selection in the dropdown list.

Here is the full code I am using.

<form id="form1" runat="server">
    <div>
    </div>
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
        <asp:ListItem>Year1</asp:ListItem>
        <asp:ListItem>Year2</asp:ListItem>
        <asp:ListItem>Year3</asp:ListItem>
        <asp:ListItem>Year4</asp:ListItem>
        <asp:ListItem>Year5</asp:ListItem>
    </asp:DropDownList>
    <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Skills] WHERE (@YearCol = @YearValue)">
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownList1" DefaultValue="Year1" Name="YearCol" PropertyName="SelectedValue" Type="String" />
            <asp:Parameter DefaultValue="1" Name="YearValue" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>
</form>
1
  • You cannot use a Parameter to represent a column name. You should do it with code manually building your query based on the value selected in the DropDownList. Commented Jan 9, 2018 at 11:40

2 Answers 2

2

You should change it manually because parameters just use for col values not col name, so do something like:

protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
    string selectCommand = "SELECT * FROM [Skills] WHERE ({0} = @YearValue)";
    string yearParam = Convert.ToString(e.Command.Parameters["@YearCol"].Value);
    string yearColName = string.Empty;
    switch (yearParam)
    {
        case "Year1":
            yearColName = "Year1";//What you want
            break;
        case "Year2":
            yearColName = "Year2,Year3";
            break;
          .....
    }
    e.Command.Parameters["@YearValue"].Value = yearParam;
    SqlDataSource1.SelectCommand = string.Format(selectCommand, yearColName); 
}

Can be done in Selecting event or ddl change event or where you want .

Sign up to request clarification or add additional context in comments.

Comments

0

I am accepting Aria's answer, but also adding my final code in case it helps someone. I just needed dropdownbox values as the Column Name in the where clause. Still surprised it is not supported through ASP.

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString))
{
con.Open();
string selectCommand = "SELECT * FROM [Skills] WHERE ({0} = '1')";
String yearColName = Convert.ToString(DropDownList1.SelectedItem);
SqlDataSource1.SelectCommand = string.Format(selectCommand, yearColName);
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.