• We just launched and are currently in beta. Join us as we build and grow the community.

Part IV: Improving of Code using Modules in Visual Basic.Net

asadsumra99

Data Exfiltration Specialist
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
89
Likes
38
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
This tutorial is a continuation of Part III: Update and Delete Specific Data in MySQL Database using Visual Basic. Net. But this time we're going to focus on how we can improve the design of our existing code using Code Refactoring. Refactoring is the process of changing a software system in such a way that it hasn't altered the external behavior of the code, yet improves its internal structure.

To start with, we need to open our last project from part three the project is so called “VBMYSQL”. Then after this on the solution explorer right click the project name->Select Add->New Item. In the Add New Item form Select “Module” and name it as “dbcon” and finally click “Add” button and this look like as shown below.

m1.png


And we're going to do it again for two times and name it as “dbselect” and “dbcrud”. After clicking the Add button, it will automatically show in the Solution Explorer and double click this “dbcon” module so that we can add our code to set for Database connection. And this bit of code.

  1. Imports

    MySql.

    Data

    .

    MySqlClient
  2. Module

    dbcon

  3. 'we declare con as our mysqlconnection because we wanted this connection will be available in all parts of our project
  4. Public

    con As

    MySqlConnection =

    jokenconn(

    )

  5. 'This time our mysqlconnection is place inside the a function name jokenconn()
  6. 'the purpose of of this is that it will always return the mysqlconnection as new mysqlconnection wherever we want to use this connection
  7. Public

    Function

    jokenconn(

    )

    As

    MySqlConnection
  8. Return

    New

    MySqlConnection(

    "server=localhost;user id=root;password=;database=test"

    )
  9. End

    Function

  10. End

    Module

Next we’re now going to set also our module for Creating, Updating and deleting of Data. To do this just simply double click the “dbcrud” module and add this code.

  1. Imports

    MySql.

    Data

    .

    MySqlClient
  2. Module

    dbcrud
  3. Dim

    result As

    Integer
  4. Dim

    cmd As

    New

    MySqlCommand
  5. Public

    con As

    MySqlConnection =

    jokenconn(

    )
  6. Public

    Sub

    jokeninsert(

    ByVal

    sql As

    String

    )
  7. Try
  8. con.

    Open

    (

    )
  9. With

    cmd
  10. .

    Connection

    =

    con
  11. .

    CommandText

    =

    sql
  12. result =

    cmd.

    ExecuteNonQuery
  13. If

    result =

    0

    Then
  14. MsgBox

    (

    "Data has been Inserted!"

    )

  15. Else
  16. MsgBox

    (

    "Successfully saved!"

    )

  17. End

    If
  18. End

    With
  19. Catch

    ex As

    Exception
  20. MsgBox

    (

    ex.

    Message

    )

  21. End

    Try
  22. con.

    Close

    (

    )

  23. End

    Sub

  24. Public

    Sub

    jokenupdate(

    ByVal

    sql As

    String

    )
  25. Try
  26. con.

    Open

    (

    )
  27. With

    cmd
  28. .

    Connection

    =

    con
  29. .

    CommandText

    =

    sql
  30. result =

    cmd.

    ExecuteNonQuery
  31. If

    result =

    0

    Then
  32. MsgBox

    (

    "No Data has been Updated!"

    )

  33. Else

  34. MsgBox

    (

    "New Data is updated succesfully!"

    )

  35. End

    If
  36. End

    With
  37. Catch

    ex As

    Exception
  38. MsgBox

    (

    ex.

    Message

    )


  39. End

    Try
  40. con.

    Close

    (

    )

  41. End

    Sub

  42. Public

    Sub

    jokendelete(

    ByVal

    sql As

    String

    )
  43. Try
  44. con.

    Open

    (

    )
  45. With

    cmd
  46. .

    Connection

    =

    con
  47. .

    CommandText

    =

    sql
  48. result =

    cmd.

    ExecuteNonQuery
  49. If

    result =

    0

    Then
  50. MsgBox

    (

    "No Data has been deleted!"

    )

  51. Else
  52. MsgBox

    (

    "Data is deleted succesfully!"

    )

  53. End

    If
  54. End

    With
  55. Catch

    ex As

    Exception
  56. MsgBox

    (

    ex.

    Message

    )

  57. End

    Try
  58. con.

    Close

    (

    )

  59. End

    Sub

  60. End

    Module

Code for dbselect module.

  1. Imports

    MySql.

    Data

    .

    MySqlClient
  2. Module

    dbselect
  3. Dim

    cmd As

    New

    MySqlCommand
  4. Dim

    da As

    New

    MySqlDataAdapter
  5. Public

    con As

    MySqlConnection =

    jokenconn(

    )

  6. Public

    Sub

    findThis(

    ByVal

    sql As

    String

    )
  7. Try
  8. con.

    Open

    (

    )
  9. With

    cmd
  10. .

    Connection

    =

    con
  11. .

    CommandText

    =

    sql
  12. End

    With

  13. Catch

    ex As

    Exception
  14. MsgBox

    (

    ex.

    Message

    )

  15. End

    Try
  16. con.

    Close

    (

    )
  17. da.

    Dispose

    (

    )
  18. End

    Sub
  19. Public

    Sub

    filltable(

    ByVal

    dtgrd As

    Object

    )
  20. Dim

    publictable As

    New

    DataTable
  21. Try
  22. da.

    SelectCommand

    =

    cmd
  23. da.

    Fill

    (

    publictable)
  24. dtgrd.

    DataSource

    =

    publictable
  25. dtgrd.

    Columns

    (

    0

    )

    .

    Visible

    =

    False
  26. ' dtgrd.Columns(1).Visible = False
  27. da.

    Dispose

    (

    )

  28. Catch

    ex As

    Exception
  29. MsgBox

    (

    ex.

    Message

    )

  30. End

    Try

  31. End

    Sub

  32. End

    Module

