Bind Combo Box with MySQL Database Value using C#

Bind Combo Box with MySQL Database Value using C#

70 Best Web Based Capstone Projects...
70 Best Web Based Capstone Projects in PHP and MySQL

In this tutorial I will show you how to populate MySQL Database Value in ComboBox using C# in a simple way. Let’s follow tutorial below.

Step 1:

Create Project And Add Reference To C# WinForms Project. (visit the link to the first tutorial on how to add reference to our C# project)

How to connect MySQL Database to C# Tutorial and Source code

Step 2:

Open MySQL Workbench, right click and create schema (new database), give database name as “sampledb” and create table in database and give a name as “information”, then create columns id, emp_id, name, designation, username and password and then insert data into table.

visit the link below on the tutorial on how to insert record in mysql and c#

Insert or Save Data into MySQL Database using C#

Bind Combo Box with MySQL Database Value using C# - Step 2
Bind Combo Box with MySQL Database Value using C# – Step 2

Step 3:

Back to the windows forms application and design ComboBox forms like this

  • txtname
Bind Combo Box with MySQL Database Value using C# - Form Design
Bind Combo Box with MySQL Database Value using C# – Form Design

Step 4:

  • double click forms to show form load events

Source code for Form Load events:

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_FULL_COURSE_WITH_MYSQL_DATABASE
{
    public partial class information : Form
    {
public information ()
        {
            InitializeComponent();
        }
        private void fillcombobox_Load(object sender, EventArgs e)
        {
            MySqlConnection con = new MySqlConnection("datasource= localhost; database=sampledb;port=3306; username = root; password= db1234"); //open connection
            con.Open();
            MySqlCommand cmd = new MySqlCommand("select name from information order by name ASC",con);
            MySqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                string name = reader.GetString("name");
                txtname.Items.Add(name);
            }
            cmd.Dispose();
            reader.Close();
            con.Close();
   }
}
}

Code Explanation:

This code is for Binding A Data from database to ComboBox Using This all information will display in combobox.

Result:

Bind Combo Box with MySQL Database Value using C# - Output
Bind Combo Box with MySQL Database Value using C# – Output

Bind Combo Box with MySQL Database Value using C# Free Download Source code

Mark Jaylo

markjaylo26@gmail.com

https://www.youtube.com/watch?v=LII8nh0MN5E

You may visit our facebook page for more information, inquiries and comments.

Hire our team to do the project.

, , ,

Post navigation