Sunday 5 May 2019

Adding Data to MySQL using VB 2012

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

1. Design your form to accept data entry.
Requirements: 
- Label         Text: Enter course name
- Textbox         Name: txtcname
- Command Button Name: btnsave
                                        Text: Add
- Form 1         Text: Demo System

2.  Double Click Add Button
Add the statement above the Public Class Form1
Imports MySql.Data.MySqlClient


Add the statement inside the Form1_Load
txtcname.Enabled = False

Add the following script inside the Private Sub of btnsave Click Event
'Basic Script
If btnsave.Text = "Add" Then
            btnsave.Text = "Save"
            txtcname.Enabled = True
            txtcname.Focus()
        Else           
            btnsave.Text = "Add"
            txtcname.Enabled = False
 End If

Insert the following statement in Else part
'Check data entered by user
            If txtcname.Text = "" Then
                MsgBox("Please fill in required data to poroess.", vbCritical, "Demo System")
            Else
                'create the SQL query to check if data exist in database
                sql = "SELECT * FROM tbl_course where cname = '" & txtcname.Text & "'"
                'Implement Try catch method to capture error when detected
                Try
                    'to establish connection given the sql query and database connection in mysql_connect
                    dbcomm = New MySqlCommand(sql, dbconn)
                    'to execute the SQL command
                    dbread = dbcomm.ExecuteReader()
                    'open connection and read data from database
                    dbread.Read()
                    'if dbread has values from database
                    If dbread.HasRows = True Then
                        MsgBox("Course name already exists in database.", vbCritical, "Course Maintenance")
                    Else
                        'close the active connection in database
                        dbread.Close()
                        'sql query for inserting data to database
                        sql = "INSERT INTO tbl_course (c_id, c_name) VALUES (NULL, '" & txtcname.Text & "')"
                        Try
                            dbcomm = New MySqlCommand(sql, dbconn)
                            dbread = dbcomm.ExecuteReader()
                            MsgBox("Course name successfully added to database.",
vbInformation, "Course Maintenance")
                            dbread.Close()
                        Catch ex As Exception
                            MsgBox("Error in saving to database. Error is :" & ex.Message,
vbCritical, "Course Maintenance")
                            Exit Sub
                        End Try
                        'clear textfield
                        txtcname.Text = ""
                    End If
                    dbread.Close()
                Catch ex As Exception
                    'to capture error so system will not crash or close abruptly
                    MsgBox("Error in collecting data from database. Error is:" & ex.Message,
vbCritical, "Course Maintenance")
                    dbread.Close()
                    Exit Sub
        End Try
            End If

3. Run the program to test the script

It detected error in the field name cname so change the SQL script into c_name
sql = "SELECT * FROM tbl_course where c_name = '" & txtcname.Text & "'"

4. Confirm localhost/phpMyAdmin to check the database if record has been added

The data was successfully added into the database.

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.

1 comments:

Thanks a lot. It's really a great help to us. 😁😎

Post a Comment

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