2

How can I add items to my comboBox which is in Form1, but the funcion that add items to that combo box is in another class ?

public void comboBox1_Categories_Load()
{
    SqlConnection con = new SqlConnection(connection_string);

    string select_string = "SELECT * FROM dbo.Categories";
    SqlCommand cmd = new SqlCommand(select_string, con);

    SqlDataReader myReader;
    con.Open();

    myReader = cmd.ExecuteReader();

    while (myReader.Read())
    {
        comboBox1.Items.Add(myReader[1]); 
    }

    myReader.Close();
    con.Close();
}
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
Marox
  • 359
  • 9
  • 26
  • try the following instead `comboBox1.Items.Add(myReader[1], myReader[1].ToString());` also you may want to wrap your sql objects around a `using(){}` to take advantage of auto disposing of objects or you could do this for your `myReader` object outside of the while loop `((IDisposible)myReader).Dispose();` please show the code that is calling the event || method `comboBox1_Categories_Load()` show all relevant code here when asking questions – MethodMan Oct 26 '15 at 20:41
  • 4
    you would have to pass a reference to the comboBox in to the function. A better approach would be to have your function return a list of items to Form1, then load them in to the comboBox there. I try to avoid passing control references around when it only concerns loading data in to it. – Lee Harrison Oct 26 '15 at 20:44
  • I agree with @LeeHarrison, return a list of items and have the Form's Load function call `AddRange` on the `List.Items` property – SwDevMan81 Oct 26 '15 at 20:46
  • I have no code for this because nothing works for now. What I'm trying to do is to add items using this funcion(in "Database" class) to comboBox1 which is in class "Form1" and call this funcion in "Form1". – Marox Oct 26 '15 at 20:47
  • @SwDevMan81 thanks for answer. I had a bad idea to use a component in another class. Now I made a method that is returning a list and it's a lot easier to work with. – Marox Oct 26 '15 at 21:39

2 Answers2

1

Form:

public partial class Form1 : Form
    {
        private BusinessLayer _businessLayer ;

        public Form1()
        {
            InitializeComponent();
            _businessLayer = new BusinessLayer();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var categories = _businessLayer.GetCategories();
            comboBox1.DataSource = categories;
        }
    }

Business Class:

class BusinessLayer
{
    private DataLayer _dataLayer;

    public BusinessLayer()
    {
        _dataLayer = new DataLayer();
    }

    internal List<string> GetCategories()
    {
        return _dataLayer.RetrieveCatagories();
    }
}

Data Layer (you can refactor and extract the connection to another method):

class DataLayer
{
    public const string ConnectionString = "my connection string";

    internal List<string> RetrieveCatagories()
    {
        List<string> items = new List<string>();

        using (SqlConnection con = new SqlConnection(ConnectionString))
        {
            string select_string = "SELECT * FROM dbo.Categories";
            SqlCommand cmd = new SqlCommand(select_string, con);

            con.Open();

            SqlDataReader myReader = cmd.ExecuteReader();

            while (myReader.Read())
            {
                items.Add(myReader[1].ToString());
            }

            myReader.Close();
        }

        return items;
    }


}
ehh
  • 3,412
  • 7
  • 43
  • 91
0

You can something like this:

public static OtherClass()
{
    public void RetrieveData(ComboBox comboBox)
    {
        SqlConnection con = new SqlConnection(connection_string);

        string select_string = "SELECT * FROM dbo.Categories";
        SqlCommand cmd = new SqlCommand(select_string, con);

        SqlDataReader myReader;
        con.Open();

myReader = cmd.ExecuteReader();

while (myReader.Read())
{
    comboBox.Items.Add(myReader[1]); 
}

myReader.Close();
con.Close();
    }
}

//then the class where your form is

public void comboBox1_Categories_Load()
{
    OtherClass.RetrieveData(comboBox1);
}
Juran
  • 177
  • 1
  • 8