How to add value from database to combobox

To add data from mysql table to combobox in c#

1. add preferences for mysql , to add preferences to visual studio view this article
2.double click in form1 and write the code below
Private void Form1_load(Object sender,EventArgs e){
 
MysqlConnection conn = New MysqlConnection();
MysqlCommand comm = New MysqlCommand();
MysqlAdapter adapat = New MysqlAdapter;
MysqlDataReader reader;
string Connection = “Server=YourSeverName ; user = youruser;database = yourdatabase;”;
conn = new MysqlConnection(Connection);
conn.Open();
comm = conn.CreateCommand();
comm.CommandText  = “select column_name from tablename;”;
adapat = New MysqlDataAdapter(comm);
reader = comm.ExecuteReader();
while (reader.Read()){
string skode = reader.GetString(“Kodebarang”);
combobox1.Items.Add(skode);
 
}
conn.close()
 
}

Leave a Reply