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


using System.Data.SqlClient;


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);
            comboBox1.DataSource = ds.Tables[0];
            comboBox1.DisplayMember = "UserName";


Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s