4
Answers

Bind data to a combo box from a table in the DB

Dinusha Gamage

Dinusha Gamage

16y
4.5k
1

Dear Friends,

How do you bind data to a combo box ? I want to load data field like Customer Code in a combo box and when you are running the apllication, It should be appeared in a dropped down list. So hw do you do this?

Help me......

Answers (4)
0
Manish Dwivedi

Manish Dwivedi

NA 8.3k 1.2m 16y
Hi,
   That DataBind() method used in web application. I thought u want to bind the drop down list in the web application. but u want to bind the combo box in the window application. I am sending you the working code for that.

private void Form1_Load(object sender, EventArgs e)

        {

            ListCustomerCode();

        }

        public DataSet ActiveCust() // Create  the data set

        {

            SqlConnection scon = new SqlConnection("server=.;database=Northwind;UID=sa;pwd=;");

            scon.Open();

            DataSet LCust = new DataSet();

            string query = "SELECT Name,City FROM tblCity";

            SqlCommand cmd = new SqlCommand(query, scon);

            SqlDataAdapter adpt = new SqlDataAdapter(cmd);

            adpt.Fill(LCust, "LostCustomer");

            scon.Close();

            return LCust;

        }

 

        private void ListCustomerCode()

        {

            DataSet Lcust = new DataSet();

            Lcust = ActiveCust();

            if (!object.Equals(Lcust, null))

            {

                comboBox1.DataSource = Lcust.Tables[0];

                comboBox1.DisplayMember = "Name";

            }

        }

Happy Coding!!
0
Dinusha Gamage

Dinusha Gamage

NA 87 0 16y

Thanks Dear, I have applied the coding you sent. Can you plz check this since there are some probs.

public LostCustInfo ActiveCust() // Create  the data set

{

SqlConnection scon = new SqlConnection(con.SetConnection());

scon.Open();

LostCustInfo LCust = new LostCustInfo();

string query = "SELECT cust_code,cust_name FROM CM_LOSTCUST WHERE active = 'Y'";

SqlCommand cmd = new SqlCommand(query, scon);

SqlDataAdapter adpt = new SqlDataAdapter(cmd);

adpt.Fill(LCust, "LostCustomer");

scon.Close();

return LCust;

}

 

private void ListCustomerCode()

{

LostCustInfo Lcust = new LostCustInfo();

Lcust = Lcust.ActiveCust();

if (!object.Equals(Lcust, null))

{

cboCustCode.DataSource = Lcust;

cboCustCode.DataBindings; // Here there is no method like this ????????

}

}

0
Dinusha Gamage

Dinusha Gamage

NA 87 0 16y
Thanks for your help, I need more clarification on this, I'll put that into forum. Hope you can guide me.
0
Manish Dwivedi

Manish Dwivedi

NA 8.3k 1.2m 16y

Hi,
    For binding the dropdownlist. write  the method which returns all the customerid, as u want. then provide this datasource to the dropdownlist, in the page load or where u want. I am posting you the sample code for this.

dataSet = new DataSet();

dataSet = "Write the method which returnd the data."

if (!object.Equals(dataSet, null))

{

dropdownSample.DataSource = dataSet;

dropdownSample.DataBind();

}

Happy Coding!!