title: How to use one method to populate DropDownList or AJAX ComboBox
body: <p>I'm trying to write a single method that can populate either a DropDownList or an AJAX ComboBox (instead of having two separate, nearly identical methods) in C#. I pass the method a query string, the name of the text field, and the name of the value field, along with the dropdown object. Here's what I have so far:</p>
  
  <pre><code>public static void BindDropDown(string selectCmd, string textField, string valueField, object ddList)
  //Populate dropdown list (DropDownList or ComboBox)
  {
      // var for dropdown object
      object _ddl = null;
  
      // Get type of dropdown
      if (ddList.GetType() == typeof(DropDownList))
      {
          _ddl = (DropDownList)ddList;
      }
      else if (ddList.GetType() == typeof(ComboBox))
      {
          _ddl = (ComboBox)ddList;
      }
  
      // Setup DB connection
      SqlConnection dbConn = new SqlConnection(LTConnStr);
      SqlCommand myCmd = new SqlCommand(selectCmd, dbConn);
  
      //Set dropdown list params
      _ddl.DataTextField = textField;
      _ddl.DataValueField = valueField;
  
      try
      {
          dbConn.Open();
          _ddl.DataSource = myCmd.ExecuteReader();
          _ddl.DataBind();
      }
      finally
      {
          dbConn.Close();
      }
  } //end method BindDropDown
  </code></pre>
  
  <p>But I get the error "'object' does not contain a definition for 'DataTextField' ...". I've also tried the "is" syntax:</p>
  
  <pre><code>        // Get type of dropdown
          if (ddList is DropDownList)
          {
              _ddl = (DropDownList)ddList;
          }
          else if (ddList is ComboBox)
          {
              _ddl = (ComboBox)ddList;
          }
  </code></pre>
  
  <p>with the same result. I know I'm close, but I can't quite figure it out.</p>