Sunday, 5 May 2019

Reading Data from Database in VB 2012 and MySQL

In this Visual Guide, the students will learn to read data from the MySQL database.

1. Add the data grid to the form
Requirements:
- Data Grid name: gridrecords
Remove check in Enable Adding, Editing and Deleting
Click Edit Columns
Click Add





Click Close after adding columns, then click OK
Set the following properties
RowHeadersVisible = False
Set Font to Tahoma Bold 12 to all CellStyle Properties

2. Now, we are ready add script that will load data coming from database.
        Double Form1
        Create a new Public Function show_records
Public Function show_records(ByVal sql As String) As Boolean
        'this will set the datagridview rows to zero
        gridrecords.RowCount = 0
        'SQL query to display all records from database
        sql = "SELECT * FROM tbl_course order by c_name"
        Try
            dbcomm = New MySqlCommand(sql, dbconn)
            dbread = dbcomm.ExecuteReader()
            'while data has record in the database
            While dbread.Read
                'This will add rows in the data grid view
                'gridrecords.Rows.Add
                'gridrecords.Rows.Add(gridrecords.rowcount + 1 this is the first column in gridrecords
                'dbread.GetValue(1) is the 2nd column. (1) is the index value from db which is c_name
                'dbread.GetValue(0) for the 3rd column. (0) is the index value from db which is c_id
                gridrecords.Rows.Add(gridrecords.RowCount + 1, dbread.GetValue(1), dbread.GetValue(0))
            End While
            'close db connection
            dbread.Close()
        Catch ex As Exception
            MsgBox("Error in collecting data from Database. Error is :" & ex.Message,
vbCritical, "Course Maintenance")
            dbread.Close()
            'this is required to return value if true or false in function
            'true if there is data to be returned
            'false no data to be returned
            Return False
            Exit Function
        End Try
        'this is required to return value if true or false in function
        'true if there is data to be returned
        'false no data to be returned
        Return True
End Function

    Add show_records(sql) in Form1_Load
   Add show_records(sql) in btn_save code before the End If Statement

3. Run the program (F5)

The system successfully loaded data from 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:

It is very detailed and easy to follow 👍👍👍

Post a Comment

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