title: Pass variable to sqldatasource Select command client side
body: <p>I have a grid and a dropdownlist from where the user select the query to fill in the grid.
  As it is now it works fine:</p>
  
  <pre><code>protected void Button1_Click(object sender, EventArgs e)
      {
          string selected = DropDownList1.SelectedItem.Value;
          if (selected == "0")
          {
              Label1.Text = "You shall select a query!";
          }
          else
          {
              int x = Int32.Parse(selected);
              string query = "";
              switch (x)
              {
                  case 1:     //Top 100 TB 321
                      query = @"...something...";
                      SqlDataSource1.SelectCommand = query;
                      break;
  
                  case 2:
                      query = @"....something else....";
                      SqlDataSource1.SelectCommand = query;
                      break;
  
                  case 3:     
                 .......
              }
          }
  </code></pre>
  
  <p>And in the markup I simply have the sqldatsource with the connection string:</p>
  
  <pre><code> &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server"
           ConnectionString="&lt;%$ ConnectionStrings:BOMConnectionString %&gt;" 
           &gt;&lt;/asp:SqlDataSource&gt;
  </code></pre>
  
  <p>With the above code I am unable to export the content of the grid because it generate an error "Object reference not set to an instance of an object". To avoid this issue, instead of defining the select command from code behind,  I should pass the query as variable to the sqldatasource in the markup.
  How can I pass the variable "query" to the sqldatasource SelectCommand in the markup?</p>