C# - How To Bind a Combobox With Mysql Database Values In C#

C# - How To Bind a Combobox With Mysql Database Values In C#


                                                                                                                                                     

In This C# Code We Will See How To Populate A ComboBox With MySQL DataBase Data In C# Programming Language.


Project Source Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace
Csharp_And_MySQL
{
    public partial class Csharp_Comobobox_values_from_mysql_database : Form
    {
        public Csharp_Comobobox_values_from_mysql_database()
        {
            InitializeComponent();
        }
        private void Csharp_Comobobox_values_from_mysql_database_Load(object sender, EventArgs e)
        {
            try
            {
                MySqlConnection connection = new MySqlConnection("Datasource=localhost;port=3306;username=root;password=");
             
                string selectQuery = "SELECT * FROM test_db.users";
                connection.Open();
                MySqlCommand command = new MySqlCommand(selectQuery, connection);
                MySqlDataReader reader = command.ExecuteReader();
                while(reader.Read())
                {
                    comboBox1.Items.Add(reader.GetString("id"));
                }
            }catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

}


=> OUTPUT:

c# combobox values from mysql
c# combobox values from mysql database




Share this

Related Posts

Previous
Next Post »