title: Dynamic column in ASP.Net SQL query Where clause
body: <p>I need to select the column in a where clause dynamically from the value of a control, Can someone help with that. </p>
  
  <p>I need help with this bit: </p>
  
  <pre><code>SelectCommand="SELECT * FROM [Skills] WHERE (@YearCol = @YearValue)"&gt;
  </code></pre>
  
  <p>YearCol is a dropdown list, I want column in the where clause to be the selection in the dropdown list. </p>
  
  <p>Here is the full code I am using. </p>
  
  <pre><code>&lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
      &lt;/div&gt;
      &lt;asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"&gt;
          &lt;asp:ListItem&gt;Year1&lt;/asp:ListItem&gt;
          &lt;asp:ListItem&gt;Year2&lt;/asp:ListItem&gt;
          &lt;asp:ListItem&gt;Year3&lt;/asp:ListItem&gt;
          &lt;asp:ListItem&gt;Year4&lt;/asp:ListItem&gt;
          &lt;asp:ListItem&gt;Year5&lt;/asp:ListItem&gt;
      &lt;/asp:DropDownList&gt;
      &lt;asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"&gt;
      &lt;/asp:GridView&gt;
      &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="&lt;%$ ConnectionStrings:ConnectionString %&gt;" SelectCommand="SELECT * FROM [Skills] WHERE (@YearCol = @YearValue)"&gt;
          &lt;SelectParameters&gt;
              &lt;asp:ControlParameter ControlID="DropDownList1" DefaultValue="Year1" Name="YearCol" PropertyName="SelectedValue" Type="String" /&gt;
              &lt;asp:Parameter DefaultValue="1" Name="YearValue" Type="String" /&gt;
          &lt;/SelectParameters&gt;
      &lt;/asp:SqlDataSource&gt;
  &lt;/form&gt;
  </code></pre>