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>