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

Deleting Multiple Data in MySQL Database Using Visual Basic 2008

Fun

Punchline Pro
F Rep
0
0
0
Rep
0
F Vouches
0
0
0
Vouches
0
Posts
99
Likes
97
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In my last tutorial I teach you how to save multiple data in MySQL database. This time, I will teach you how to delete multiple data with a selector which is a CheckBox. With this, you can choose whatever data you want to delete and it is very useful.

Now lets begin:

I already created the MySQL Database in my first tutorial. Just click here.

1. Open the Visual Basic 2008.
2. Create a Project.
3. Set your Form just like this.

fform_0.jpg


Double click the Form and do this code above the Form_load

.
This code is for setting up the connection of MySQL Database and declaring all the variables and classes that you needed.

  1. 'set your imports
  2. Imports

    MySql.

    Data

    .

    MySqlClient
  3. Public

    Class

    Form1
  4. 'set up your connection of MySQL database
  5. Dim

    con As

    MySqlConnection =

    New

    MySqlConnection(

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

    )
  6. 'a set of COMMANDS in MySQL
  7. Dim

    cmd As

    New

    MySqlCommand
  8. 'a bridge between the database and the datatable for saving and retrieving data
  9. Dim

    da As

    New

    MySqlDataAdapter
  10. 'a specfic table in the database
  11. Dim

    dt As

    New

    DataTable
  12. 'variable string for storing value
  13. Dim

    sql As

    String
  14. 'variable integer for storing value
  15. Dim

    result As

    Integer
  16. End

    Class

After that, create a Sub procedure for retrieving data in MySQL Database. So that, you can call the SubName and put it in different places that you want.

  1. 'a Sub Procedure for retrieving data
  2. Public

    Sub

    listrecords(

    )
  3. Try
  4. 'opening the connection
  5. con.

    Open

    (

    )
  6. 'store your select query to a variable(sql)
  7. sql =

    "SELECT * FROM member"
  8. 'set a new specific table in the database
  9. dt =

    New

    DataTable
  10. 'Set your MySQL COMMANDS
  11. With

    cmd
  12. .

    Connection

    =

    con
  13. .

    CommandText

    =

    sql
  14. End

    With
  15. da =

    New

    MySqlDataAdapter(

    sql, con)
  16. 'set the MySqlDataAdapter to add or refresh rows in a specified range in the dataset
  17. 'to match those in the data source the data Table name.
  18. da.

    Fill

    (

    dt)
  19. 'to get or set the data source to diplay in the DataGridView
  20. DataGridView1.

    DataSource

    =

    dt
  21. Catch

    ex As

    Exception
  22. MsgBox

    (

    ex.

    Message

    )
  23. End

    Try
  24. 'Releases all resources used by the MySqlDataAdapter
  25. da.

    Dispose

    (

    )
  26. 'close the connection
  27. con.

    Close

    (

    )
  28. End

    Sub

Under the Form_Load

, do the following codes for adding the CheckBox column and call the SubName for retrieving the data in MySQL Database.

  1. Private

    Sub

    Form1_Load(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    MyBase

    .

    Load
  2. 'set a variable as a checkbox column in the DataGridView
  3. Dim

    chkbox As

    New

    DataGridViewCheckBoxColumn
  4. 'set the width of the column in the DataGridView
  5. With

    chkbox
  6. .

    Width

    =

    30
  7. End

    With

  8. With

    DataGridView1
  9. 'Adding the checkbox column in the DataGridView
  10. .

    Columns

    .

    Add

    (

    chkbox)
  11. 'set the rows header to invisible
  12. .

    RowHeadersVisible

    =

    False
  13. End

    With
  14. 'Put a Sub procedure name for Listing Records on the first load
  15. listrecords(

    )
  16. End

    Sub

Go back to the Design Views, double click the Delete Button. And under the Button1_Click

, do the following codes to delete the records in MySQL Database.

  1. Private

    Sub

    Button1_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    Button1.

    Click
  2. Try
  3. 'opening the connection
  4. con.

    Open

    (

    )
  5. 'DataGridViewRow represents a row in the DataGridView Control
  6. 'Rows is to get the collection of rows in the DataGridView control
  7. 'the syntax is , getting the rows one by one
  8. For

    Each

    row As

    DataGridViewRow In

    DataGridView1.

    Rows
  9. 'Cells is to get the collection of cell that populate the row
  10. 'FormattedValue is to get the value of the cell as formtted for display
  11. 'the condition is, if the checkbox is checked then it delete the data in the database
  12. If

    row.

    Cells

    (

    0

    )

    .

    FormattedValue

    =

    True

    Then
  13. 'store your delete query to a variable(sql)
  14. sql =

    "DELETE FROM member WHERE id = '"

    _
  15. &

    CStr

    (

    row.

    Cells

    (

    1

    )

    .

    FormattedValue

    )

    &

    "'"
  16. 'Set your MySQL COMMANDS
  17. With

    cmd
  18. .

    Connection

    =

    con
  19. .

    CommandText

    =

    sql
  20. End

    With
  21. 'Execute the Data
  22. result =

    cmd.

    ExecuteNonQuery
  23. End

    If
  24. Next
  25. 'the condition is, if the result is equals to zero
  26. 'then the message will appear and says "No Deleted Record."
  27. 'and if not the message will appear and says "The Record(s) has been deleted."
  28. If

    result =

    0

    Then
  29. MsgBox

    (

    "No Deleted Record."

    )
  30. Else
  31. MsgBox

    (

    "The Record(s) has been deleted."

    )
  32. End

    If
  33. Catch

    ex As

    Exception
  34. MsgBox

    (

    ex.

    Message

    )
  35. End

    Try
  36. 'Close the connection
  37. con.

    Close

    (

    )
  38. 'call the SubName for retrieving data to resfresh the list of Records in the DataGridView
  39. listrecords(

    )
  40. End

    Sub

Reminders: Read all the comments that I put , so that you will understand well what is happening and what are the functionalities in every code that I made.

You can download the complete Source Code and run it on your computer.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top