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

how to show database value to ComboBox in C# ?

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);
            catch (Exception ex){
            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