ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

Populate Data in the Datagridview using MySQL Database and C#.Net

Load Data Dtg MySQL 3In 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:

 

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 a 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 8. 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 languages. You can contact me @ :
Email – jannopalacios@gmail.com
Mobile No. – 09305235027 – TNT

 

Download Source code:  click here

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!