C# - How To Bind a Combobox With Mysql Database Values In C#
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 database |
Download Projects Source Code