lhbenja02
Global Tech Strategist
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
200 XP
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.
Creating Database
Create a database named “profile”
Do the following query to create a table and add the data inside the table.
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.
Step 4
Inside the class, create a connection between MySQL and Visual Basic 2015. After that, declare all the classes that are needed.
Step 5
Create a sub procedure for retrieving data in the database
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.
- CREATE
TABLE
`profile`
(
- `id`
int
(
100
)
NOT
NULL
,
- `name`
varchar
(
100
)
DEFAULT
NULL
,
- `email`
varchar
(
100
)
DEFAULT
NULL
,
- `photo`
varchar
(
250
)
DEFAULT
NULL
,
- `joindate`
date
NOT
NULL
- )
ENGINE
=
MyISAM DEFAULT
CHARSET
=
latin1;
- --
- -- Dumping data for table `profile`
- --
- INSERT
INTO
`profile`
(
`id`
,
`name`
,
`email`
,
`photo`
,
`joindate`
)
VALUES
- (
1
,
'Jaison Joy'
,
'[email protected]'
,
'http://jaisonjoy.com//image/man.png'
,
'2019-01-01'
)
,
- (
2
,
'Vini'
,
'[email protected]'
,
'http://jaisonjoy.com/image/female.png'
,
'2019-01-15'
)
,
- (
3
,
'John'
,
'[email protected]'
,
'http://jaisonjoy.com/image/male.png'
,
'2019-02-03'
)
,
- (
4
,
'Test'
,
'Test'
,
NULL
,
'2019-02-14'
)
,
- (
5
,
'test2'
,
'Test2'
,
NULL
,
'2019-02-14'
)
;
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
Step 6
Do the following codes for retrieving data in the first load of the form.
- 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
Step 7
Do the following codes for finding records when the radio button is checked.
- 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
For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.