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

2
155

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:
  1. Imports MySql.Data.MySqlClient
  2. Module connectionModule
  3.  
  4. Public conn As MySqlConnection
  5. Public cmd As MySqlCommand
  6. Public dr As MySqlDataAdapter
  7. Public dt As DataTable
  8. Public sql As String
  9. Public result As Integer
  10.  
  11. Public Sub dbConnection()
  12. Try
  13. conn = New MySqlConnection
  14. conn.ConnectionString = "server=localhost; userid=root; password=; database=module;"
  15. Catch ex As MySqlException
  16. MsgBox(ex.Message)
  17. End Try
  18. End Sub
  19.  
  20. End Module

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

  1. Imports MySql.Data.MySqlClient
  2. Module addModule
  3.  
  4. Public Sub addValue()
  5. Try
  6. dbConnection()
  7. sql = "INSERT INTO test (`Username`,`Password`) VALUES (@Username, @Password);"
  8. cmd = New MySqlCommand
  9. With cmd
  10. .Connection = conn
  11. .CommandText = sql
  12. .Parameters.Clear()
  13. .Parameters.AddWithValue("@Username", Form1.TextBox1.Text)
  14. .Parameters.AddWithValue("@Password", Form1.TextBox2.Text)
  15. result = .ExecuteNonQuery()
  16. End With
  17. conn.Close()
  18. Catch ex As MySqlException
  19. MsgBox(ex.Message)
  20. Finally
  21. If result = 0 Then
  22. MsgBox("Error in adding values!")
  23. Else
  24. MsgBox("Successfully added new values!")
  25. End If
  26. End Try
  27. End Sub
  28.  
  29. End Module

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

  1. Imports MySql.Data.MySqlClient
  2. Module updateModule
  3.  
  4. Public Sub updateValue()
  5. Try
  6. dbConnection()
  7. sql = "UPDATE test SET Username = @Username, Password = @Password WHERE ID = @ID;"
  8. cmd = New MySqlCommand
  9. With cmd
  10. .Connection = conn
  11. .CommandText = sql
  12. .Parameters.Clear()
  13. .Parameters.AddWithValue("@Username", Form1.TextBox1.Text)
  14. .Parameters.AddWithValue("@Password", Form1.TextBox2.Text)
  15. .Parameters.AddWithValue("@ID", Form1.Label4.Text)
  16. result = .ExecuteNonQuery()
  17. End With
  18. conn.Close()
  19. Catch ex As MySqlException
  20. MsgBox(ex.Message)
  21. Finally
  22. If result = 0 Then
  23. MsgBox("Error in updating values!")
  24. Else
  25. MsgBox("Successfully updated selected values!")
  26. End If
  27. End Try
  28. End Sub
  29.  
  30. End Module

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

  1. Imports MySql.Data.MySqlClient
  2. Module deleteModule
  3.  
  4. Public Sub deleteValue()
  5. Try
  6. dbConnection()
  7. sql = "DELETE FROM test WHERE ID = @ID;"
  8. cmd = New MySqlCommand
  9. With cmd
  10. .Connection = conn
  11. .CommandText = sql
  12. .Parameters.Clear()
  13. .Parameters.AddWithValue("@ID", Form1.Label4.Text)
  14. result = .ExecuteNonQuery()
  15. End With
  16. conn.Close()
  17. Catch ex As MySqlException
  18. MsgBox(ex.Message)
  19. Finally
  20. If result = 0 Then
  21. MsgBox("Error in deleting values!")
  22. Else
  23. MsgBox("Successfully deleted selected values!")
  24. End If
  25. End Try
  26. End Sub
  27.  
  28. End Module

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

  1. Public Sub retrieveValues()
  2. Try
  3. dbConnection()
  4. sql = "SELECT * FROM test;"
  5. cmd = New MySqlCommand(sql, conn)
  6. dr = cmd.ExecuteReader
  7. Form1.DataGridView1.Rows.Clear()
  8. While dr.Read
  9. Form1.DataGridView1.Rows.Add(dr(0), dr(1), dr(2))
  10. End While
  11. Catch ex As MySqlException
  12. MsgBox(ex.Message)
  13. Finally
  14. conn.Close()
  15. dr.Close()
  16. End Try
  17. End Sub

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.

  1. Public Class Form1
  2.  
  3. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  4. retrieveValues()
  5. End Sub
  6.  
  7. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  8. addValue()
  9. retrieveValues()
  10. End Sub
  11.  
  12. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  13. updateValue()
  14. retrieveValues()
  15. End Sub
  16.  
  17. Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
  18. deleteValue()
  19. retrieveValues()
  20. End Sub
  21. End Class

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:

 

 

Facebook Comments
(Visited 168 times, 1 visits today)

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here