Sreenath Ganga
Sreenath Ganga

Reputation: 736

loading combobox with datasource

i want to fill a combobox with data from the database when the page load I had written the code as below

private void QuotationForm_Load(object sender, EventArgs e)
        {
            MessageBox.Show("hghjgvhg");
            comboboxload();
        }




 public void comboboxload()
        {

            OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
            oleDbConnection1.Open();
            OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("Select jobpk,jobecode from  jobcodemastertable",oleDbConnection1);
            OleDbDataReader reader = oleDbCommand1.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Columns.Add("jobpk", typeof(int));
            dt.Columns.Add("jobcode", typeof(string));
            dt.Load(reader);
            cmbjobcode.ValueMember = "jobpk";
            cmbjobcode.DisplayMember = "jobcode";
            cmbjobcode.DataSource = dt;
            oleDbConnection1.Close();  
        }

it doesnot deturns an error or exception but doesnot load the combobox with data values

Upvotes: 1

Views: 51017

Answers (4)

Manly
Manly

Reputation: 71

try this

comboBox1.DataSource = ds.Tables[0];
comboBox1.ValueMember = "id";
comboBox1.DisplayMember = "name";

Upvotes: 7

Ebad Masood
Ebad Masood

Reputation: 2379

You have to call DataBind method on your combo. Thats why its not populating.

Upvotes: 0

user1216891
user1216891

Reputation: 31

You're missing the DataBind method

dt.Load(reader);
cmbjobcode.ValueMember = "jobpk";
cmbjobcode.DisplayMember = "jobcode";
cmbjobcode.DataSource = dt;
//here
cmbjobcode.DataBind();
oleDbConnection1.Close();

Upvotes: 1

Ajeesh M
Ajeesh M

Reputation: 2092

You may need to bind datatable's view with combo box

cmbjobcode.DataSource = dt.DefaultView;

Upvotes: 3

Related Questions