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

Hi

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

my_con.Open();
DataTable dt = new DataTable();
OleDbCommand cmd = new OleDbCommand("select * from Batch", my_con);
OleDbDataAdapter ad = new OleDbDataAdapter(cmd);
ad.Fill(dt);
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

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))
{
    objCon.Open();
    using (objCmd = new OleDbCommand(getSql, objCon))
    {
        OleDbDataAdapter da = new OleDbDataAdapter(objCmd);
        da.Fill(dt);
    }
}
objCon.Close();
comboBox1.DataSource = dt;
comboBox1.ValueMember = "Batch_Name";
comboBox1.DisplayMember = "Batch_Name";