How To Populate Datagridview With MySQL Database using C#

Populate Datagridview With MySQL Database using C#. Net

populate datagridview c# from mysql
populate datagridview c# from mysql

In this tutorial,  I will teach you how to populate data in the Datagridview using MySQL Database and C#.net. This tutorial will help you enhance your knowledge about displaying the data in the Datagridview. This method will display the data that has been saved in the database from the Datagridview. See the step by step guide below.

Let’s get started:

Steps How to Populate Datagridview With MySQL Database using C#

Step 1: Create a database in the MySQL database named “productdb“.

Step 2: Create a table using the following query below.

CREATE TABLE IF NOT EXISTS `tblproduct` (
 `PRODUCTID` int(11) NOT NULL AUTO_INCREMENT,
 `MODEL` varchar(30) NOT NULL,
 `BRAND` varchar(30) NOT NULL,
 `DESCRIPTION` varchar(99) NOT NULL,
 `PRICE` double NOT NULL,
 PRIMARY KEY (`PRODUCTID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

Step 3: Insert the data using the following query below.

INSERT INTO `tblproduct` (`PRODUCTID`, `MODEL`, `BRAND`, `DESCRIPTION`, `PRICE`) VALUES
(1, 'HK-LE3212UVP', 'Sharp', 'LCD VIDEO-KARAOKE 32''''', 33298),
(2, 'HK-LE1110UVP', 'Sharp', 'LCD VIDEO-KARAOKE 19''''', 22198),
(3, '21V-FS720S', 'Sharp', 'Pure Flat TV ', 7190),
(4, 'ES-D708', 'Sharp', 'Spin Dryer 7kg ', 4998),
(5, 'ES-D958', 'Sharp', 'Spin Dryer 9.5 KG', 5698),
(6, 'SJ-DT55AS', 'Sharp', '5.4 CU.FT S/D SEMI AUTO ', 10900);

Step 4: Open Microsoft Visual Studio and create new windows form application.

Step 5: Design the Form as follows.Load Data Dtg MySQL

Step 6: Go to the solution explorer and hit the “view code” to fire the code editor.

Load Data Dtg MySQL 2

Step 7: In the code editor, initialize all the MySQL classes that are needed.

Note: Add MySQL.Data.dll as your references to access mysql  and include “using MySql.Data.MySqlClient;”  above the namespace to access mysql library.

 //initialize sql connection
 MySqlConnection con = new MySqlConnection();
 //initialize all classes
 MySqlCommand cmd = new MySqlCommand();
 MySqlDataAdapter da = new MySqlDataAdapter();
 DataTable dt = new DataTable();

Step 8: Create a connection between MySQL and C#.net.

 private void Form1_Load(object sender, EventArgs e)
 {
 //set a connection string 
 con.ConnectionString = "server=localhost;user id=root;password=;Database=productdb;";
 }

Step 9: Go back to the Form Design, double-click the button and do the following code in the click events of a button.

 private void button1_Click(object sender, EventArgs e)
 {
 string query;
 try
 {
 con.Open();
 //create a query for retrieving data in the database.
 query = "SELECT * FROM `tblproduct`";
 //initialize new Sql commands
 cmd = new MySqlCommand();
 //hold the data to be executed.
 cmd.Connection = con;
 cmd.CommandText = query;
 //initialize new Sql data adapter
 da = new MySqlDataAdapter();
 //fetching query in the database.
 da.SelectCommand = cmd;
 //initialize new datatable
 dt = new DataTable();
 //refreshes the rows in specified range in the datasource. 
 da.Fill(dt);

 
 dataGridView1.DataSource = dt;
 
 }
 catch (Exception ex)
 {
 //catching error
 MessageBox.Show(ex.Message);
 }
 finally
 {
 da.Dispose();
 con.Close();
 }
 }

For all students who need a programmer for your thesis system or anyone who needs a source code in any programming language.

You can contact me @ :
Email – [email protected]
Mobile No. – 09305235027 – TNT

But if you are looking for a tutorial on how to load data in the datagridview using C# with SQL Server you can click it here.

 

Download Source code:  click here

If you have any questions or suggestions about How to Populate Datagridview With MySQL Database using C#.net, please feel free to leave a comment below.

ABOUT PROJECTPROJECT DETAILS
Project Name :How To Populate Datagridview With MySQL Database
Project Platform :C#
Programming Language Used:C# Programming Language
Developer Name :itsourcecode.com
IDE Tool (Recommended):Visual Studio 2019
Project Type :Desktop Application
Database:MYSQL DATABASE
Upload Date and Time:August 17, 2016 – 5:00 am

Frequently Asked Questions

How does this C# DataGridView example work?

Demonstrates binding a DataGridView control to a DataTable / List / SQL Server source via ADO.NET. Common patterns: filter rows, edit cells with validation, export to Excel via interop, custom column formatting, search functionality. Foundation skill for any C# data-management capstone.

What .NET and SQL Server versions does this project require?

Most projects in this batch use C# WinForms on .NET Framework 4.5+ (the dominant stack for tutorial sites) with SQL Server 2012 Express or higher. A few newer projects use .NET 6/7/8. To run: install Visual Studio 2019 / 2022 (Community edition is free), install SQL Server Express + SSMS, open the .sln file, build, run.

How do I set up the database for this C# project?

Open SQL Server Management Studio (SSMS) and connect to your SQL Server instance (e.g. localhost\SQLEXPRESS). Right-click Databases > Restore Database OR > New Database then import the included .sql script. Update the connection string in App.config (or in code-behind) with your server name + credentials. Rebuild and run.

Can I use this C# project for a BSIT capstone or thesis?

Yes, but extend it. A bare CRUD form is too narrow for full capstone scope. Add: role-based access (admin/staff/customer login redirect), Crystal Reports or RDLC reports, dashboard with Chart controls, audit log, multi-branch support. Pair with Chapter 1-5 documentation matching your panel’s rubric.

Why am I getting ‘connection error’ or ‘object reference not set’?

Three common C# issues: (1) Connection error: SQL Server isn’t running OR connection string in App.config has wrong server name. Open SQL Server Configuration Manager + verify SQL Server (SQLEXPRESS) service is running. (2) NullReferenceException: a control reference or DB column returned NULL, add a check or use ?? operator. (3) Build error ‘The type or namespace could not be found’: missing assembly reference, add via Project > Add Reference.

Where can I find more C# projects with source code?

Browse the C# Projects hub for the full library. For other .NET stacks see VB.NET Projects (300+ Windows Forms systems). For ASP.NET WebForms see ASP.NET Projects. For BSIT capstone idea lists see 150 Best Capstone Project Ideas.

Leave a Comment