Click here to Skip to main content
15,914,642 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
private void Form1_Load(object sender, EventArgs e)
      {
          OleDbConnection dbConn;
          OleDbCommand dbCmd;
          DataSet ds;
          OleDbDataAdapter dbAdapter;
          try
          {
              string connection;
              connection = "Provider=Microsoft.ACE.OLEDB.12.0;" +
                      "Data Source=Database.mdb ";
              dbConn = new OleDbConnection(connection);
              dbConn.Open();

              string sql = "Select * FROM Database";

              dbCmd = new OleDbCommand(sql, dbConn);

              ds = new DataSet();
              dbAdapter = new OleDbDataAdapter();

              dbAdapter.SelectCommand = dbCmd;
              dbAdapter.Fill(ds, "Database");

              comboBox1.DataSource = ds.Tables["Database"];
              comboBox1.DisplayMember = "ProductDescription";

              dbConn.Close();
          }
          catch (Exception exc)
          {
              MessageBox.Show("Error" + exc);
          }
      }
Posted
Updated 20-May-15 10:45am
v2

1 solution

In general, using a DataReader can have advantages over using a DataAdapter. But for populating a ComboBox I would recommend you to use the DataAdapter.

Some suggestion for you in the comments:

C#
private void Form1_Load(object sender, EventArgs e)
{
    comboBox1.DataSource = LoadDataTable("Database");
    comboBox1.DisplayMember = "ProductDescription";
}

// split your database-code into separate methods that don't
// access Form-members:
private void DataTable LoadDataTable(string tableName)
{
   // declare variables where you need them, not at the top of a method
   // (removed from here)

   try
   {
      string connection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database.mdb";
      string sql = "Select * FROM " + tableName;

      // put everything Db***** (Connections, Commands, DataAdapter..)
      // in using(...)-blocks:
      using (var dbConn = new OleDbConnection(connection))
      using (var dbCmd = new OleDbCommand(sql, dbConn))
      using (var dbAdapter = new OleDbDataAdapter())
      {
          // a DataAdapter will open the connection automatically for you
          // (dbConn.Open() removed)
 
          // you only need a DataTable, not a whole DataSet:
          DataTable dt = new DataTable(tableName);
                
          dbAdapter.SelectCommand = dbCmd;
          dbAdapter.Fill(dt);

          return dt;
      } 
      // the connection will be closed here automatically
      // due to the effect of the using-block
      // (conn.Close() removed)
   }
   catch (Exception exc)
   {
       MessageBox.Show("Error: " + exc);
       
       // if you catch an exception and you don't do anything to deal
       // with the error (remedy it), then re-throw it:
       throw;
   }
}

As you might have recognized, the method LoadDataTable(..) now allows you to use it for any table in your database: Just give it the name of the table as an argument and you get a DataTable returned. So you can re-use it for other ComboBoxes or completely different purposes than binding to some control.
 
Share this answer
 
v2

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