F how to show database value to ComboBox in C# ? | CodeTheta

how to show database value to ComboBox in C# ?

April 06, 2019

Put the below code in to separate class.

public DataTable Select() {
            SqlConnection conn = new SqlConnection(myconnstrng);
            DataTable dt = new DataTable();
            try {
                string sql = "SELECT * FROM tablename";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                conn.Open();
                adapter.Fill(dt);
            }
            catch (Exception ex){
            }
            finally{
                conn.Close();
            }
            return dt;
        }

Below code for form code section
private void frmDeparmentChoose_Load(object sender, EventArgs e) {
            DataTable dt = c.Select();
            comboBoxDepartmentChoose.DisplayMember = "department_name";
            comboBoxDepartmentChoose.DataSource = dt;
        }

IDE Used To Test This Code : Visual Studio.

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us.We will reply you as soon as possible.

Post a Comment

PRODUCTS

LISTS OF PRODUCTS

SERVICES

SKILLS
SOFTWARE DEVELOPMENT
WEBSITE DEVELOPMENT
WEB HOSTING
BULK SMS SERVICE
SEO SERVICE
ANDROID APPS
QR CODE / BARCODE
HARDWARE SERVICE
OUR WORK AREA