Search DataGridView Using TextBox in VB.Net and MySQL Database
This tutorial is all about How to Search Data in the DataGridView Using a TextBox in VB.Net and MySQL Database.
In this tutorial, I will show you how to search for records in VB.Net and MySQL Database.
What is Visual Basic’s purpose?
The third-generation programming language was created to aid developers in the creation of Windows applications.
It has a programming environment that allows programmers to write code in.exe or executable files.
They can also utilize it to create in-house front-end solutions for interacting with huge databases.
It is because the language allows for continuing changes, you can keep coding and revising your work as needed.
However, there are some limits to the Microsoft Visual Basic download. If you want to make applications that take a long time to process, this software isn’t for you.
That implies you won’t be able to use VB to create games or large apps because the system’s graphic interface requires a lot of memory and space. Furthermore, the language is limited to Microsoft and does not support other operating systems.
What are the most important characteristics of Visual Basic?
Microsoft Visual Basic for Applications Download, unlike other programming languages, allows for speedier app creation.
It has string processing capabilities and is compatible with C++, MFC, and F#. Multi-targeting and the Windows Presentation Framework are also supported by the system, allowing developers to create a variety of Windows apps, desktop tools, metro-style programs, and hardware drivers.
The list of records in the database will be displayed on the Datagridview and it will show you how easy it is to search a record using a TextBox.
How to search datagridview using textbox vb.net
Step 1: Download the database of this project is located in this link here. (Follow the instructions for creating a database.)
Step 2: After creating a database, open the Visual Basic and create a new Windows Form Application. After that, set the Form just like this.
Step 3: After setting up the Form, double click it and do this code for setting up the connection of MySQL Database to Visual Basic and declare all the classes that are needed above the
Public con As MySqlConnection = New MySqlConnection("server=localhost;user id=root;database=dbemployees") Public cmd As New MySqlCommand Public da As New MySqlDataAdapter Public dt As New DataTable
mysql.data.dll like your reference to make MySQL work. Follow here the Complete Guide on How to add mysql.data.dll to your vb.net application.
Step 4: Go to
Form1_Load and do the following codes for retrieving the record in the database.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'OPENING THE CONNECTION con.Open() 'SET A NEW SPECIFIC TABLE IN THE DATABASE dt = New DataTable 'SET YOUR COMMANDS TO PROVIDE A TEXT-BASE INTERFACE INTO THE MYSQL DATABASE SERVER. 'AND ONCE IT'S CONNECTED, YOU CAN MAKE QUERY OR MANY OTHER OPERATION. With cmd .Connection = con .CommandText = "SELECT `EMP_ID` as EmployeeId, `F_NAME` as FirstName, `L_NAME` as LastName, `EMAIL` as UserName, `PHONENUMB` as PhoneNumber FROM `employees` " End With 'SET THIS STORED PROCEDURE TO SELECT THE RECORD IN THE DATASOURCE da.SelectCommand = cmd 'FILLING THE DATA IN THE DATATABLE da.Fill(dt) 'SET THE DATASOURCE OF THE DATAGRIDVIEW DataGridView1.DataSource = dt 'CLOSING THE CONNECTION con.Close() da.Dispose() End Sub
Step 5: Go back to the Form Design, double click the “Search” Button and do this code for searching the records in the database.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try con.Open() dt = New DataTable With cmd .Connection = con .CommandText = "SELECT `EMP_ID` as EmployeeId, `F_NAME` as FirstName, `L_NAME` as LastName, `EMAIL` as UserName, `PHONENUMB` as PhoneNumber FROM `employees` where EMP_ID like '%" & TextBox1.Text & "%'" End With da.SelectCommand = cmd da.Fill(dt) DataGridView1.DataSource = dt Catch ex As Exception MsgBox(ex.Message) End Try da.Dispose() con.Close() End Sub
Download the Code
Download the complete Source Code and run it on your computer searchdatadatagridview
If you have any questions or suggestions about this tutorial on how to Search data in the DataGridView Using TextBox in VB.Net and MySQL Database. Please leave a comment below.
If you want to learn how to master the VB.Net with MySQL Database in 30 minutes, watch and follow the video here.