title: binding dropdownlist to a dataset returned from a function
body: <p>I have written the following method that binds the dropdown to the dataset. I need to call this method twice in my project on different pages. So I created a class and put the method in it and I am trying to access this method by creating an object. Having trouble doing so...</p>
  
  <pre><code> public void bind()
      {
          DataSet ds1 = new DataSet();
          SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
          con.Open();
          string strQuery = "SELECT CountryName + '(+' + CountryCode + ')' As CountryName,CountryCode from ACountry";
          SqlCommand cmd = new SqlCommand(strQuery, con);
          using (SqlDataAdapter da = new SqlDataAdapter(cmd))
          da.Fill(ds1, "AUser");
          ddlCountryCode.DataSource = ds1.Tables["AUser"];
          ddlCountryCode.DataTextField = "CountryCode";
  
          //ddlCountryCode.SelectedValue = "India(+91)";
          ddlCountryCode.DataBind();
          ddlCountryCode.SelectedIndex = ddlCountryCode.Items.IndexOf(ddlCountryCode.Items.FindByText("India(+91)"));
          con.Close();
      }
  </code></pre>
  
  <p>If I write this complete method in the new class, it does not recognize the controls (the dropdownlist) used in it &amp; so it throws an error. So I included only the following part in it:</p>
  
  <pre><code>  public void bindddl()
      {
          DataSet ds1 = new DataSet();
          SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
          con.Open();
          string strQuery = "SELECT CountryName + '(+' + CountryCode + ')' As CountryName,CountryCode from ACountry";
          SqlCommand cmd = new SqlCommand(strQuery, con);
          using (SqlDataAdapter da = new SqlDataAdapter(cmd))
          da.Fill(ds1, "AUser");
  
          con.Close();
      }
  </code></pre>
  
  <p>now this returns a dataset that I need to bind with the dropdown list on another form (.aspx). How do I do that?</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
      {
          Bind objbind = new Bind();
          ddlCountryCode.DataSource = objbind.----&gt;?????????;
          ddlCountryCode.DataTextField = "CountryCode";
  
          //ddlCountryCode.SelectedValue = "India(+91)";
          ddlCountryCode.DataBind();
          ddlCountryCode.SelectedIndex = ddlCountryCode.Items.IndexOf(ddlCountryCode.Items.FindByText("India(+91)"));
      }
  </code></pre>
  
  <p>Also, What else can I do? Is there another better option here?</p>