Sunday 5 May 2019

Update Data from Database using Visual Basic 2012 and MySQL

In this Visual Guide, the students will learn how to update data in MySQL database using Visual Basic 2012.

This is the final part of the Visual Basic 2012 CRUD Implementation using MySQL database.

Please follow the steps below to arrive at the same output.

1.  Modify your form to add the following objects.
Requirements:
- Button Name: btn_update
Text: Update
Name: btn_close
Text: Close
- Textbox Name: txtcname_id
Visible: False



Modify gridrecords course_id Visible property to True

2. Double-click Form_Load to create a new public sub
Public Sub griddata()
        'check if gridrecords has data to be edited
        If Val(gridrecords.RowCount) < 1 Then
            MsgBox("No record to display", vbInformation + vbOKOnly, "Course Maintenance")
        Else
            'control user action to click only on course name
            If Val(gridrecords.CurrentCell.ColumnIndex) < 1 Then
                MsgBox("Your not allowed to select on this column.", vbInformation + vbOKOnly, "Course Maintenance")
            Else
                'capture data position in the grid row and column value
                Dim i = gridrecords.CurrentCellAddress.X
                Dim j = gridrecords.CurrentCellAddress.Y
                'display the value to textbox based on user click click
                txtcname.Text = gridrecords.CurrentCell.Value
                'txtcname_id.Text = gridrecords.Item(i + 1, j).Value.ToString
                'get the value of the next column which is the the course id from the database
                txtcname_id.Text = gridrecords.Item(i + 1, j).Value.ToString
                txtcname.Enabled = True
                btnupdate.Enabled = True
                btnsave.Enabled = False
                btnclose.Text = "Cancel"
            End If
        End If
    End Sub
3. Double-click gridrecords to add the following script
Select gridrecords_CellClick and gridrecords_CellContentClick
Call griddata()

Modify gridrecords course_id Visible property to False

4. Double Click btn_update to add the following script
sql = "SELECT * FROM tbl_course where c_name= '" & txtcname.Text & "'"
        Try
            dbcomm = New MySqlCommand(sql, dbconn)
            dbread = dbcomm.ExecuteReader()
            dbread.Read()
            If dbread.HasRows = True Then
                MsgBox("Course name already exists", vbCritical, "Course Maintenance")
            Else
                dbread.Close()
                'SQL query in updating data in database
                sql = "UPDATE tbl_course set c_name = '" & txtcname.Text & "' where c_id = '" & txtcname_id.Text & "'"
                Try
                    dbcomm = New MySqlCommand(sql, dbconn)
                    dbread = dbcomm.ExecuteReader()
                    dbread.Close()
                Catch ex As Exception
                    MsgBox("Error in saving to Database. Error is :" & ex.Message, vbCritical, "Course Maintenance")
                    Exit Sub
                End Try
                MsgBox("The course name successfully updated.", vbInformation, "Course Maintenance")
                txtcname.Text = ""
            End If
            dbread.Close()
        Catch ex As Exception
            MsgBox("Error in collecting data from Database. Error is :" & ex.Message, vbCritical, "Course Maintenance")
            dbread.Close()
            Exit Sub
        End Try
        btnclose.Text = "Close"
        btnsave.Text = "Add"
        btnupdate.Enabled = False
        btnsave.Enabled = True
        show_records(sql)
    End Sub

5. Create a new public sub that will clear all textbox value
Public Sub clearall()
        txtcname.Text = ""
        txtcname_id.Text = ""
    End Sub
Set txtcname_id Visible property to False

6. Double click btn_close button
Add the following script
If btnclose.Text = "Close" Then
            Me.Close()
        Else
            btnclose.Text = "Close"
            btnsave.Text = "Add"
            btnupdate.Enabled = False
            btnsave.Enabled = True
            txtcname.Enabled = False
            show_records(sql)
            clearall()
 End If

It completes our tutorial on the implementation of Insert Data, Read Data and Update Data in the database using Visual Basic 2012 and MySQL.

Visual Basic 2012 using MySQL Database Implementation

1. Installing MySQL Connector 6.9.8
2. Visual Basic 2012 Connecting MySQL Database
3. Adding Data to MySQL using Visual Basic 2012
4. Reading Data from Database in Visual Basic 2012 and MySQL 
5. Updating Data in Database in Visual Basic 2012 and MySQL

Please leave a comment if you think this article is helpful to your project. Thank you.


2 comments:

Good eve sir, may additional columns po kami kaso ung griddata po namin may error bandang tostring po, i just want to ask if how can we input properly the codes.

thank u sir hahaha i hope u can help us

Thank you for bringing your laptop over to fix it.

Post a Comment

If possible, leave a positive comment. No hate speech or elicit comments. Thank you.