How To Bind Combobox with values retrived from database in windows forms


Create DataBaseTable as follows:\

then take windows forms application and place a combobox and button

the write the following code under button click event

Namespaces:

using System.Data.SqlClient;

Code:

DataSet ds = new DataSet();
            SqlConnection con = new SqlConnection("Data Source=server2;Initial Catalog=MySampleDB;User ID=sa;password=123");
            SqlCommand cmd = new SqlCommand("select * from UserDetails", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            comboBox1.DataSource = ds.Tables[0];
            comboBox1.DisplayMember = "UserName";

Result:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s