After setting the three modules were now going to use this module. Let's start with “Create User” button to do this double click it deletes all the codes in there except the “INSERT INTO” statement because this will be using it for our code.

And the “Create User” button will now look like as shown below.

  1. Private

    Sub

    btncreate_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    btncreate.

    Click

  2. jokeninsert(

    "INSERT INTO `test`.`users` (`user_id`, (`user_id`, `fullname`, `username`, `password`) "

    &

    _
  3. "VALUES (NULL, '"

    &

    txtfullname.

    Text

    &

    "', '"

    &

    txtusername.

    Text

    &

    "', '"

    &

    txtpassword.

    Text

    &

    "');"

    )

  4. End

    Sub

And you try this now by running your program. Then after this we will do now our Reading of Data to do this just add this code.

  1. findThis(

    "Select * from users"

    )
  2. filltable(

    dtguser)

and for “Update User” here’s the code.

  1. Private

    Sub

    btnupdate_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    btnupdate.

    Click
  2. jokenupdate(

    "UPDATE `test`.`users` SET `fullname` = '"

    &

    txtfullname.

    Text

    &

    "',`username` = '"

    &

    txtusername.

    Text

    &

    "',`password` = '"

    &

    txtpassword.

    Text

    &

    "' WHERE `users`.`user_id` ="

    &

    Val

    (

    lblid.

    Text

    )

    &

    ";"

    )
  3. Call

    btnload_Click(

    sender, e)
  4. End

    Sub

And for “Delete User” here’s the code.

  1. Private

    Sub

    btnDelete_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    btnDelete.

    Click

  2. jokendelete(

    "DELETE FROM `test`.`users` WHERE `users`.`user_id` ="

    &

    Val

    (

    lblid.

    Text

    )

    &

    ";"

    )
  3. Call

    btnload_Click(

    sender, e)

  4. End

    Sub

And now this is our code for “Manage_user” form.

  1. 'Description: This program can able to insert and read user form the MySQL Database using Visual Basic.
  2. 'Author: Joken Villanueva
  3. 'Date Created:

  4. Imports

    MySql.

    Data

    .

    MySqlClient
  5. Public

    Class

    Manage_user

  6. Private

    Sub

    btncreate_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    btncreate.

    Click

  7. jokeninsert(

    "INSERT INTO `test`.`users` (`user_id`, `fullname`, `username`, `password`) "

    &

    _
  8. "VALUES (NULL, '"

    &

    txtfullname.

    Text

    &

    "', '"

    &

    txtusername.

    Text

    &

    "', '"

    &

    txtpassword.

    Text

    &

    "');"

    )

  9. End

    Sub

  10. Private

    Sub

    btnupdate_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    btnupdate.

    Click
  11. jokenupdate(

    "UPDATE `test`.`users` SET `fullname` = '"

    &

    txtfullname.

    Text

    &

    "',`username` = '"

    &

    txtusername.

    Text

    &

    "',`password` = '"

    &

    txtpassword.

    Text

    &

    "' WHERE `users`.`user_id` ="

    &

    Val

    (

    lblid.

    Text

    )

    &

    ";"

    )
  12. Call

    btnload_Click(

    sender, e)
  13. End

    Sub

  14. Private

    Sub

    btnDelete_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    btnDelete.

    Click

  15. jokendelete(

    "DELETE FROM `test`.`users` WHERE `users`.`user_id` ="

    &

    Val

    (

    lblid.

    Text

    )

    &

    ";"

    )
  16. Call

    btnload_Click(

    sender, e)

  17. End

    Sub

  18. Private

    Sub

    btnload_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    btnload.

    Click
  19. findThis(

    "Select * from users"

    )
  20. filltable(

    dtguser)

  21. End

    Sub

  22. Private

    Sub

    dtguser_CellMouseClick(

    ByVal

    sender As

    Object

    , ByVal

    e As

    System.

    Windows

    .

    Forms

    .

    DataGridViewCellMouseEventArgs

    )

    Handles

    dtguser.

    CellMouseClick
  23. 'this code will simply pass the value from the specific row selected by the user
  24. lblid.

    Text

    =

    dtguser.

    CurrentRow

    .

    Cells

    (

    0

    )

    .

    Value
  25. txtfullname.

    Text

    =

    dtguser.

    CurrentRow

    .

    Cells

    (

    1

    )

    .

    Value
  26. txtusername.

    Text

    =

    dtguser.

    CurrentRow

    .

    Cells

    (

    2

    )

    .

    Value
  27. txtpassword.

    Text

    =

    dtguser.

    CurrentRow

    .

    Cells

    (

    3

    )

    .

    Value

  28. End

    Sub

  29. End

    Class

As we can observe there is really big different of using modules compare to use of traditional coding style. With the help of this module we can now able to do our coding 10x faster compare before. Meaning with the use of this technique we can create a system very soon and with less of bugs. Hope it can help you to improve your productivity in programming using visual Basic Modules.

 

452,292

324,135

324,143

Top