Find Records Based on RadioButton in VB.Net
Submitted by janobe on Thursday, February 21, 2019 - 17:08.
In this tutorial, I will teach how to find record based on RadioButton in vb.net. This method has the ability to find records in the database based on the value of the radio button. This is so simple yet very powerful methods that can be learn in few minutes. All you have to do is follow the instructions below to see how it’s done.
Step 6
Do the following codes for retrieving data in the first load of the form.
Step 7
Do the following codes for finding records when the radio button is checked.
For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.
Creating Database
Create a database named “profile” Do the following query to create a table and add the data inside the table.- --
- -- Dumping data for table `profile`
- --
Creating Application
Step 1
Open Microsoft Visual Studio 2015 and create a new windows form application in visual basic.
Step 2
Do the form just like shown below.
Step 3
Go to the code view and add a namespace for mysql above the class.- Imports MySql.Data.MySqlClient
Step 4
Inside the class, create a connection between MySQL and Visual Basic 2015. After that, declare all the classes that are needed.- Dim con As MySqlConnection = New MySqlConnection("server=localhost;user id=root;password=;database=profile;sslMode=none")
- Dim cmd As MySqlCommand
- Dim da As MySqlDataAdapter
- Dim dt As DataTable
- Dim sql As String
Step 5
Create a sub procedure for retrieving data in the database- Private Sub findData(sql As String, dtg As DataGridView)
- Try
- con.Open()
- cmd = New MySqlCommand
- With cmd
- .Connection = con
- .CommandText = sql
- End With
- da = New MySqlDataAdapter
- da.SelectCommand = cmd
- dt = New DataTable
- da.Fill(dt)
- dtg.DataSource = dt
- Catch ex As Exception
- MsgBox(ex.Message)
- Finally
- con.Close()
- da.Dispose()
- End Try
- End Sub
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- sql = "SELECT `id` as 'ID', `name` as 'NAME', `gender` as 'Sex', `email` as 'E-MAIL', `photo` as 'Image', `joindate` As 'JoinDate' FROM `profile`"
- findData(sql, dtgList)
- End Sub
- Private Sub rdoMale_Click(sender As Object, e As EventArgs) Handles rdoMale.Click, rdoFemale.Click
- If rdoFemale.Checked = True Then
- sql = "SELECT `id` as 'ID', `name` as 'NAME', `gender` as 'Sex', `email` as 'E-MAIL', `photo` as 'Image', `joindate` As 'JoinDate' FROM `profile` WHERE gender='" & rdoFemale.Text & "'"
- findData(sql, dtgList)
- Else
- sql = "SELECT `id` as 'ID', `name` as 'NAME', `gender` as 'Sex', `email` as 'E-MAIL', `photo` as 'Image', `joindate` As 'JoinDate' FROM `profile` WHERE gender='" & rdoMale.Text & "'"
- findData(sql, dtgList)
- End If
- End Sub
Add new comment
- 519 views