How to use Module and its uses in VB.Net with mySQL codes

How to use Module and its uses in VB.Net with mySQL codes

In this tutorial, you will learn how to use module and its uses in VB.Net with mySQL codes.

Take note, modules are not objects or building blocks like classes. Instead, they are like containers which can be useful in terms of locating your line of codes around the Solution Project easily.

To demonstrate the use of module, we will create a new project with the use of mySQL manipulation values. We will create an example of the following:

  1. Connection Module – for easy call of mySQL connection string.
  2. Add Module – for adding values in our database.
  3. Update Module – for updating values in our database.
  4. Delete Module – for deleting of values in our database.
  5. Retrieve Module – for retrieving values from our database to datagridview.

Let’s start!

I made a sample database named module with test table. This is to achieve on how to use module and its uses in VB.Net with mySQL codes.

  1. Create a module name connectionModule and insert this line of code:

[vbnet]

Imports MySql.Data.MySqlClient
Module connectionModule

Public conn As MySqlConnection
Public cmd As MySqlCommand
Public dr As MySqlDataAdapter
Public dt As DataTable
Public sql As String
Public result As Integer

Public Sub dbConnection()
Try
conn = New MySqlConnection
conn.ConnectionString = “server=localhost; userid=root; password=; database=module;”
Catch ex As MySqlException
MsgBox(ex.Message)
End Try
End Sub

End Module

[/vbnet]

2. Create a module named addModule and insert this line of code:

[vbnet]

Imports MySql.Data.MySqlClient
Module addModule

Public Sub addValue()
Try
dbConnection()
sql = “INSERT INTO test (`Username`,`Password`) VALUES (@Username, @Password);”
cmd = New MySqlCommand
With cmd
.Connection = conn
.CommandText = sql
.Parameters.Clear()
.Parameters.AddWithValue(“@Username”, Form1.TextBox1.Text)
.Parameters.AddWithValue(“@Password”, Form1.TextBox2.Text)
result = .ExecuteNonQuery()
End With
conn.Close()
Catch ex As MySqlException
MsgBox(ex.Message)
Finally
If result = 0 Then
MsgBox(“Error in adding values!”)
Else
MsgBox(“Successfully added new values!”)
End If
End Try
End Sub

End Module

[/vbnet]

3. Create a module named updateModule and insert this line of code:

[vbnet]

Imports MySql.Data.MySqlClient
Module updateModule

Public Sub updateValue()
Try
dbConnection()
sql = “UPDATE test SET Username = @Username, Password = @Password WHERE ID = @ID;”
cmd = New MySqlCommand
With cmd
.Connection = conn
.CommandText = sql
.Parameters.Clear()
.Parameters.AddWithValue(“@Username”, Form1.TextBox1.Text)
.Parameters.AddWithValue(“@Password”, Form1.TextBox2.Text)
.Parameters.AddWithValue(“@ID”, Form1.Label4.Text)
result = .ExecuteNonQuery()
End With
conn.Close()
Catch ex As MySqlException
MsgBox(ex.Message)
Finally
If result = 0 Then
MsgBox(“Error in updating values!”)
Else
MsgBox(“Successfully updated selected values!”)
End If
End Try
End Sub

End Module

[/vbnet]

4. Create a module named deleteModule and insert this line of code:

[vbnet]

Imports MySql.Data.MySqlClient
Module deleteModule

Public Sub deleteValue()
Try
dbConnection()
sql = “DELETE FROM test WHERE ID = @ID;”
cmd = New MySqlCommand
With cmd
.Connection = conn
.CommandText = sql
.Parameters.Clear()
.Parameters.AddWithValue(“@ID”, Form1.Label4.Text)
result = .ExecuteNonQuery()
End With
conn.Close()
Catch ex As MySqlException
MsgBox(ex.Message)
Finally
If result = 0 Then
MsgBox(“Error in deleting values!”)
Else
MsgBox(“Successfully deleted selected values!”)
End If
End Try
End Sub

End Module

[/vbnet]

4. Create a module named retrieveModule and insert this line of code:

[vbnet]

Public Sub retrieveValues()
Try
dbConnection()
sql = “SELECT * FROM test;”
cmd = New MySqlCommand(sql, conn)
dr = cmd.ExecuteReader
Form1.DataGridView1.Rows.Clear()
While dr.Read
Form1.DataGridView1.Rows.Add(dr(0), dr(1), dr(2))
End While
Catch ex As MySqlException
MsgBox(ex.Message)
Finally
conn.Close()
dr.Close()
End Try
End Sub

[/vbnet]

How to use module and its uses in VB.Net with mySQL codes.

After creating these modules, you can easily call them through their sub’s name. Take note, you need to public your subs in order for the code to be useful anywhere in your project. You are closing to achieve on how to use module and its uses in VB.Net with mySQL codes.

This will be the line of code inside our Form1 designer.

[vbnet]

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
retrieveValues()
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
addValue()
retrieveValues()
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
updateValue()
retrieveValues()
End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
deleteValue()
retrieveValues()
End Sub
End Class

[/vbnet]

Success! You knew about on how to use module and its uses in VB.Net with mySQL codes.

RAR Extraction Password:

Password: luffypirates

For questions or any other concerns or thesis/capstone creation with documentation, you can contact me through the following:

E-Mail: [email protected]

Facebook: facebook.com/kirk.lavapiez

Contact No.: +639771069640

To download the sample project, click here.

Ian Hero L. Lavapiez

BSIT Graduate

System Analyst and Developer

Related topic(s) that you may like:

 

 

2 thoughts on “How to use Module and its uses in VB.Net with mySQL codes”

Leave a Comment