AndrewD
AndrewD

Reputation: 171

Fill ComboBox with Access DB data

I'm using Visual Studio 2010 and C# to create a windows form with a combobox that should contain employees initials. I have spent the last few days searching through every solution I can find and I still can not get my combobox to populate.

This is what I've got as of now:

public static void FillComboBox(string Query, System.Windows.Forms.ComboBox LoggedByBox)
{
    using (var CONN = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\Documents\\Service Request Application\\bin\\Debug\\servicereq1.mdb"))
    {
        CONN.Open();
        DataTable dt = new DataTable();
        try
        {
            OleDbCommand cmd = new OleDbCommand(Query, CONN);
            OleDbDataReader myReader = cmd.ExecuteReader();
            dt.Load(myReader);
        }
        catch (OleDbException e)
        {
            Console.WriteLine(e.ToString());
            Console.ReadLine();

            return;
        }
        LoggedByBox.DataSource = dt;
        LoggedByBox.ValueMember = "ID";
        LoggedByBox.DisplayMember = "Initials";
    }
}

Then I call it when the form loads

private void Form1_Load(object sender, EventArgs e)
{
    FillComboBox("select ID, Initials from [Fixers and Testers]", LoggedByBox);
}

When I run the program, the combobox is still blank. I'm positive that my column names and table names are correct. Any suggestions?

Upvotes: 0

Views: 12014

Answers (4)

AdamMc331
AdamMc331

Reputation: 16730

Another possible solution would be to query and return a list of strings. Perhaps it may be less efficient, but it's what I used in a recent project of mine. Here's an example that would reside in a method, possibly called GetInitialsFromDatabase():

using(var conn = new MySqlConnection(connectionString)
{
    conn.Open();
    using(MySqlCommand cmd = new MySqlCommand())
    {
        cmd.Connection = conn;
        cmd.CommandText = "SELECT Initials FROM [Fixers and Testers]  WHERE [Status] ='C'";
        MySqlDataReader reader = cmd.ExecuteReader();
        while(reader.Read())
        {
            // initials is a List<String> previously defined (Assuming strings)
            initials.Add(String.Format("{0}", reader[0]));
        }
    }
    conn.Close();
}

And then return the initials List, and then in your GUI you could say:

comboBox1.DataSource = returnedList;
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;

Upvotes: 0

AndrewD
AndrewD

Reputation: 171

I finally got my ComboBox filled and I wanted to share what I changed for anyone else who stumbles across this question in their searches. After spending a bit more time searching through other questions and MSDN, I was able to come up with this.

    private void LoadComboLogged()
    {
        AppDomain.CurrentDomain.SetData("DataDirectory",@"\\prod\ServiceRequests");

        string strCon = @"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=|DataDirectory|\servicereq1.mdb";

        try
        {
            using (OleDbConnection conn = new OleDbConnection(strCon))
            {
                conn.Open();
                string strSql = "SELECT Initials FROM [Fixers and Testers] WHERE [Status] ='C'";
                OleDbDataAdapter adapter = new OleDbDataAdapter(new OleDbCommand(strSql, conn));
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                loggedByComboBox.DataSource = ds.Tables[0];
                loggedByComboBox.DisplayMember = "Initials";
                loggedByComboBox.ValueMember = "Initials";

            }
        }
        catch (Exception ex)
        {

        }
    }

I also found that I needed to call

    LoadComboLogged(); 

when I initialized my form. Without that line, the ComboBox would only show a blank dropdown list. Hope this helps someone else who runs into this problem.

Upvotes: 5

rushank shah
rushank shah

Reputation: 856

  SqlConnection con = new SqlConnection("Data Source=RUSH-PC\\RUSH;Initial Catalog=Att;Integrated Security=True");

            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("select name from userinfo", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            DataRow dr;
            dr = dt.NewRow();

            dt.Rows.InsertAt(dr, 1);
            comboBox1.DisplayMember = "name";
            comboBox1.ValueMember = "name";
            comboBox1.DataSource = dt;    
            con.Close();

This may help you... Good luck...:-)

Upvotes: 0

Bhasyakarulu Kottakota
Bhasyakarulu Kottakota

Reputation: 833

Passing control to static method causing this issue. Instead of passing control to the method make that method returns the table and within the load method load the control.

Upvotes: 0

Related Questions