• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

How to Create a Simple Search Based on RadioButton in C# and MySQL Database

arp

Crypto Gaming Specialist
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
46
Likes
27
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
In this tutorial, I will teach you how to create a simple search based on radiobutton using c# and mysql database. This method has the ability to select one option from a group that is used to control the data, then it will be displayed in the datagriview. Follow the step by step guide to see how it works.

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application for c#.
creating_application_c_2.png

Step 2

Do the form just like shown below.
2019-06-28_2.png

Step 3

Open the code editor by pressing the F7 on the keyboard. In the code editor, add a namespace to access MySQL Libraries.
  1. using

    MySql.Data.MySqlClient

    ;

Step 4

Create a connection between C# and MySQL Database. After that, declare all the classes that are needed.
  1. MySqlConnection con =

    new

    MySqlConnection(

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

    )

    ;
  2. MySqlCommand cmd;
  3. MySqlDataAdapter da;
  4. DataTable dt;
  5. string

    sql;

Step 5

Create a method for retrieving data in the database and it will be displayed inside the datagridview.

  1. private

    void

    LoadData(

    string

    sql,DataGridView dtg)
  2. {
  3. try
  4. {
  5. con.

    Open

    (

    )

    ;

  6. cmd =

    new

    MySqlCommand(

    )

    ;
  7. da =

    new

    MySqlDataAdapter(

    )

    ;
  8. dt =

    new

    DataTable(

    )

    ;

  9. cmd.

    Connection

    =

    con;
  10. cmd.

    CommandText

    =

    sql;

  11. da.

    SelectCommand

    =

    cmd;
  12. da.

    Fill

    (

    dt)

    ;

  13. dtg.

    DataSource

    =

    dt;


  14. }
  15. catch

    (

    Exception ex)
  16. {
  17. MessageBox.

    Show

    (

    ex.

    Message

    )

    ;
  18. }
  19. finally
  20. {
  21. con.

    Close

    (

    )

    ;
  22. da.

    Dispose

    (

    )

    ;
  23. }
  24. }

Step 6

Write the following code for retrieving data in the database in the first load of the form.

  1. private

    void

    Form1_Load(

    object

    sender, EventArgs e)
  2. {
  3. sql =

    "SELECT * FROM `profile`"

    ;
  4. LoadData(

    sql, dataGridView1)

    ;
  5. }

Step 7

Write this code for finding data in the database.

  1. private

    void

    rdoFemale_CheckedChanged(

    object

    sender, EventArgs e)
  2. {
  3. sql =

    "SELECT * FROM `profile` WHERE `gender`='"

    +

    rdoFemale.

    Text

    +

    "'"

    ;
  4. LoadData(

    sql, dataGridView1)

    ;
  5. }
  6. private

    void

    rdoMale_CheckedChanged(

    object

    sender, EventArgs e)
  7. {
  8. sql =

    "SELECT * FROM `profile` WHERE `gender`='"

    +

    rdoMale.

    Text

    +

    "'"

    ;
  9. LoadData(

    sql, dataGridView1)

    ;
  10. }

The complete source code is included, you can download it and run it on your computer.
For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,501

355,782

355,789

Top