• We just launched and are currently in beta. Join us as we build and grow the community.

Find Records Based on RadioButton in VB.Net

lhbenja02

Global Tech Strategist
L Rep
0
0
0
Rep
0
L Vouches
0
0
0
Vouches
0
Posts
82
Likes
126
Bits
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.
  1. CREATE

    TABLE

    `profile`

    (
  2. `id`

    int

    (

    100

    )

    NOT

    NULL

    ,
  3. `name`

    varchar

    (

    100

    )

    DEFAULT

    NULL

    ,
  4. `email`

    varchar

    (

    100

    )

    DEFAULT

    NULL

    ,
  5. `photo`

    varchar

    (

    250

    )

    DEFAULT

    NULL

    ,
  6. `joindate`

    date

    NOT

    NULL


  7. )

    ENGINE

    =

    MyISAM DEFAULT

    CHARSET

    =

    latin1;

  8. --
  9. -- Dumping data for table `profile`
  10. --

  11. INSERT

    INTO

    `profile`

    (

    `id`

    ,

    `name`

    ,

    `email`

    ,

    `photo`

    ,

    `joindate`

    )

    VALUES


  12. (

    1

    ,

    'Jaison Joy'

    ,

    '[email protected]'

    ,

    'http://jaisonjoy.com//image/man.png'

    ,

    '2019-01-01'

    )

    ,
  13. (

    2

    ,

    'Vini'

    ,

    '[email protected]'

    ,

    'http://jaisonjoy.com/image/female.png'

    ,

    '2019-01-15'

    )

    ,
  14. (

    3

    ,

    'John'

    ,

    '[email protected]'

    ,

    'http://jaisonjoy.com/image/male.png'

    ,

    '2019-02-03'

    )

    ,
  15. (

    4

    ,

    'Test'

    ,

    'Test'

    ,

    NULL

    ,

    '2019-02-14'

    )

    ,
  16. (

    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.
creating_vb_7.png

Step 2

Do the form just like shown below.
2019-02-21.png

Step 3

Go to the code view and add a namespace for mysql above the class.
  1. 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.
  1. Dim

    con As

    MySqlConnection = New

    MySqlConnection("server=localhost;user id=root;password=;database=profile;sslMode=none"

    )
  2. Dim

    cmd As

    MySqlCommand
  3. Dim

    da As

    MySqlDataAdapter
  4. Dim

    dt As

    DataTable
  5. Dim

    sql As

    String

Step 5

Create a sub procedure for retrieving data in the database

  1. Private

    Sub

    findData(sql As

    String

    , dtg As

    DataGridView)
  2. Try
  3. con.Open

    ()
  4. cmd = New

    MySqlCommand
  5. With

    cmd
  6. .Connection = con
  7. .CommandText = sql
  8. End

    With
  9. da = New

    MySqlDataAdapter
  10. da.SelectCommand = cmd
  11. dt = New

    DataTable
  12. da.Fill(dt)
  13. dtg.DataSource = dt
  14. Catch ex As

    Exception
  15. MsgBox(ex.Message)
  16. Finally
  17. con.Close

    ()
  18. da.Dispose()
  19. End

    Try
  20. End

    Sub

Step 6
Do the following codes for retrieving data in the first load of the form.
  1. Private

    Sub

    Form1_Load(sender As

    Object

    , e As

    EventArgs) Handles MyBase.Load
  2. sql = "SELECT `id` as 'ID', `name` as 'NAME', `gender` as 'Sex', `email` as 'E-MAIL', `photo` as 'Image', `joindate` As 'JoinDate' FROM `profile`"
  3. findData(sql, dtgList)
  4. End

    Sub

Step 7
Do the following codes for finding records when the radio button is checked.
  1. Private

    Sub

    rdoMale_Click(sender As

    Object

    , e As

    EventArgs) Handles rdoMale.Click, rdoFemale.Click
  2. If

    rdoFemale.Checked = True

    Then
  3. 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 & "'"
  4. findData(sql, dtgList)
  5. Else
  6. 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 & "'"
  7. findData(sql, dtgList)
  8. End

    If
  9. 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.

 

452,496

336,529

336,537

Top