VB.NET - Calculate Percentage With MySQL

How To Calculate Percentage From MySQL Database In Visual Basic.Net 

Calculate Percentage With MySQL Using Vbnet

In This VB.Net and MySQL Tutorial We Will See How To Calculate and Display the Percentage Values From a MySQL Database Table Using Visual Basic.Net Programming Language And Visual Studio Editor.

- To Learn More Watch The Video Tutorial Below. 


Project Source Code:

Imports MySql.Data.MySqlClient

Public Class calc_percent_form

    ' create the connection with mysql database
    Dim connection As New MySqlConnection("datasource=localhost;port=3306;username=root;password=;database=vbnet_student_db")

    Private Sub calc_percent_form_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim totalStd As Integer = Convert.ToInt32(totalStudents())
        Dim totalMaleStd As Integer = Convert.ToInt32(totalMaleStudents())
        Dim totalFemaleStd As Integer = Convert.ToInt32(totalFemaleStudents())

        ' the % formula
        ' (male students * 100) / totale students
        ' (female students * 100) / totale students

        Dim maleStd_p As Double = (totalMaleStd * 100) / totalStd
        Dim femaleStd_p As Double = (totalFemaleStd * 100) / totalStd

        lbl_total_students.Text = totalStd.ToString()
        lbl_total_Male_Students.Text = maleStd_p.ToString("0.00") & "%"
        lbl_total_Female_Students.Text = femaleStd_p.ToString("0.00") & "%"


    End Sub


    'create a function to execute count queries
    Function execQuery(ByVal query As String) As String

        Dim command As New MySqlCommand(query, connection)

        ' open connection
        If connection.State = ConnectionState.Closed Then
            connection.Open()
        End If

        Return command.ExecuteScalar.ToString()

        ' close connection
        If connection.State = ConnectionState.Open Then
            connection.Close()
        End If


    End Function


    ' create a function to return total students
    Function totalStudents() As String

        Return execQuery("SELECT COUNT(*) FROM `student`")

    End Function

    ' create a function to return total MALE students
    Function totalMaleStudents() As String

        Return execQuery("SELECT COUNT(*) FROM `student` WHERE gender = 'MALE'")

    End Function

    ' create a function to return total FEMALE students
    Function totalFemaleStudents() As String

        Return execQuery("SELECT COUNT(*) FROM `student` WHERE gender = 'FEMALE'")

    End Function


End Class  
   


///////////////OUTPUT:



How To Calculate Percentage From MySQL Using Vbnet



Share this

Related Posts

Previous
Next Post »