Crud

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

CRUD (Create, Retrieve, Update and Delete) with Login

Application Using MySQL and Visual Studio

Contents:

1.1. Creating Database ‘userlist’


1.2. Creating New Project and Setting Up the MySQL Reference in a Project
1.3. Creating the Login Form
1.4. Coding the Login Form
1.5. Creating the CRUD Form
1.6. Coding the CRUD Form

1.1 Creating Database ‘userlist’

In your MySQL Workbench, create the database ‘userlist’ with a table named as ‘users’. This will be used later as a
reference for our application in Visual Studio.

To do this, follow the screenshot given below:

Then insert at least one (1) record.

1.2 Creating New Project and Setting Up the MySQL Reference in a Project

In your Visual Studio 2013 (or 2010), create a new Project. To do this, follow the step below:

File > New > Project > Click ‘OK’

We will just use the default name provided by Visual Studio since this will just serve as an example and reference.
Once you are done creating the new Project, we will setup the Reference to the MySQL Server. This is a reference
framework to connect with the MySQL Server from our project. Go to the Solution Explorer window found on the upper
right portion.

Then right click on the name of your project, go to

Add > Reference


A Reference Manager window will appear on the screen, similar to the screenshot below:

In the Search Assemblies found on the upper right, type in ‘MySQL’. This will display all the MySQL framework that we
can use in our project. Select the MySQL.Data (version 6.9.8.0) from the selection result provided. If there are multiple
MySQL.Data, just select one.
Then click ‘OK’.

1.3 Creating the Login Form

In your Form1, use the necessary objects to create the Login form found in your Toolbox (located on the left side of the
Visual Studio window). There are 2 Labels, 2 Textboxes and 1 Button in the form. Do not change the name of the
Textboxes for now. The result should be somehow similar to the screenshot below:

1.4 Coding the Login Form

To make our Login form functional, we need to apply the necessary codes to verify if the user credentials are correct
based from our database. Double click the LOGIN button in your form to automatically add the predefined codes for the
button.
This will redirect you to the Code View of your form. You may add the necessary codes needed. Further explanation of
each line of code will be discussed.

This is a library where all the MySQL related codes to transport and receive data will be found.

This is found below Public Class Form1.

The variable dbconnection will hold the necessary credentials needed in making the connection to our MySQL server.
The username (uid) and password will depend on what you have configured in your MySQL server.

The variable con As New MySqlConnection(dbconnection) will be the instance of the MySqlConnection class, having the
connection string enclosed in a parenthesis, in this case we use dbconnection.

The variable tbl As New DataTable will serve as our storage of data base on the results returned from the SQL
statement.

The screenshot above are found inside the Button1_Click function.

The variable query will hold our SQL statement to be passed on later to the database server. As you can see, there are ‘+’
signs. These are used to concatenate the values from the two textboxes to our SQL statement.
The variable adpt will be the instance of the MySqlDataAdapter class. This represents a set of data commands (query)
and a database connection (con) that are used to fill a dataset. (MySQL documentation, 2019).

tbl.Clear() function is used to remove any possible previous results stored in the DataTable tbl.

adpt.Fill(tbl) function is used to fill up the result set returned from the database to our DataTable tbl.

A condition is applied to check if the data provided from the user matches on one of the records in the database.

If tbl.Rows.Count = Nothing --- If the returned set is empty. This means that the data provided does
not match with the records in the database. Thus, this will return a message to the user “Invalid
Username and/or Password”.

Else --- if the data provided by the user are correct. This will show now the other form found in your
project (in this case, it uses Form1)

Me.Hide() is a function to hide the recently displayed form to the user.

Form1.Show() is a function to display another form found in the project (in this case, if your login form uses the name
Form1, and the other form created uses the name Form2, you may change it to Form2.Show() ).

Before starting/debugging the project, add another form to avoid errors. You may go back to the Solution Explorer found
on the upper right side of the Visual Studio window. Right click on the project, then go to

Add > Windows Form

The Add New Item window will appear. Just press the Add button.
This is how the result should look like if you will Start your project.

You might also like