Populate ComboBox from MS Access Data Base using C#

Last Reply on Sep 26, 2016 07:25 AM By dharmendr

Posted on Sep 26, 2016 03:44 AM


i am useing access databasr for windows application

i have certain table which executes the result in sql view 

of an ms acces when we write the query.but it is not executing in 

C# it is showing with out error data table is empty

below is my code

DataTable dt = new DataTable();
OleDbCommand cmd = new OleDbCommand("select * from Batch", my_con);
OleDbDataAdapter ad = new OleDbDataAdapter(cmd);
comboBox1.ValueMember = "Batch_Name";
comboBox1.DisplayMember = "Batch_Name";
comboBox1.DataSource = dt;
comboBox1.Text = "select id";

so is it version problem of ms access 

please help me

You are viewing reply posted by: dharmendr on Sep 26, 2016 07:25 AM.
Posted on Sep 26, 2016 07:25 AM

Hi siddangoud,

Refer the below code.

string conStr = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" + Server.MapPath("~/database.mdb");
OleDbConnection objCon;
OleDbCommand objCmd;
DataTable dt = new DataTable();
string getSql = string.Empty;
getSql = "(SELECT * FROM Batch)";
using(objCon = new OleDbConnection(conStr))
    using (objCmd = new OleDbCommand(getSql, objCon))
        OleDbDataAdapter da = new OleDbDataAdapter(objCmd);
comboBox1.DataSource = dt;
comboBox1.ValueMember = "Batch_Name";
comboBox1.DisplayMember = "Batch_Name";