Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi everyone ..
i know how to load data from sql to combobox i haven't problem with that but my problem is i don't feel mine is right way that u have to make many public class *method* for every combobx...
C#
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
             load1();
            load2();
        }
        SqlConnection cn = new SqlConnection("Data Source=.;Initial Catalog=datatest;Integrated Security=True");

public void load()
        {
            string ss = @"select personnam from tblcustomres ";
            //string sb = @"select items from tblitems";
            //SqlCommand cmd = new SqlCommand(@"select personnam from tblcustomres ", cn);
            SqlCommand cmd = new SqlCommand(ss,cn); 
            SqlDataReader dr;
            cn.Open();
            dr = cmd.ExecuteReader();
            while(dr.Read())
            {
                      string smd1 = (string)dr["personnam"].ToString();
                comboBox1.Items.Add(smd1);
            }
            cn.Close();
        }
        public void load1()
        {
            SqlCommand cmd = new SqlCommand(@"select items from tblitems", cn);
            SqlDataReader dr;
            cn.Open();
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {

                string smd1 = (string)dr["items"].ToString();
                comboBox2.Items.Add(smd1);

            }
            cn.Close();
public void load2()
        {
            SqlCommand cmd = new SqlCommand(@"select location from tblloc", cn);
            SqlDataReader dr;
            cn.Open();
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {

                string smd1 = (string)dr["location"].ToString();
                comboBox3.Items.Add(smd1);

            }
            cn.Close();
        }
        }

any answers well be so appreciated...
Posted

1 solution

You're right with your assumption, it's possible to do this in a better way, without repeating almost the same code for each combobox. You have to identify which parts they have in common and which parts are different. Then you replace the differing parts by variables that you then provide with method-parameters instead. I've done this for you here; also I've improved some other aspects:
- the SqlConnection in a local using-block, not as a class-member
- removed the repeated opening and closing of the connection
- the SqlCommand and SqlDataReader in a using-block
- removed unneccessary casting of the row-cell

C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=datatest;Integrated Security=True"))
        {
            conn.Open();

            FillComboBox(conn, "tblcustomres", "personnam", comboBox1);
            FillComboBox(conn, "tblitems", "items", comboBox2);
            FillComboBox(conn, "tblloc", "location", comboBox3);

        } // the connection will be closed here automatically
    }

    public void FillComboBox(SqlConnection conn, string table, string column, ComboBox comboBox)
    {
        string query = String.Format("SELECT {0} FROM {1};", column, table);

        using (SqlCommand cmd = new SqlCommand(query, conn))
        using (SqlDataReader dr = cmd.ExecuteReader())
        {
            while (dr.Read())
            {
                string item = dr[column].ToString();
                comboBox.Items.Add(item);
            }
        }
    }
}

I would also recommend you to rename your comboboxes something like "cbPerson", "cbItems", "cbLocation" and likewise, to give any other controls and variables meaningful names which will make it much easier to understand your code, even for yourself, especially when getting back to it after some weeks.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900