C# (SHARP) AND MYSQL DATABASE CRUD - Insert, Update, Search, Delete and Display Data On DataGridView

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 193

MAURICE MUTETI

Everything Goes Here

HOME SAMPL E PAG E

C SHARP AND MYSQL Donate

DATABASE CRUD TUTORIAL Your donation helps to


maintain the servers and
Insert Update Search Delete content creation and will be
much appreciated.
And Display Data On
DataGridView Using Visual
Studio 2010
 by admin  February 16, 2020
Search … 

C# (SHARP) AND MYSQL DATABASE


CRUD TUTORIAL Insert Update Search
Delete And Display Data On DataGridView Recent Posts

Using Visual Studio 2010 The primary reference


“MySql.Data” could not be
This tutorial shows how to create crud(create, read, update, delete) resolved because it was built
application using c#(C-Sharp) programming language. In this tutorial i against the
have shared screenshots and video tutorials that equips you with basics “.NETFramework,Version=v4.

of c# windows forms application and Mysql database. In order to 5” framework. This is a


higher version than the
understand this tutorial you need to have c# and sql knowledge. You
currently targeted framework
don’t need to be advanced programmer but basics will be enough to
“.NETFramework,Version=v4.
understand this guide. After going through this guide you will be able to
0”.
perform almost all c# crud operations. This tutorial also contains code
How to Move Selected Code
snippets for both c# and sql. Am programming in windows 7, using
Up or Down in Visual Studio
Microsoft visual studio 2010 integrated development environment (IDE)
2010
, xampp localhost( With Apache, MySQL, and PHP already configured).
To connect to Mysql database am using MySql.Data.dll (MySql .NET How To Download Mysql
Connector For Visual Studio
Connector) which can be downloaded from Mysql official website. You
C Sharp Project
will see how to add the connector in visual studio 2010 in this tutorial.
C SHARP AND MYSQL
DATABASE CRUD TUTORIAL
Insert Update Search Delete
And Display Data On
DataGridView Using Visual
Studio 2010
C SHARP AND MYSQL
DATABASE CRUD TUTORIAL
61 How To Insert Update
Search Delete And Display
Data On DataGridView
Complete Video Tutorial

Recent Comments

Table Of Contents
Archives

1. How To Insert Update Search Delete And Display Data On


February 2020
DataGridView – Demo Video (Overview) – Project Walk-Through
January 2020
2. How To Create A New Form
3. How To Display The Form At The Center Screen December 2019
4. How To Add Panel To A Form
October 2019
5. How To Change Or Set Form Background Color
September 2019
6. How To Add Labels In A Form
7. How To Add TextBoxes In A Form July 2019
8. How To Add RadioButtons On A Form
June 2019
9. How To Add CheckBoxes On A Form
10. How To Add Combobox On A Form Categories
11. How To Add Buttons On A Form
12. How To Add Table On A Form Uncategorized
13. How To Add Picturebox On A Form
14. How To Add Image Icon On A Button
15. How To Browse And Display Image On PictureBox
16. How To Resize Image To Fit PictureBox
17. How To Filter Image Types (Png, Jpg, Gif) (OpenFileDialog)
18. How To Display Image Absolute Path (OpenFileDialog)
19. How To Download Mysql Connector For Visual Studio C# Project
20. How To Add Mysql Connector To Visual Studio 2010 Project
21. How To Create New MySql Database Using PHPMyAdmin
22. How To Connect To MySql Database – Part 1
23. How To Connect To MySql Database – Part 2 .Net Framework
4.5.2 Download
24. How To Connect To MySql Database – Part 3 Change The Target
Framework
25. How To Connect To MySql Database – Part 4 – Check Connection
[XAMPP]
26. How To Connect To MySql Database – Part 5 Opening Connection
Function
27. How To Connect To MySql Database – Part 6 Closing Connection
Function
28. How To Insert TextBox Text, Value, Data Into MySql Database –
Part 1
29. How To Insert TextBox Text, Value, Data Into MySql Database –
Part 2
30. How To Obtain Only The Filename (OpenFileDialog) Insert Into Db
– Part 3
31. How To Insert Radio Button Values, Data Into MySql Database
32. How To Insert Checkbox Value, Data Into Database Using C#
33. How To Insert Combobox Selected Item, Value Into Mysql In C#
34. How To Save Or Insert Image Into Mysql Database In C#
35. How To Retrieve Data From Mysql And Display It On DataGridView
In C#
36. How To Automatically Adjust Columns Width To Fit In The
DataGridView In C#
37. How To Increase (Change Or Set) DataGridView Row Height In C#
38. How To Stretch An Image In C# DataGridView Fit Image In Column
Row Cell
39. How To Create Getter And Setter, Constructor In C# – Shortcut
40. How To Read Mysql Data And Store Them In Array List C#
41. How To Populate (Bind) Datagridview From List Of Items In C#
42. How To Move a Line(Selected Code) Up or Down in Visual Studio
43. How To Check If Datagridview Row Is Selected Or Not In C#
44. How To Get Datagridview Currently Selected Row Index On Row
click C#
45. How To Display Selected Row From Datagridview To Textboxes In
C#
46. How To Display Selected Row Image From Datagridview To
Picturebox C#
47. How To Display Selected Row Value From Datagridview To
RadioButton C#
48. How To Display Selected Row Value From Datagridview To
CheckBox C#
49. How To Display Selected Row Value From Datagridview To
ComboBox C#
50. How To clear Or Reset a combobox On Button Click In C#
51. How To clear Or Reset Radio Button On Button Click In C#
52. How To clear Or Reset Check Box On Button Click In C#
53. How To clear Or Reset Text Boxes On Button Click In C#
54. How To clear Or Reset Image On A Picturebox On Button Click In
C#
55. How To Make Only One Checkbox Checked At A Time C#
56. How To Disable Text Input [Editing] In TextBox In C#
57. How To Check If Input Fields Are Empty In C#
58. How To Delete Data From Table In MySql Using C#
59. How To Refresh Datagridview Rows After Change In MySql C#
60. How To Update Data From Table In MySql Using C#
61. How To Search Data From MySql and Filter in datagridview Table
In C#
62. Fixing (GUI) Graphical User Interface
63. Complete Video Tutorial

How To Insert Update Search Delete And


Display Data On DataGridView – Demo
Video (Overview) – Project Walk-Through

This is an overview showing complete project in action. In this video I


am demonstrating how the application works.
To insert the data to Mysql database and display on DataGridView you
need to start XAMPP Localhost. Xampp has Mysql where the data is
stored. I am storing data (Text and images) in a single database. I have
only one table created in the database. The table has different columns
where different type of data is stored. ID column is the ‘Primary Key’ and
auto-increments whenever you create or insert a new row. Images are
stored in the database as Large BLOB(Binary Large OBject).

DEMO VIDEO

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share

C# AND MYSQL DATABASE CRUD TUTORIAL #0 – How To Insert Update Search Delete And Display
Data On DataGridView – Demo Video (Overview) – Project Walk-Through
How To Create A New Form

Start Visual Studio 2010.


Select the File menu, then New, then Project.

How To Create A New Form New Project

On the right pane choose/select windows forms application.


Name the project and the solution:
CSHARPANDMYSQLDATABASECRUD
Browse to the place on your computer where you wish Visual Studio
2010
to create the directory for your solution (keeping the check box
selected).
Click okay and you will be taken to a new forms window.

How To Create A New Form – New C SHarp Form Project

New form gets created immediately and you can resize it by increasing
width and height. This can be done by dragging the edges or changing
the forms properties.
How To Create A New Form – New Form

Solution Explorer Shows the files created for your project.


How To Create A New Form – Solution Explorer

In Solution Explorer, right click On Your Project Name And Select “Open
Project In Windows Explorer”. This is only when you want to know the
location where your c# project files are stored.
How To Create A New Form – Browse Folder Location

This is the location you chose when creating new project.


How To Create A New Form – Project Location

Form1.cs
1 using System; ?

2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9
10 namespace CSHARPANDMYSQLDATABASE
11 {
12 public partial class Form1 : Form
13 {
14 public Form1()
15 {
16 InitializeComponent();
17 }
18 }
19 }

Form1.Designer.cs
1 namespace CSHARPANDMYSQLDATABASE ?

2 {
3 partial class Form1
4 {
5 /// <summary>
6 /// Required designer variable.
7 /// </summary>
8 private System.ComponentModel.IContaine
9
10 /// <summary>
11 /// Clean up any resources being used.
12 /// </summary>
13 /// <param name="disposing">true if man
14 protected override void Dispose(bool di
15 {
16 if (disposing && (components != nul
17 {
18 components.Dispose();
19 }
20 base.Dispose(disposing);
21 }
22
23 #region Windows Form Designer generated
24
25 /// <summary>
26 /// Required method for Designer suppor
27 /// the contents of this method with th
28 /// </summary>
29 private void InitializeComponent()
30 {
31 this.components = new System.Compon
32 this.AutoScaleMode = System.Windows
33 this.Text = "Form1";
34 }
35
36 #endregion
37 }
38 }

Program.cs
1 using System; ?

2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Windows.Forms;
5
6 namespace CSHARPANDMYSQLDATABASE
7 {
8 static class Program
9 {
10 /// <summary>
11 /// The main entry point for the applic
12 /// </summary>
13 [STAThread]
14 static void Main()
15 {
16 Application.EnableVisualStyles();
17 Application.SetCompatibleTextRender
18 Application.Run(new Form1());
19 }
20 }
21 }

VIDEO TUTORIAL
C# AND MYSQL DATABASE CRUD TUTOR…
TUTOR…
Watch later Share

C# AND MYSQL DATABASE CRUD TUTORIAL #1 – How To Create A New Form

How To Display The Form At The Center


Screen

By default the project might not start at the center of the screen, to start
the window in the center screen, select it, then go to properties and set
start position to center screen.
How To Display The Form At The Center Screen

When you run the project it should start at the center of the screen.
How To Display The Form At The Center Screen Positioned

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #2 – How To Display The Form At The Center Screen

How To Add Panel To A Form

Drag and drop panel from toolbox onto the form.


How To Add Panel To A Form

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR


C# AND MYSQL DATABASE CRUD TUTOR…
TUTOR…
Watch later Share

C# AND MYSQL DATABASE CRUD TUTORIAL #3 – How To Add Panel To A Form

How To Change Or Set Form Background


Color

Select the form, then go to properties and click next to “BackColor” and
choose the desired color.
How To Change Or Set Form Background Color

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #4 – How To Change Or Set Form Background Color

How To Add Labels In A Form

Drag and drop label from toolbox onto the form.


How To Add Labels In A Form

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #5 – How To Add Labels In A Form

How To Add TextBoxes In A Form

Drag and drop TextBox from toolbox onto the form.

How To Add TextBoxes On A Form

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #6 – How To Add TextBoxes On A Form

How To Add RadioButtons On A Form

Drag and drop RadioButton from toolbox onto the form.


How To Add RadioButtons On A Form

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #7 – How To Add RadioButtons On A Form

How To Add CheckBoxes On A Form

Drag and drop CheckBox from toolbox onto the form.

How To Add CheckBoxes On A Form

VIDEO TUTORIAL
C# AND MYSQL DATABASE CRUD TUTOR…
TUTOR…
Watch later Share

C# AND MYSQL DATABASE CRUD TUTORIAL #8 – How To Add CheckBoxes On A Form

How To Add Combobox On A Form

Drag and drop ComboBox from toolbox onto the form.


How To Add Combobox On A Form

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #9 – How To Add Combobox On A Form

How To Add Buttons On A Form

Drag and drop Button from toolbox onto the form.

How To Add Buttons On A Form

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #10 – How To Add Buttons On A Form

How To Add DataGridView (Table) On A


Form

Drag and drop DataGridView from toolbox onto the form.


How To Add DataGridView On A Form

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #11 – How To Add Table On A Form

How To Add Picturebox On A Form

Drag and drop PictureBox from toolbox onto the form.


How To Add Picturebox On A Form

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share

C# AND MYSQL DATABASE CRUD TUTORIAL #12 – How To Add Picturebox On A Form

How To Add Image Icon On A Button

First you need to add image icons to your project resources folder.

Right Click On your project from solution explorer, and select properties.
How To Add Image Icon On A Button – Properties

Select resources tab.

Then from Add resource dropdown, Select Add Existing File.


How To Add Image Icon On A Button – Add Existing File

Select icons from your computer and click open.


How To Add Image Icon On A Button – Select From PC

Select a button, then go to properties and click next to Image.


How To Add Image Icon On A Button

check “Project resource file” radio button and select an icon from the
list. Then click Okay to add it on the button.
How To Add Image Icon On A Button – From Resources

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #13 – How To Add Image Icon On A Button

How To Browse And Display Image On


PictureBox

Browse button opens dialog box where you can choose image from your
computer.

How To Browse And Display Image On PictureBox


Choose one image from your PC and click open.

How To Browse And Display Image On PictureBox – Dialog Box

After you click open the image is displayed on picturebox.


How To Browse And Display Image On PictureBox – Image Selected

C# SOURCE CODE
1 //Browse image from the copmputer and display ?i
2 private void button1_Click(object sender, Event
3 {
4 OpenFileDialog ofd = new OpenFileDialog();
5 //Filter Only Image with the following exte
6 ofd.Filter = "Choose Only Images|*.png;*.jp
7 //Dialog box title.
8 ofd.Title = "Choose Image";
9 //If Open Button from opened dialog box is
10 if (ofd.ShowDialog() == DialogResult.OK)
11 {
12 //Displaying image on picturebox.
13 pictureBox1.Image = Image.FromFile(ofd
14 //Displaying image path On Disabled tex
15 textBoxImagePath.Text = ofd.FileName;
16 //Getting Image Filename
17 textBoxImageName.Text = Path.GetFileNam
18 }
19 }

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share

C# AND MYSQL DATABASE CRUD TUTORIAL #14 – How To Browse And Display Image On
PictureBox

How To Resize Image To Fit PictureBox

Click on picturebox to select it. Click on small right arrow icon located at
the top right of the picturebox.
How To Resize Image To Fit PictureBox

Change Size Mode To “StretchImage”.


How To Resize Image To Fit PictureBox – Stretched

C# SOURCE CODE
1 this.pictureBox1.SizeMode = System.Windows.Forms
?

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #15 – How To Resize Image To Fit PictureBox

How To Filter Image Types (Png, Jpg, Gif)


(OpenFileDialog)

This Filters Only File of type png, jpg and gif.

C# SOURCE CODE
1 //Filter Only Image with the followin
?

2 ofd.Filter = "Choose Only Images|*.png;*.jpg;*.g

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Q
Watch later Share

C# AND MYSQL DATABASE CRUD TUTORIAL #16 – How To Filter Image Types (Png, Jpg, Gif)

How To Display Image Absolute Path


(OpenFileDialog)

C# SOURCE CODE
1 //Displaying image path On Disabled textbox. ?

2 textBoxImagePath.Text = ofd.FileName;

Image path is displayed in disabled textbox.


How To Display Image Absolute Path OpenFileDialog

C# SOURCE CODE
1 //Displaying image path On Disabled textbox. ?

2 textBoxImagePath.Text = ofd.FileName;

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #17 – How To Display Image Absolute Path
(OpenFileDialog)

How To Download Mysql Connector For


Visual Studio C# Project

Step 1. Go to https://dev.mysql.com/downloads/connector/net/

Select “.NET & Mono” from the dropdown list. Then Click Download
Button Next to Zip File.
How To Download Mysql Connector NET For Visual Studio C# Project

Step 2 – Click “No Thanks, Just Start My Download”.


How To Download Mysql Connector NET For Visual Studio C# Project Zip

Step 3 – Click Open To Open mysql connector/NET automatically with


winrar when the download completes.
How To Download Mysql Connector NET For Visual Studio C# Project Zip Download

Step 4 – Wait For The Download To Finish.

You can view the download progress by clicking the down arrow as
shown in the screenshot below. Am Using Mozilla FireFox Browser.
How To Download Mysql Connector NET For Visual Studio C# Project – Downloading

Step 5 – When the download finishes, Zip file Opens Automatically In


Winrar. This is because I chose that option before the download started.

How To Download Mysql Connector NET For Visual Studio C# Project – Download Complete
Step 6 – Open any folder to check the files inside.

How To Download Mysql Connector NET For Visual Studio C# Project – File In Winrar

Step 7 – Select The Downloaded Zip file.


How To Download Mysql Connector NET For Visual Studio C# Project – Temp Folder

Step 8 – Right Click And Select Extract To A Folder.


How To Download Mysql Connector NET For Visual Studio C# Project – Extract To A Folder Inside
Temp

Step 9 – The files are extracted to specified folder.


How To Download Mysql Connector NET For Visual Studio C# Project – Extracted To A Folder

Step 10 – If you extracted the files in temp folder, copy them to another
location because this is a temporary folder. Anything stored here gets
deleted in case you wipe your PC. (With software like CCleaner).
How To Download Mysql Connector NET For Visual Studio C# Project – Copy Folder To Desired
Location

Step 11 – Open the Folder where the files are stored.

How To Download Mysql Connector NET For Visual Studio C# Project – Open Folder From Desktop
Step 12 – Here you can see mysql.data.dll file. This is the file you add to
your integrated development environment (IDE) like Visual Studio 2010.

How To Download Mysql Connector NET For Visual Studio C# Project – NET Connector File

VIDEO TUTORIAL

How To Download Mysql Connector/NET …


Watch later Share
How To Download Mysql Connector/NET For Visual Studio C# Project

How To Add Mysql Connector To Visual


Studio 2010 Project

Right Click on project name from solution explorer.

Select add reference.


How To Display Image Absolute Path OpenFileDialog – Right Click Then Add Reference

click on browse tab.

Locate the connector folder and open it.

Select MySql.Data.Dll.

Click Ok.
How To Display Image Absolute Path OpenFileDialog – Browse Mysql Connector File

Mysql.Data is added to references section.

This means mysql connector/NET has been added in your project


successfully.
How To Display Image Absolute Path OpenFileDialog – Added To Reference

This is the folder where MySql Connector/NET is found.


How To Add Mysql Connector To Visual Studio 2010 Project – Connector Folder

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #18 – How To Add Mysql Connector To Visual Studio

How To Create New MySql Database Using


PHPMyAdmin

Start xampp control panel.

If you have shortcut click it to start xampp.

If you don’t, Start xampp from installation location.


How To Create New MySql Database Using PHPMyAdmin – Xampp Location

Start both Apache and Mysql.


How To Create New MySql Database Using PHPMyAdmin – Start Xampp

Both services are now running.


How To Create New MySql Database Using PHPMyAdmin – Start apache and mysql

Click Admin next to mysql to stat PHPMyadmin GUI. On your browser.


How To Create New MySql Database Using PHPMyAdmin – Admin Next To mysql

Click New To create new database.


How To Create New MySql Database Using PHPMyAdmin – phpmyadmin Homepage

Enter database name and click create button.


How To Create New MySql Database Using PHPMyAdmin – Enter Database Name

The database is created without any table inside.


How To Create New MySql Database Using PHPMyAdmin – Database Created

On the left pane under created database, click on new, to create new
table.

Enter table name and add required columns and their datatypes.
How To Create New MySql Database Using PHPMyAdmin – Create New Table

To check the table structure click on structure tab.


How To Create New MySql Database Using PHPMyAdmin – Table Structure

To run a query for selecting all data from the table click on “SQL” tab.

Enter query in the text area.


How To Create New MySql Database Using PHPMyAdmin – Select All

Click “GO” to run your query.


How To Create New MySql Database Using PHPMyAdmin – All Table Data Selected

SQL QUERY
1 -- phpMyAdmin SQL Dump ?

2 -- version 4.9.0.1
3 -- https://www.phpmyadmin.net/
4 --
5 -- Host: 127.0.0.1
6 -- Generation Time: Feb 20, 2020 at 02:46 PM
7 -- Server version: 10.3.15-MariaDB
8 -- PHP Version: 7.3.6
9
10 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
11 SET AUTOCOMMIT = 0;
12 START TRANSACTION;
13 SET time_zone = "+00:00";
14
15
16 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARAC
17 /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARA
18 /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLAT
19 /*!40101 SET NAMES utf8mb4 */;
20
21 --
22 -- Database: `csharpandmysqlcrudinsertupdatedel
23 --
24 CREATE DATABASE IF NOT EXISTS `csharpandmysqlcr
25 USE `csharpandmysqlcrudinsertupdatedeleteselect
26
27 -- --------------------------------------------
28
29 --
30 -- Table structure for table `csharpandmysqlcru
31 --
32
33 DROP TABLE IF EXISTS `csharpandmysqlcrudinsertu
34 CREATE TABLE IF NOT EXISTS `csharpandmysqlcrudi
35 `Id` int(11) NOT NULL AUTO_INCREMENT,
36 `FirstName` varchar(50) NOT NULL,
37 `LastName` varchar(50) NOT NULL,
38 `Email` varchar(50) NOT NULL,
39 `Gender` varchar(20) NOT NULL,
40 `Language` varchar(50) NOT NULL,
41 `Country` varchar(50) NOT NULL,
42 `ImageName` varchar(50) NOT NULL,
43 `ImagePath` varchar(255) NOT NULL,
44 `ImageBlob` longblob NOT NULL,
45 PRIMARY KEY (`Id`)
46 ) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARS
47
48 --
49 -- Dumping data for table `csharpandmysqlcrudin
50 --
51
52 COMMIT;
53
54 /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTE
55 /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACT
56 /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATIO

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share

C# AND MYSQL DATABASE CRUD TUTORIAL #19 – How To Create New MySql Database –
PHPMyAdmin

How To Connect To MySql Database – Part


1
To work with database you need to be connected.

MySQLDatabaseConnection.cs
1 using System; ?

2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using MySql.Data.MySqlClient;
6 using System.Data;
7 using System.Windows.Forms;
8
9 namespace JAVAANDMYSQLDATABASECRUD
10 {
11 public class MySQLDatabaseConnection
12 {
13 MySqlConnection connection = new MySqlC
14
15 //Function to get connection
16
17 public MySqlConnection getDatabaseConne
18 {
19 return connection;
20 }
21
22 //Open Connection function
23
24 public void openDBConnection() {
25 //Check if Connection Is Closed And
26 if (connection.State == ConnectionS
27 {
28 //MessageBox.Show("Connection O
29 connection.Open();
30 }
31 }
32
33 //Close Connection function
34
35 public void closeDBConnection()
36 {
37 //Check if Connection Is Open And C
38 if (connection.State == ConnectionS
39 {
40 //MessageBox.Show("Connection C
41 connection.Close();
42 }
43 }
44
45 }
46 }

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share

C# AND MYSQL DATABASE CRUD TUTORIAL #20 – How To Connect To MySql Database – Part 1
How To Connect To MySql Database – Part
2 .Net Framework 4.5.2 Download

To download .Net Framework 4.5.2 go to


https://www.microsoft.com/en-us/download/details.aspx?id=42642

Click Download button to begin the download process.


How To Download And Install Net Framework 4 5 2 – Official Microsoft Website

When the download finishes, run the downloaded file and follow all the
steps till .Net Framework 4.5.2 is completely installed.

How To Download And Install Net Framework 4 5 2 – Downloaded and installed

VIDEO TUTORIAL

C# AND MYSQL DATABASE #21 - How To …


Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #21 – How To Connect To MySql Database – Part 2
.Net Framework 4.5.2 Download

How To Connect To MySql Database – Part


3 Change The Target Framework

I had to change the configuration xml file targeted framework to 4.5.2


from 4.0 because i was getting this error “The primary reference
“MySql.Data” could not be resolved because it was built against the
“.NETFramework,Version=v4.5” framework. This is a higher version than
the currently targeted framework “.NETFramework,Version=v4.0”.”.
How To Change The Target Framework In
Visual Studio 2010 by updating the .csproj
Xml File

This solution fixes the above error.

I have .NETFramework Version=v4.5.2 already installed in windows 7.


Am going to point my project to this version in my settings.
How To Change The Target Framework In Visual Studio 2010 by updating the dot csproj Xml File – FW
452

To Check currently selected framework version right click on your


project then select properties.

How To Change The Target Framework In Visual Studio 2010 by updating the dot csproj Xml File –
Properties
Click on application tab and check the target framework. You can
choose different framework from the drop down list.

How To Change The Target Framework In Visual Studio 2010 by updating the dot csproj Xml File –
Target Framework

Right click on your project and select “Unload Project”.


How To Change The Target Framework In Visual Studio 2010 by updating the dot csproj Xml File –
Unloading project

Right click on unloaded project and select edit (ProjectName) .csproj.


How To Change The Target Framework In Visual Studio 2010 by updating the dot csproj Xml File –
Edit .csproj

Change The following sections to targeted framework version.


How To Change The Target Framework In Visual Studio 2010 by updating the dot csproj Xml File –
Before Changing FW

In this case am targeting .NET Framework version 4.5.2

How To Change The Target Framework In Visual Studio 2010 by updating the dot csproj Xml File –
XML File

Save your settings then right click on project name and select “Reload
Project”.
How To Change The Target Framework In Visual Studio 2010 by updating the dot csproj Xml File –
Reload Project

Click Okay When you are prompted to close open document.


How To Change The Target Framework In Visual Studio 2010 by updating the dot csproj Xml File –
Close Open Doc

Project is reloaded, And it’s available in solution explorer.


How To Change The Target Framework In Visual Studio 2010 by updating the dot csproj Xml File –
Project Reloaded
Try to run your project again and it should run without any problem.

.CSPROJ XML FILE AFTER RECTIFYING THE


ERROR.
1 <?xml version="1.0" encoding="utf-8"?> ?

2 <Project ToolsVersion="4.5.2" DefaultTargets="


3 <PropertyGroup>
4 <Configuration Condition=" '$(Configuratio
5 <Platform Condition=" '$(Platform)' == ''
6 <ProductVersion>8.0.30703</ProductVersion>
7 <SchemaVersion>2.0</SchemaVersion>
8 <ProjectGuid>{8C91C6DC-A0DD-4EA2-AA0E-5C49
9 <OutputType>WinExe</OutputType>
10 <AppDesignerFolder>Properties</AppDesigner
11 <RootNamespace>JAVAANDMYSQLDATABASECRUD</R
12 <AssemblyName>JAVAANDMYSQLDATABASECRUD</As
13 <TargetFrameworkVersion>v4.5.2</TargetFram
14 <TargetFrameworkProfile>
15 </TargetFrameworkProfile>
16 <FileAlignment>512</FileAlignment>
17 </PropertyGroup>
18 <PropertyGroup Condition=" '$(Configuration)
19 <PlatformTarget>x86</PlatformTarget>
20 <DebugSymbols>true</DebugSymbols>
21 <DebugType>full</DebugType>
22 <Optimize>false</Optimize>
23 <OutputPath>bin\Debug\</OutputPath>
24 <DefineConstants>DEBUG;TRACE</DefineConsta
25 <ErrorReport>prompt</ErrorReport>
26 <WarningLevel>4</WarningLevel>
27 <Prefer32Bit>false</Prefer32Bit>
28 </PropertyGroup>
29 <PropertyGroup Condition=" '$(Configuration)
30 <PlatformTarget>x86</PlatformTarget>
31 <DebugType>pdbonly</DebugType>
32 <Optimize>true</Optimize>
33 <OutputPath>bin\Release\</OutputPath>
34 <DefineConstants>TRACE</DefineConstants>
35 <ErrorReport>prompt</ErrorReport>
36 <WarningLevel>4</WarningLevel>
37 <Prefer32Bit>false</Prefer32Bit>
38 </PropertyGroup>
39 <PropertyGroup>
40 <StartupObject>
41 </StartupObject>
42 </PropertyGroup>
43 <ItemGroup>
44 <Reference Include="MySql.Data, Version=8
45 <SpecificVersion>False</SpecificVersion>
46 <HintPath>..\..\..\..\..\Downloads\mysql
47 </Reference>
48 <Reference Include="System" />
49 <Reference Include="System.Core" />
50 <Reference Include="System.Xml.Linq" />
51 <Reference Include="System.Data.DataSetExt
52 <Reference Include="Microsoft.CSharp" />
53 <Reference Include="System.Data" />
54 <Reference Include="System.Deployment" />
55 <Reference Include="System.Drawing" />
56 <Reference Include="System.Windows.Forms"
57 <Reference Include="System.Xml" />
58 </ItemGroup>
59 <ItemGroup>
60 <Compile Include="Form1.cs">
61 <SubType>Form</SubType>
62 </Compile>
63 <Compile Include="Form1.Designer.cs">
64 <DependentUpon>Form1.cs</DependentUpon>
65 </Compile>
66 <Compile Include="MySQLDatabaseConnection
67 <Compile Include="Person.cs" />
68 <Compile Include="PersonData.cs" />
69 <Compile Include="Program.cs" />
70 <Compile Include="Properties\AssemblyInfo
71 <EmbeddedResource Include="Form1.resx">
72 <DependentUpon>Form1.cs</DependentUpon>
73 </EmbeddedResource>
74 <EmbeddedResource Include="Properties\Reso
75 <Generator>ResXFileCodeGenerator</Genera
76 <LastGenOutput>Resources.Designer.cs</La
77 <SubType>Designer</SubType>
78 </EmbeddedResource>
79 <Compile Include="Properties\Resources.Des
80 <AutoGen>True</AutoGen>
81 <DependentUpon>Resources.resx</Dependent
82 <DesignTime>True</DesignTime>
83 </Compile>
84 <None Include="app.config" />
85 <None Include="Properties\Settings.setting
86 <Generator>SettingsSingleFileGenerator</
87 <LastGenOutput>Settings.Designer.cs</Las
88 </None>
89 <Compile Include="Properties\Settings.Desi
90 <AutoGen>True</AutoGen>
91 <DependentUpon>Settings.settings</Depend
92 <DesignTimeSharedInput>True</DesignTimeS
93 </Compile>
94 </ItemGroup>
95 <ItemGroup>
96 <None Include="Resources\x-button.png" />
97 </ItemGroup>
98 <ItemGroup>
99 <None Include="Resources\plus-1.png" />
100 </ItemGroup>
101 <ItemGroup>
102 <None Include="Resources\refresh-24.png" /
103 </ItemGroup>
104 <ItemGroup>
105 <None Include="Resources\x-button1.png" />
106 </ItemGroup>
107 <ItemGroup>
108 <None Include="Resources\clear.png" />
109 </ItemGroup>
110 <Import Project="$(MSBuildToolsPath)\Microso
111 <!-- To modify your build process, add your
112 Other similar extension points exist, s
113 <Target Name="BeforeBuild">
114 </Target>
115 <Target Name="AfterBuild">
116 </Target>
117 -->
118 </Project>

VIDEO TUTORIAL

C# AND MYSQL TUTORIAL #22 - How To …


Watch later Share

C# AND MYSQL DATABASE CRUD TUTORIAL #22 – How To Connect To MySql Database – Part 3
Changing The Target Framework
How To Connect To MySql Database – Part
4 – Check Connection [XAMPP]

Check if database connection is established.

VIDEO TUTORIAL

C# AND MYSQL TUTORIAL #23 - How To …


Watch later Share

C# AND MYSQL DATABASE CRUD TUTORIAL #23 – How To Connect To MySql Database – Part 4 –
Check Connection [XAMPP]
How To Connect To MySql Database – Part
5 Opening Connection Function

To work on database you need to open database connection.

C# SOURCE CODE
1 //Open Connection function ?

2
3 public void openDBConnection() {
4 //Check if Connection Is Closed And Open It
5 if (connection.State == ConnectionState.Clo
6 {
7 //MessageBox.Show("Connection Opened");
8 connection.Open();
9 }
10 }

VIDEO TUTORIAL

C# AND MYSQL TUTORIAL #24 - How To …


Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #24 – How To Connect To MySql Database – Part 5
Opening Connection Function

How To Connect To MySql Database – Part


6 Closing Connection Function

It’s a good practice to close database connection after running queries.

C# SOURCE CODE
1 //Close Connection function ?

2
3 public void closeDBConnection()
4 {
5 //Check if Connection Is Open And Close It
6 if (connection.State == ConnectionState.Ope
7 {
8 //MessageBox.Show("Connection Closed");
9 connection.Close();
10 }
11 }
VIDEO TUTORIAL

C# AND MYSQL TUTORIAL #25 - How To …


Watch later Share

C# AND MYSQL DATABASE CRUD TUTORIAL #25 – How To Connect To MySql Database – Part 6
Closing Connection Function

How To Insert TextBox Text, Value, Data


Into MySql Database – Part 1

The values from textboxes are inserted to database varchar columns.


VIDEO TUTORIAL

C# AND MYSQL TUTORIAL #26 - How To I…


I…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #26 – How To Insert TextBox Text Or Value Or
Data Into MySql Database – Part 1

How To Insert TextBox Text, Value, Data


Into MySql Database – Part 2

How To Insert TextBox Text Value Data Into MySql Database

VIDEO TUTORIAL
C# AND MYSQL TUTORIAL #27 - How To I…
I…
Watch later Share

C# AND MYSQL DATABASE CRUD TUTORIAL #26 – How To Insert TextBox Text Or Value Or Data Into
MySql Database – Part 1

How To Obtain Only The Filename


(OpenFileDialog) Insert Into Db – Part 3

This is extracting the file name from the full path.

C# SOURCE CODE
1 //Getting Image Filename ?

2 textBoxImageName.Text = Path.GetFileName(ofd.Fil

VIDEO TUTORIAL
C# AND MYSQL TUTORIAL #28 - How To …
Watch later Share

C# AND MYSQL DATABASE CRUD TUTORIAL #28 – How To Obtain Only The Filename
(OpenFileDialog) Insert Into Db – Part 3

How To Insert Radio Button Values, Data


Into MySql Database

Radio button values are inserted to database as varchar.


How To Insert Radio Button Values Data Into MySql Database

VIDEO TUTORIAL

C# AND MYSQL CRUD TUTORIAL #29 - H…


H…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #29 – How To Insert Radio Button Values Or Data Into
Mysql Database

How To Insert Checkbox Value, Data Into


Database Using C#

CheckBox values are saved in tables varchar column.

How To Insert Checkbox Value Data Into Database Using C


VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share

C# AND MYSQL DATABASE CRUD TUTORIAL #30 – How To Insert Checkbox Values Or Data Into
Database

How To Insert Combobox Selected Item,


Value Into Mysql In C#

Selected value from dropdown is inserted to tables varchar column.


How To Insert Combobox Selected Item Value Into Mysql In C

VIDEO TUTORIAL

C# AND MYSQL CRUD TUTORIAL #31 - H…


H…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #31 – How To Insert Combobox Selected Item Or
Value Into Mysql Database

How To Save Or Insert Image Into Mysql


Database In C#

Image from picturebox is inserted to large BLOB column.

How To Save Or Insert Image Into Mysql Database


VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share

C# AND MYSQL DATABASE CRUD TUTORIAL #32 – How To Save Or Insert Image Into Mysql
Database

How To Retrieve Data From Mysql And


Display It On DataGridView In C#

All rows are displayed In DataGridView from Mysql table when you run
the application. Also the datagridview refreshes when you : insert new
row(data), delete a row, update a row etc…..
Person.cs
1 using System; ?

2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace JAVAANDMYSQLDATABASECRUD
7 {
8 class Person
9 {
10 //Shortcut For Creating properties and
11 // Type propfull and double click Tab
12
13 private int _Id;
14 private string _FirstName;
15 private string _LastName;
16 private string _Email;
17 private string _Gender;
18 private string _Language;
19 private string _Country;
20 private string _ImageName;
21 private string _ImagePath;
22 private byte[] _ImageBlob;
23
24
25 //To create Constructor Type ctor, then
26 public Person()
27 {
28
29 }
30
31 public int Id
32 {
33 get { return _Id; }
34 set { _Id = value; }
35 }
36
37 public string FirstName
38 {
39 get { return _FirstName; }
40 set { _FirstName = value; }
41 }
42
43 public string LastName
44 {
45 get { return _LastName; }
46 set { _LastName = value; }
47 }
48
49 public string Email
50 {
51 get { return _Email; }
52 set { _Email = value; }
53 }
54
55 public string Gender
56 {
57 get { return _Gender; }
58 set { _Gender = value; }
59 }
60
61 public string Language
62 {
63 get { return _Language; }
64 set { _Language = value; }
65 }
66
67 public string Country
68 {
69 get { return _Country; }
70 set { _Country = value; }
71 }
72
73 public string ImageName
74 {
75 get { return _ImageName; }
76 set { _ImageName = value; }
77 }
78
79 public string ImagePath
80 {
81 get { return _ImagePath; }
82 set { _ImagePath = value; }
83 }
84
85 public byte[] ImageBlob
86 {
87 get { return _ImageBlob; }
88 set { _ImageBlob = value; }
89 }
90
91
92 }
93 }

PersonData.cs
1 using System; ?

2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Windows.Forms;
6 using MySql.Data.MySqlClient;
7
8 namespace JAVAANDMYSQLDATABASECRUD
9 {
10 class PersonData
11 {
12 MySQLDatabaseConnection dbCONN = new My
13 public List<Person> PersonList(string v
14 {
15
16 List<Person> personList = new List<
17 String selectFromMysqlDatabase = "S
18 MySqlCommand command;
19 MySqlDataReader reader;
20 Person person;
21
22 dbCONN.openDBConnection();
23 try
24 {
25 command = new MySqlCommand(sele
26 reader = command.ExecuteReader
27
28 while (reader.Read())
29 {
30 person = new Person();
31 person.Id = Convert.ToInt32
32 //.GetInt32(reader[0].T
33 person.FirstName = reader[1
34 person.LastName = reader[2]
35 person.Email = reader[3].To
36 person.Gender = reader[4].T
37 person.Language = reader[5]
38 person.Country = reader[6]
39 person.ImageName = reader[7
40 person.ImagePath = reader[8
41 person.ImageBlob = (byte[])
42
43 personList.Add(person);
44 }
45 }
46 catch (Exception err)
47 {
48 MessageBox.Show("Error " + err
49 }
50 finally
51 {
52 //reader.Close();
53 //command.Dispose();
54 dbCONN.closeDBConnection();
55
56 }
57
58 return personList;
59
60 }
61 }
62 }

C# SOURCE CODE
1 private void Form1_Load(object sender, EventArg
?

2 {
3 //Fill Datagridview with data from mysql da
4 refreshDatagridView("");
5
6 }
7
8 private void refreshDatagridView(string searchV
9 {
10 dataGridView1.AutoSizeColumnsMode = DataGri
11 dataGridView1.RowTemplate.Height = 45;
12
13 BindingSource bsource = new BindingSource()
14 PersonData pData = new PersonData();
15 bsource.DataSource = pData.PersonList(searc
16 dataGridView1.DataSource = bsource;
17
18 //Fit Image In DataGridView Cell
19 DataGridViewImageColumn dGVimageColumn = (D
20 dGVimageColumn.ImageLayout = DataGridViewIm
21 }
How To Retrieve Data From Mysql And Display It On DataGridView In C

VIDEO TUTORIAL

C# AND MYSQL CRUD TUTORIAL #33 - H…


H…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #33 – How To Retrieve Data From Mysql And Display
It On DataGridView

How To Automatically Adjust Columns


Width To Fit In The DataGridView In C#

This makes the column fit to the datagridview. Sometimes columns


might be less than the datagridview size. This Enable them To adjust
automatically to fill datagridview.
Automatically Adjust Columns Width To Fit In The DataGridView In C

C# SOURCE CODE
1 this.dataGridView1.AutoSizeColumnsMode = System?

VIDEO TUTORIAL

C# AND MYSQL TUTORIAL #34 - How To …


Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #34 – How To Automatically Adjust Columns Width To
Fit In The DataGridView

How To Increase (Change Or Set)


DataGridView Row Height In C#

It is important to increase or decrease the row height for clear view of


the cell content.

C# SOURCE CODE
1 dataGridView1.RowTemplate.Height = 45; ?

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #35 – How To Increase (Change Or Set)
DataGridView Row Height

How To Stretch An Image In C#


DataGridView Fit Image In Column Row
Cell

Images should be visible in full format. This is done by stretching them


to fit the cell.

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #36 – How To Stretch An Image to Fit In DataGridView
Cell

How To Create Getter And Setter,


Constructor In C# – Shortcut

Getters, setters and constructors simplify the whole coding process and
reduce the code.

C# SOURCE CODE
1 using System; ?

2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace JAVAANDMYSQLDATABASECRUD
7 {
8 class Person
9 {
10 //Shortcut For Creating properties and
11 // Type propfull and double click Tab
12
13 private int _Id;
14 private string _FirstName;
15 private string _LastName;
16 private string _Email;
17 private string _Gender;
18 private string _Language;
19 private string _Country;
20 private string _ImageName;
21 private string _ImagePath;
22 private byte[] _ImageBlob;
23
24
25 //To create Constructor Type ctor, then
26 public Person()
27 {
28
29 }
30
31 public int Id
32 {
33 get { return _Id; }
34 set { _Id = value; }
35 }
36
37 public string FirstName
38 {
39 get { return _FirstName; }
40 set { _FirstName = value; }
41 }
42
43 public string LastName
44 {
45 get { return _LastName; }
46 set { _LastName = value; }
47 }
48
49 public string Email
50 {
51 get { return _Email; }
52 set { _Email = value; }
53 }
54
55 public string Gender
56 {
57 get { return _Gender; }
58 set { _Gender = value; }
59 }
60
61 public string Language
62 {
63 get { return _Language; }
64 set { _Language = value; }
65 }
66
67 public string Country
68 {
69 get { return _Country; }
70 set { _Country = value; }
71 }
72
73 public string ImageName
74 {
75 get { return _ImageName; }
76 set { _ImageName = value; }
77 }
78
79 public string ImagePath
80 {
81 get { return _ImagePath; }
82 set { _ImagePath = value; }
83 }
84
85 public byte[] ImageBlob
86 {
87 get { return _ImageBlob; }
88 set { _ImageBlob = value; }
89 }
90
91
92 }
93 }

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share

C# AND MYSQL DATABASE CRUD TUTORIAL #37 – How To Create Getters Setters And Constructor
– Shortcut
How To Read Mysql Data And Store Them
In Array List C#

All rows from mysql database are stored in an arraylist function.

C# SOURCE CODE
1 using System; ?

2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Windows.Forms;
6 using MySql.Data.MySqlClient;
7
8 namespace JAVAANDMYSQLDATABASECRUD
9 {
10 class PersonData
11 {
12 MySQLDatabaseConnection dbCONN = new My
13 public List<Person> PersonList(string v
14 {
15
16 List<Person> personList = new List<
17 String selectFromMysqlDatabase = "S
18 MySqlCommand command;
19 MySqlDataReader reader;
20 Person person;
21
22 dbCONN.openDBConnection();
23 try
24 {
25 command = new MySqlCommand(sele
26 reader = command.ExecuteReader
27
28 while (reader.Read())
29 {
30 person = new Person();
31 person.Id = Convert.ToInt32
32 //.GetInt32(reader[0].T
33 person.FirstName = reader[1
34 person.LastName = reader[2]
35 person.Email = reader[3].To
36 person.Gender = reader[4].T
37 person.Language = reader[5]
38 person.Country = reader[6]
39 person.ImageName = reader[7
40 person.ImagePath = reader[8
41 person.ImageBlob = (byte[])
42
43 personList.Add(person);
44 }
45 }
46 catch (Exception err)
47 {
48 MessageBox.Show("Error " + err
49 }
50 finally
51 {
52 //reader.Close();
53 //command.Dispose();
54 dbCONN.closeDBConnection();
55
56 }
57
58 return personList;
59
60 }
61 }
62 }
VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share

C# AND MYSQL DATABASE CRUD TUTORIAL #38 – How To Read Mysql Rows(Data) And Store
Them In Array List

How To Populate (Bind) Datagridview From


List Of Items In C#

C# SOURCE CODE
1 private void refreshDatagridView(string searchV
?

2 {
3 dataGridView1.AutoSizeColumnsMode = DataGri
4 dataGridView1.RowTemplate.Height = 45;
5
6 BindingSource bsource = new BindingSource()
7 PersonData pData = new PersonData();
8 bsource.DataSource = pData.PersonList(searc
9 dataGridView1.DataSource = bsource;
10
11 //Fit Image In DataGridView Cell
12 DataGridViewImageColumn dGVimageColumn = (D
13 dGVimageColumn.ImageLayout = DataGridViewIm
14 }

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share

C# AND MYSQL DATABASE CRUD TUTORIAL #39 – How To Populate (Bind) Datagridview From List
Of Items
How To Move a Line(Selected Code) Up or
Down in Visual Studio

This involves moving the selected line of code up or down using


keyboard shortcut. To do this you need to add appropriate plugin to your
IDE (Visual Studio 2010).

You can easily move lines up and down using MoveLine Extension.
Download Link – https://marketplace.visualstudio.com/items?
itemName=KevinAenmey.MoveLine
How to Move Selected Code Up or Down in Visual Studio – Homepage

To Download “Move Line Extension Using


Visual Studio 2010” :

Step 1. Go Tools
How to Move Selected Code Up or Down in Visual Studio – Tools

Step 2. Choose Extension Manager.


How to Move Selected Code Up or Down in Visual Studio – Extension Manager

Step 3. From the left pane select “Online Gallery”.


How to Move Selected Code Up or Down in Visual Studio – Online gallery

Step 4. Type The name of the extension in the search box. In this Case
“MoveLine”;
How to Move Selected Code Up or Down in Visual Studio – Search MoveLine

Step 5. Then Click Download.


How to Move Selected Code Up or Down in Visual Studio – Install MoveLine

Step 6. When The Download Finishes, restart your IDE.


How to Move Selected Code Up or Down in Visual Studio – Restart IDE Visual Studio 2010

If you go back to extension manager, you will see that the extension has
been added.
How to Move Selected Code Up or Down in Visual Studio – Installed

To move single line or selected block of code (Multiple Lines) use the
keyboard shortcuts below.

The default shortcut keys are as follows:

Alt+Up Arrow – Moves a line up


Alt+Down Arrow – Moves a line down

To change the shortcut keys:


In the Visual Studio main menu go to Tools -> Options
Expand Environment
Select Keyboard
In the Show commands containing box type Tools.MoveLineUp
orTools.MoveLineDown
In the Press shortcut keys box press the new shortcut keys
Click Assign

VIDEO TUTORIAL

C# AND MYSQL DATABASE TUTORIAL #4…


#4…
Watch later Share

C# AND MYSQL DATABASE CRUD TUTORIAL #40 – How to Move a Line(Selected Code) Up or Down
in Visual Studio
How To Check If Datagridview Row Is
Selected Or Not In C#

It’s good to check if a row is selected before performing a task on it.


Failure to that results in unwanted errors.

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share

C# AND MYSQL DATABASE CRUD TUTORIAL #41 – How To Check If Datagridview Row Is Selected
Or Not
How To Get Datagridview Currently
Selected Row Index On Row click C#

Selected row has a unique index starting from 0.

VIDEO TUTORIAL

C# AND MYSQL CRUD TUTORIAL #42 - H…


H…
Watch later Share

C# AND MYSQL DATABASE CRUD TUTORIAL #42 – How To Get Datagridview Currently Selected
Row Index On Row click
How To Display Selected Row From
Datagridview To Textboxes In C#

Selected row values are displayed in textboxes when you click on


datagridview row.

C# SOURCE CODE
1 private void dataGridView1_CellClick(object sen
?

2 {
3 //Check If there is a row selected
4
5 if (dataGridView1.SelectedRows.Count > 0)
6 {
7 selectedRowIndex = e.RowIndex;
8 // Selected Row
9 //MessageBox.Show("Selected Row Index >
10
11 DataGridViewRow selectedDGVRow = dataGr
12 //Diplaying Selected DataGridView row d
13 textBoxID.Text = selectedDGVRow.Cells[0
14 textBoxFirstName.Text = selectedDGVRow
15 textBoxLastName.Text = selectedDGVRow.C
16 textBoxEmail.Text = selectedDGVRow.Cell
17 //Check if datagridview cell value matc
18 if (selectedDGVRow.Cells[4].Value.ToStr
19 {
20 //Diplaying Selected DataGridView r
21 radioButtonMale.Checked = true;
22 }
23 else
24 if (selectedDGVRow.Cells[4].Value.T
25 {
26 //Diplaying Selected DataGridVi
27 radioButtonFemale.Checked = tru
28 }
29 if (selectedDGVRow.Cells[5].Value.ToStr
30 {
31 //Diplaying Selected DataGridView r
32 checkBoxEnglish.Checked = true;
33 }
34 else
35 if (selectedDGVRow.Cells[5].Value.T
36 {
37 //Diplaying Selected DataGridVi
38 checkBoxArabic.Checked = true;
39 }
40
41 comboBoxCountry.Text = selectedDGVRow.C
42 textBoxImageName.Text = selectedDGVRow
43 textBoxImagePath.Text = selectedDGVRow
44 //Diplaying Selected DataGridView row d
45 byte[] imgB = (byte[])selectedDGVRow.Ce
46 MemoryStream ms = new MemoryStream(imgB
47 pictureBox1.Image = Image.FromStream(ms
48 }
49 else
50 {
51
52 //No Row Selected
53 }
54 }
How To Get Datagridview Currently Selected Row Index On Row click C

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #43 – How To Display Selected Row From
Datagridview Into Textboxes

How To Display Selected Row Image From


Datagridview To Picturebox C#

The image from datagridview is displayed in picturebox when you click


on datagridview row.

C# SOURCE CODE
1 private void dataGridView1_CellClick(object sen
?

2 {
3 //Check If there is a row selected
4
5 if (dataGridView1.SelectedRows.Count > 0)
6 {
7 selectedRowIndex = e.RowIndex;
8 // Selected Row
9 //MessageBox.Show("Selected Row Index >
10
11 DataGridViewRow selectedDGVRow = dataGr
12 //Diplaying Selected DataGridView row d
13 textBoxID.Text = selectedDGVRow.Cells[0
14 textBoxFirstName.Text = selectedDGVRow
15 textBoxLastName.Text = selectedDGVRow.C
16 textBoxEmail.Text = selectedDGVRow.Cell
17 //Check if datagridview cell value matc
18 if (selectedDGVRow.Cells[4].Value.ToStr
19 {
20 //Diplaying Selected DataGridView r
21 radioButtonMale.Checked = true;
22 }
23 else
24 if (selectedDGVRow.Cells[4].Value.T
25 {
26 //Diplaying Selected DataGridVi
27 radioButtonFemale.Checked = tru
28 }
29 if (selectedDGVRow.Cells[5].Value.ToStr
30 {
31 //Diplaying Selected DataGridView r
32 checkBoxEnglish.Checked = true;
33 }
34 else
35 if (selectedDGVRow.Cells[5].Value.T
36 {
37 //Diplaying Selected DataGridVi
38 checkBoxArabic.Checked = true;
39 }
40
41 comboBoxCountry.Text = selectedDGVRow.C
42 textBoxImageName.Text = selectedDGVRow
43 textBoxImagePath.Text = selectedDGVRow
44 //Diplaying Selected DataGridView row d
45 byte[] imgB = (byte[])selectedDGVRow.Ce
46 MemoryStream ms = new MemoryStream(imgB
47 pictureBox1.Image = Image.FromStream(ms
48 }
49 else
50 {
51
52 //No Row Selected
53 }
54 }
How To Display Selected Row Image From Datagridview To Picturebox C

VIDEO TUTORIAL

C# AND MYSQL CRUD TUTORIAL #44 - H…


H…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #44 – How To Display Selected Image From
Datagridview Row To Picturebox

How To Display Selected Row Value From


Datagridview To RadioButton C#

Radio button gets selected when you click on datagridview row with the
value that matches radio button value.

C# SOURCE CODE
1 private void dataGridView1_CellClick(object sen
?

2 {
3 //Check If there is a row selected
4
5 if (dataGridView1.SelectedRows.Count > 0)
6 {
7 selectedRowIndex = e.RowIndex;
8 // Selected Row
9 //MessageBox.Show("Selected Row Index >
10
11 DataGridViewRow selectedDGVRow = dataGr
12 //Diplaying Selected DataGridView row d
13 textBoxID.Text = selectedDGVRow.Cells[0
14 textBoxFirstName.Text = selectedDGVRow
15 textBoxLastName.Text = selectedDGVRow.C
16 textBoxEmail.Text = selectedDGVRow.Cell
17 //Check if datagridview cell value matc
18 if (selectedDGVRow.Cells[4].Value.ToStr
19 {
20 //Diplaying Selected DataGridView r
21 radioButtonMale.Checked = true;
22 }
23 else
24 if (selectedDGVRow.Cells[4].Value.T
25 {
26 //Diplaying Selected DataGridVi
27 radioButtonFemale.Checked = tru
28 }
29 if (selectedDGVRow.Cells[5].Value.ToStr
30 {
31 //Diplaying Selected DataGridView r
32 checkBoxEnglish.Checked = true;
33 }
34 else
35 if (selectedDGVRow.Cells[5].Value.T
36 {
37 //Diplaying Selected DataGridVi
38 checkBoxArabic.Checked = true;
39 }
40
41 comboBoxCountry.Text = selectedDGVRow.C
42 textBoxImageName.Text = selectedDGVRow
43 textBoxImagePath.Text = selectedDGVRow
44 //Diplaying Selected DataGridView row d
45 byte[] imgB = (byte[])selectedDGVRow.Ce
46 MemoryStream ms = new MemoryStream(imgB
47 pictureBox1.Image = Image.FromStream(ms
48 }
49 else
50 {
51
52 //No Row Selected
53 }
54 }
How To Display Selected Row Value From Datagridview To RadioButton C

VIDEO TUTORIAL

C# AND MYSQL CRUD TUTORIAL #45 - H…


H…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #45 – How To Display Selected Row Value From
Datagridview To RadioButton

How To Display Selected Row Value From


Datagridview To CheckBox C#

CheckBox gets checked when you click on datagridview row with the
value that matches CheckBox value.

C# SOURCE CODE
1 private void dataGridView1_CellClick(object sen
?

2 {
3 //Check If there is a row selected
4
5 if (dataGridView1.SelectedRows.Count > 0)
6 {
7 selectedRowIndex = e.RowIndex;
8 // Selected Row
9 //MessageBox.Show("Selected Row Index >
10
11 DataGridViewRow selectedDGVRow = dataGr
12 //Diplaying Selected DataGridView row d
13 textBoxID.Text = selectedDGVRow.Cells[0
14 textBoxFirstName.Text = selectedDGVRow
15 textBoxLastName.Text = selectedDGVRow.C
16 textBoxEmail.Text = selectedDGVRow.Cell
17 //Check if datagridview cell value matc
18 if (selectedDGVRow.Cells[4].Value.ToStr
19 {
20 //Diplaying Selected DataGridView r
21 radioButtonMale.Checked = true;
22 }
23 else
24 if (selectedDGVRow.Cells[4].Value.T
25 {
26 //Diplaying Selected DataGridVi
27 radioButtonFemale.Checked = tru
28 }
29 if (selectedDGVRow.Cells[5].Value.ToStr
30 {
31 //Diplaying Selected DataGridView r
32 checkBoxEnglish.Checked = true;
33 }
34 else
35 if (selectedDGVRow.Cells[5].Value.T
36 {
37 //Diplaying Selected DataGridVi
38 checkBoxArabic.Checked = true;
39 }
40
41 comboBoxCountry.Text = selectedDGVRow.C
42 textBoxImageName.Text = selectedDGVRow
43 textBoxImagePath.Text = selectedDGVRow
44 //Diplaying Selected DataGridView row d
45 byte[] imgB = (byte[])selectedDGVRow.Ce
46 MemoryStream ms = new MemoryStream(imgB
47 pictureBox1.Image = Image.FromStream(ms
48 }
49 else
50 {
51
52 //No Row Selected
53 }
54 }
How To Display Selected Row Value From Datagridview To CheckBox C

VIDEO TUTORIAL

C# AND MYSQL CRUD TUTORIAL #46 - H…


H…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #46 – How To Display Selected Row Value From
Datagridview To CheckBox

How To Display Selected Row Value From


Datagridview To ComboBox C#

ComboBox displays the appropriate value when you click on


datagridview row.

C# SOURCE CODE
1 private void dataGridView1_CellClick(object sen
?

2 {
3 //Check If there is a row selected
4
5 if (dataGridView1.SelectedRows.Count > 0)
6 {
7 selectedRowIndex = e.RowIndex;
8 // Selected Row
9 //MessageBox.Show("Selected Row Index >
10
11 DataGridViewRow selectedDGVRow = dataGr
12 //Diplaying Selected DataGridView row d
13 textBoxID.Text = selectedDGVRow.Cells[0
14 textBoxFirstName.Text = selectedDGVRow
15 textBoxLastName.Text = selectedDGVRow.C
16 textBoxEmail.Text = selectedDGVRow.Cell
17 //Check if datagridview cell value matc
18 if (selectedDGVRow.Cells[4].Value.ToStr
19 {
20 //Diplaying Selected DataGridView r
21 radioButtonMale.Checked = true;
22 }
23 else
24 if (selectedDGVRow.Cells[4].Value.T
25 {
26 //Diplaying Selected DataGridVi
27 radioButtonFemale.Checked = tru
28 }
29 if (selectedDGVRow.Cells[5].Value.ToStr
30 {
31 //Diplaying Selected DataGridView r
32 checkBoxEnglish.Checked = true;
33 }
34 else
35 if (selectedDGVRow.Cells[5].Value.T
36 {
37 //Diplaying Selected DataGridVi
38 checkBoxArabic.Checked = true;
39 }
40
41 comboBoxCountry.Text = selectedDGVRow.C
42 textBoxImageName.Text = selectedDGVRow
43 textBoxImagePath.Text = selectedDGVRow
44 //Diplaying Selected DataGridView row d
45 byte[] imgB = (byte[])selectedDGVRow.Ce
46 MemoryStream ms = new MemoryStream(imgB
47 pictureBox1.Image = Image.FromStream(ms
48 }
49 else
50 {
51
52 //No Row Selected
53 }
54 }
How To Display Selected Row Value From Datagridview To ComboBox C

VIDEO TUTORIAL

C# AND MYSQL CRUD TUTORIAL #47 - H…


H…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #47 – How To Display Selected Row Value From
Datagridview To ComboBox

How To clear Or Reset a combobox On


Button Click In C#

Clearing combobox on button click.

How To clear Or Reset a combobox On Button Click In C


C# SOURCE CODE
1 //Resetting/Clearing Textfields Function ?

2 private void clearFields()


3 {
4 //Resetting/Clearing Textfields
5 textBoxID.Text = String.Empty;
6 textBoxFirstName.Text = String.Empty;
7 textBoxLastName.Text = String.Empty;
8 textBoxEmail.Text = String.Empty;
9 textBoxImageName.Text = String.Empty;
10 textBoxImagePath.Text = String.Empty;
11 textBoxSearch.Text = String.Empty;
12 //Clearing Picturebox
13 pictureBox1.Image = null;
14 //Clearing Combobox
15 comboBoxCountry.Text = String.Empty;
16 //Clearing/resetting Radiobuttons
17 if (radioButtonMale.Checked)
18 {
19 radioButtonMale.Checked = false;
20 }
21 else
22 if (radioButtonFemale.Checked)
23 {
24 radioButtonFemale.Checked = false;
25 }
26 //Clearing/resetting CheckBoxes
27 if (checkBoxEnglish.Checked)
28 {
29 checkBoxEnglish.Checked = false;
30 }
31 else
32 if (checkBoxArabic.Checked)
33 {
34 checkBoxArabic.Checked = false;
35 }
36
37 //Refresh Datagridview After clearing field
38 refreshDatagridView("");
39
40 }

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share

How To clear Or Reset a combobox On Button Click In C

How To clear Or Reset Radio Button On


Button Click In C#

Resetting all radio buttons on button click.


How To clear Or Reset Radio Button On Button Click In C

C# SOURCE CODE
1 //Resetting/Clearing Textfields Function ?

2 private void clearFields()


3 {
4 //Resetting/Clearing Textfields
5 textBoxID.Text = String.Empty;
6 textBoxFirstName.Text = String.Empty;
7 textBoxLastName.Text = String.Empty;
8 textBoxEmail.Text = String.Empty;
9 textBoxImageName.Text = String.Empty;
10 textBoxImagePath.Text = String.Empty;
11 textBoxSearch.Text = String.Empty;
12 //Clearing Picturebox
13 pictureBox1.Image = null;
14 //Clearing Combobox
15 comboBoxCountry.Text = String.Empty;
16 //Clearing/resetting Radiobuttons
17 if (radioButtonMale.Checked)
18 {
19 radioButtonMale.Checked = false;
20 }
21 else
22 if (radioButtonFemale.Checked)
23 {
24 radioButtonFemale.Checked = false;
25 }
26 //Clearing/resetting CheckBoxes
27 if (checkBoxEnglish.Checked)
28 {
29 checkBoxEnglish.Checked = false;
30 }
31 else
32 if (checkBoxArabic.Checked)
33 {
34 checkBoxArabic.Checked = false;
35 }
36
37 //Refresh Datagridview After clearing field
38 refreshDatagridView("");
39
40 }

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #49 – How To clear Or Reset Radio Button On Button
Click

How To clear Or Reset Check Box On


Button Click In C#

Unchecking all checkboxes on button click.


How To clear Or Reset Check Box On Button Click In C

C# SOURCE CODE
1 //Resetting/Clearing Textfields Function ?

2 private void clearFields()


3 {
4 //Resetting/Clearing Textfields
5 textBoxID.Text = String.Empty;
6 textBoxFirstName.Text = String.Empty;
7 textBoxLastName.Text = String.Empty;
8 textBoxEmail.Text = String.Empty;
9 textBoxImageName.Text = String.Empty;
10 textBoxImagePath.Text = String.Empty;
11 textBoxSearch.Text = String.Empty;
12 //Clearing Picturebox
13 pictureBox1.Image = null;
14 //Clearing Combobox
15 comboBoxCountry.Text = String.Empty;
16 //Clearing/resetting Radiobuttons
17 if (radioButtonMale.Checked)
18 {
19 radioButtonMale.Checked = false;
20 }
21 else
22 if (radioButtonFemale.Checked)
23 {
24 radioButtonFemale.Checked = false;
25 }
26 //Clearing/resetting CheckBoxes
27 if (checkBoxEnglish.Checked)
28 {
29 checkBoxEnglish.Checked = false;
30 }
31 else
32 if (checkBoxArabic.Checked)
33 {
34 checkBoxArabic.Checked = false;
35 }
36
37 //Refresh Datagridview After clearing field
38 refreshDatagridView("");
39
40 }

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #51 – How To clear Or Reset Text Boxes On Button
Click

How To clear Or Reset Text Boxes On


Button Click In C#

Clearing all textbox values on button click. Empty values are assigned to
Textbox in this scenario.
How To clear Or Reset Text Boxes On Button Click In C

C# SOURCE CODE
1 //Resetting/Clearing Textfields Function ?

2 private void clearFields()


3 {
4 //Resetting/Clearing Textfields
5 textBoxID.Text = String.Empty;
6 textBoxFirstName.Text = String.Empty;
7 textBoxLastName.Text = String.Empty;
8 textBoxEmail.Text = String.Empty;
9 textBoxImageName.Text = String.Empty;
10 textBoxImagePath.Text = String.Empty;
11 textBoxSearch.Text = String.Empty;
12 //Clearing Picturebox
13 pictureBox1.Image = null;
14 //Clearing Combobox
15 comboBoxCountry.Text = String.Empty;
16 //Clearing/resetting Radiobuttons
17 if (radioButtonMale.Checked)
18 {
19 radioButtonMale.Checked = false;
20 }
21 else
22 if (radioButtonFemale.Checked)
23 {
24 radioButtonFemale.Checked = false;
25 }
26 //Clearing/resetting CheckBoxes
27 if (checkBoxEnglish.Checked)
28 {
29 checkBoxEnglish.Checked = false;
30 }
31 else
32 if (checkBoxArabic.Checked)
33 {
34 checkBoxArabic.Checked = false;
35 }
36
37 //Refresh Datagridview After clearing field
38 refreshDatagridView("");
39
40 }

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #51 – How To clear Or Reset Text Boxes On Button
Click

How To clear Or Reset Image On A


Picturebox On Button Click In C#

Clearing image from picture box on button click. This sets value of a
picture box Image to null.
How To clear Or Reset Image On A Picturebox On Button Click In C

C# SOURCE CODE
1 //Resetting/Clearing Textfields Function ?

2 private void clearFields()


3 {
4 //Resetting/Clearing Textfields
5 textBoxID.Text = String.Empty;
6 textBoxFirstName.Text = String.Empty;
7 textBoxLastName.Text = String.Empty;
8 textBoxEmail.Text = String.Empty;
9 textBoxImageName.Text = String.Empty;
10 textBoxImagePath.Text = String.Empty;
11 textBoxSearch.Text = String.Empty;
12 //Clearing Picturebox
13 pictureBox1.Image = null;
14 //Clearing Combobox
15 comboBoxCountry.Text = String.Empty;
16 //Clearing/resetting Radiobuttons
17 if (radioButtonMale.Checked)
18 {
19 radioButtonMale.Checked = false;
20 }
21 else
22 if (radioButtonFemale.Checked)
23 {
24 radioButtonFemale.Checked = false;
25 }
26 //Clearing/resetting CheckBoxes
27 if (checkBoxEnglish.Checked)
28 {
29 checkBoxEnglish.Checked = false;
30 }
31 else
32 if (checkBoxArabic.Checked)
33 {
34 checkBoxArabic.Checked = false;
35 }
36
37 //Refresh Datagridview After clearing field
38 refreshDatagridView("");
39
40 }

VIDEO TUTORIAL

C# AND MYSQL CRUD TUTORIAL #52 - H…


H…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #52 – How To clear Or Reset Image On A Picturebox
On Button Click

How To Make Only One Checkbox Checked


At A Time C#

Uncheck one checkbox when the other is checked.

C# SOURCE CODE
1 //Resetting/Clearing Textfields Function ?

2 private void clearFields()


3 {
4 //Resetting/Clearing Textfields
5 textBoxID.Text = String.Empty;
6 textBoxFirstName.Text = String.Empty;
7 textBoxLastName.Text = String.Empty;
8 textBoxEmail.Text = String.Empty;
9 textBoxImageName.Text = String.Empty;
10 textBoxImagePath.Text = String.Empty;
11 textBoxSearch.Text = String.Empty;
12 //Clearing Picturebox
13 pictureBox1.Image = null;
14 //Clearing Combobox
15 comboBoxCountry.Text = String.Empty;
16 //Clearing/resetting Radiobuttons
17 if (radioButtonMale.Checked)
18 {
19 radioButtonMale.Checked = false;
20 }
21 else
22 if (radioButtonFemale.Checked)
23 {
24 radioButtonFemale.Checked = false;
25 }
26 //Clearing/resetting CheckBoxes
27 if (checkBoxEnglish.Checked)
28 {
29 checkBoxEnglish.Checked = false;
30 }
31 else
32 if (checkBoxArabic.Checked)
33 {
34 checkBoxArabic.Checked = false;
35 }
36
37 //Refresh Datagridview After clearing field
38 refreshDatagridView("");
39
40 }

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #53 – How To Make Only One Checkbox Checked At A
Time

How To Disable Text Input [Editing] In


TextBox In C#

Disabling text input in textbox.

Select textbox, go to properties and change “enabled” property value to


false.

This makes a textbox completely disabled. If you want to revert this


change Enabled property value to “True”.
How To Disable Text Input Editing In TextBox In C – Enable False
How To Disable Text Input Editing In TextBox In C

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #54 – How To Disable Text Input Or Editing In TextBox

How To Check If Input Fields Are Empty In


C#

Checking if input fields are empty before inserting their values to the
database. If one of the field is blank you will not be able to insert data
into the database.

C# SOURCE CODE
1 if (string.IsNullOrEmpty(textBoxFirstName.Text)?
2 {
3 //If there is one or more empty fields outpu
4 MessageBox.Show("one or more empty fields. F
5
6 }

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #55 – How To Check If Input Fields Are Empty

How To Delete Data From Table In MySql


Using C#

Removing a row from Mysql database by clicking a button. Select the


row you want to delete from the datagridview. So long as ID field has
data you will be able to delete it from the database.
How To Delete Data From Table In MySql Using C

C# SOURCE CODE
1 //Deleting data from mysql database ?

2 private void button4_Click(object sender, Event


3 {
4
5 //First Check if the textbox field is empty
6
7 if (string.IsNullOrEmpty(textBoxID.Text))
8 {
9 MessageBox.Show("Id Field Is Empty Firs
10
11 }
12 else
13 {
14 string deleteSQLQuery = "DELETE FROM `c
15 MySqlCommand deleteCommand;
16
17 try
18 {
19 //Open Database Connection
20 dbCONN.openDBConnection();
21 deleteCommand = new MySqlCommand(de
22 deleteCommand.Parameters.Add("@Id",
23
24 //Check If The Data has been delete
25 if (deleteCommand.ExecuteNonQuery()
26 {
27 MessageBox.Show("Data Deleted S
28 }
29 }
30 catch (Exception err)
31 {
32 MessageBox.Show("Err " + err);
33 }
34 finally
35 {
36 //close Database Connection
37 dbCONN.closeDBConnection();
38 }
39 //clear input fields after Deleting dat
40 clearFields();
41 //Refresh datagridview after deleting d
42 refreshDatagridView("");
43 }
44 }

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #56 – How To Delete Data From MySql Table

How To Refresh Datagridview Rows After


Change In MySql C#

Reloading the rows after any database change.


How To Refresh Datagridview Rows After Change In MySql C

C# SOURCE CODE
1 //CALLING THIS FUNCTION REFRESHES DATAGRIDVIEW?
2 private void refreshDatagridView(string
3 {
4 dataGridView1.AutoSizeColumnsMode =
5 dataGridView1.RowTemplate.Height =
6
7 BindingSource bsource = new Binding
8 PersonData pData = new PersonData()
9 bsource.DataSource = pData.PersonLi
10 dataGridView1.DataSource = bsource;
11
12 //Fit Image In DataGridView Cell
13 DataGridViewImageColumn dGVimageCol
14 dGVimageColumn.ImageLayout = DataGr
15 }

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share

C# AND MYSQL DATABASE CRUD TUTORIAL #57 – How To Refresh Datagridview Rows After
Change In MySql

How To Update Data From Table In MySql


Using C#

Updating Mysql database on button click. To update a row first select it


from the datagridview, the input fields gets populated with the selected
row data. Edit the data then click update button.

How To Update Data From Table In MySql Using C

C# SOURCE CODE
1 //Updating Mysql Database Row ?

2 private void button3_Click(object sender, Event


3 {
4
5 //First check if there is any empty field
6
7 //Check If Input Fields Are Empty before in
8 //Check if textboxes are empty
9 //Check if Combobox Field is empty
10 //Check if Picturebox Field is empty
11
12
13 if (string.IsNullOrEmpty(textBoxFirstName.T
14 {
15 //If there is one or more empty fields
16 MessageBox.Show("one or more empty fiel
17
18 }
19 else
20 {
21
22
23 string updateSQLQuery = "UPDATE `csharp
24 MySqlCommand updateCommand;
25
26 //Check Which CheckBox Is Checked
27 if (checkBoxEnglish.Checked)
28 {
29 languageCheckBox = "English";
30 }
31 else
32 if (checkBoxArabic.Checked)
33 {
34 languageCheckBox = "Arabic";
35 }
36
37 //Check Which Button Is Checked
38 if (radioButtonMale.Checked)
39 {
40 radioButtonValueGender = "Male";
41 }
42 else
43 if (radioButtonFemale.Checked)
44 {
45 radioButtonValueGender = "Femal
46 }
47 countryCombobox = comboBoxCountry.Selec
48
49 MemoryStream ms = new MemoryStream();
50 pictureBox1.Image.Save(ms, pictureBox1
51 Byte[] img = ms.ToArray();
52
53
54 try
55 {
56 //Open Connection
57 dbCONN.openDBConnection();
58 updateCommand = new MySqlCommand(up
59 updateCommand.Parameters.Add("@Firs
60 updateCommand.Parameters.Add("@Last
61 updateCommand.Parameters.Add("@Emai
62 updateCommand.Parameters.Add("@Gend
63 updateCommand.Parameters.Add("@Lang
64 updateCommand.Parameters.Add("@Coun
65 updateCommand.Parameters.Add("@Imag
66 updateCommand.Parameters.Add("@Imag
67 updateCommand.Parameters.Add("@Imag
68 updateCommand.Parameters.Add("@Id",
69
70 //check if data has been updated
71 if (updateCommand.ExecuteNonQuery()
72 {
73 MessageBox.Show("Data Updated S
74 }
75 }
76 catch (Exception err)
77 {
78 MessageBox.Show("Error " + err);
79 }
80 finally
81 {
82 //close database connection
83 dbCONN.closeDBConnection();
84 }
85 //clear input fields after updating dat
86 clearFields();
87 //Refresh datagridview to display updat
88 refreshDatagridView("");
89 }
90 }

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share

C# AND MYSQL DATABASE CRUD TUTORIAL #58 – How To Update Data From Table In MySql

How To Search Data From MySql and Filter


in datagridview Table In C#

To search for a specific data enter the keywords in the search textbox.
Datagridview displays data that matches your keyword on keypress.
How To Search Data From MySql and Filter in datagridview Table In C

C# SOURCE CODE
1 private void textBox1_KeyUp(object sender, KeyEv
?

2 {
3 string val = textBoxSearch.Text;
4 refreshDatagridView(val);
5 }

VIDEO TUTORIAL

C# AND MYSQL CRUD TUTORIAL #59 - H…


H…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #59 – How To Search Data From MySql and Filter in
datagridview Table

Fixing (GUI) Graphical User Interface

VIDEO TUTORIAL

C# AND MYSQL DATABASE CRUD TUTOR…


TUTOR…
Watch later Share
C# AND MYSQL DATABASE CRUD TUTORIAL #60 – Fixing (GUI) Graphical User Interface

Complete Video Tutorial

C SHARP AND MYSQL DATABASE CRUD …


Watch later Share

C# AND MYSQL DATABASE CRUD TUTORIAL #61 – Insert Update Search Delete And Display Data
On DataGridView Using Visual Studio 2010 Complete Video Tutorial

Tags: C# Form Application, Database (Software Genre), localhost, mysql database, XAMPP
Published by admin

View all posts by admin

Prev Next
C SHARP AND MYSQL DATABASE How To Download Mysql Connector
CRUD TUTORIAL 61 How To Insert For Visual Studio C Sharp Project
Update Search Delete And Display
Data On DataGridView Complete
Video Tutorial

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

Comment
Name *

Email *

Website

Save my name, email, and website in this browser for the next time I comment.

POST COMMENT
© Copyright 2020 – Maurice Muteti
Allium Theme by TemplateLens ⋅ Powered by WordPress

PDFmyURL.com - convert URLs, web pages or even full websites to PDF online. Easy API for developers!

You might also like