ITSourceCode.com

Free Source Code and Tutorials

Custom Search

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 records in VB.Net and MySQL Database. 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.

Lets’ begin:

The Database of this project is located here. Follow the instructions for creating database.

After creating database, open the Visual Basic and create a new Windows Form Application. After that, set the Form just like this.
searchdataform
After that setting up the Form, double click the 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 Form1_Load

  1. Public con As MySqlConnection = New MySqlConnection("server=localhost;user id=root;database=dbemployees")
  2.     Public cmd As New MySqlCommand
  3.     Public da As New MySqlDataAdapter
  4.     Public dt As New DataTable

Note: add mysql.data.dll as your reference to make MySQL work.
Go to Form1_Load and do the following codes for retrieving the record in the database.

  1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.         'OPENING THE CONNECTION
  3.         con.Open()
  4.         'SET A NEW SPECIFIC TABLE IN THE DATABASE
  5.         dt = New DataTable
  6.         'SET YOUR COMMANDS TO PROVIDE A TEXT-BASE INTERFACE INTO THE MYSQL DATABASE SERVER.
  7.         'AND ONCE IT'S CONNECTED, YOU CAN MAKE QUERY OR MANY OTHER OPERATION.
  8.         With cmd
  9.             .Connection = con
  10.             .CommandText = "SELECT `EMP_ID` as EmployeeId, `F_NAME` as FirstName, `L_NAME` as LastName, `EMAIL` as UserName, `PHONENUMB` as PhoneNumber FROM `employees` "
  11.         End With
  12.         'SET THIS STORED PROCEDURE TO SELECT THE RECORD IN THE DATASOURCE
  13.         da.SelectCommand = cmd
  14.         'FILLING THE DATA IN THE DATATABLE
  15.         da.Fill(dt)
  16.         'SET THE DATASOURCE OF THE DATAGRIDVIEW
  17.         DataGridView1.DataSource = dt
  18.         'CLOSING THE CONNECTION
  19.         con.Close()
  20.         da.Dispose()
  21.     End Sub

Go back to the Form Design, double click the “Search” Button and do this code for searching the records in the database.

  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.         Try
  3.             con.Open()
  4.             dt = New DataTable
  5.             With cmd
  6.                 .Connection = con
  7.                 .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 & "%'"
  8.             End With
  9.             da.SelectCommand = cmd
  10.             da.Fill(dt)
  11.             DataGridView1.DataSource = dt
  12.  
  13.         Catch ex As Exception
  14.             MsgBox(ex.Message)
  15.         End Try
  16.         da.Dispose()
  17.         con.Close()
  18.     End Sub

Download the complete Source Code and run it on your computer.

searchdatadatagridview

Facebook Comments
(Visited 428 times, 23 visits today)
Share this

Check Your Domain Ranking

4 Comments

Add a Comment
  1. what do you mean add mysql.data.dll as references and where to add it?

  2. HI…! I NEED HELP FROM THE EXPERT.. THIS IS MY CODE THIS CODE IS DISPLAYING DATA FROM MY TABLE TO MY DATAGRID VIEW.. I WANT TO ADD A INNER JOIN TO MY CODE TO SELECT SOME DATA THERE AND THAT DATA WOULD BE DISPLAY ON MY TXTBOX NOT ON MY DATAGRID.. EXAMBLE DATA IS THE THE NAME OF THE PERSON AND THE OTHER DATA WOULD BE DISPLAY ON MY DATAGRID VIEW.. TNXX…

    ‘SEARCH BUTTON

    Dim CMD As New OleDbCommand
    Dim cnn As New OleDbConnection
    Dim dt As New DataTable
    Dim da As New OleDbDataAdapter
    Dim DS As New DataSet

    cnn.ConnectionString = “provider=Microsoft.SQLSERVER.CE.oleDb.3.5;Data Source=C:\Users\user\Documents\2017TAXPAYERRECORD.sdf;”
    cnn.Open()

    Text = cnn.State.ToString
    Dim str As String
    str = “SELECT TIN,QUARTER,FORMTYPE,RETURNPERIOD,TOTALAMOUNT,DATEOFPAYMENT FROM TAXPAYMENT WHERE TIN='” & STIN.Text & “‘AND RETURNPERIOD='” & SRPERIOD.Text & “‘AND FORMTYPE='” & SFOARMTYPE.Text & “‘”
    da = New OleDbDataAdapter(str, cnn)
    da.Fill(dt)
    DataGridView1.DataSource = dt

    Me.TAXPAYMENTTableAdapter1.Fill(Me.NEWPAYMENTLIST.TAXPAYMENT)

    End Sub

  3. HI NEED HELP FOR THE EXPERT.. THIS MY CODE FOR SEACHING RECORD.. I WANT THE FOLLOWING DATA FIRST NAME, LAST NAME AND MIDDLE NAME OF PERSON WOULD BE DISPLAY ON MY TEXTBOX.. HOW CAN I DO THAT.. PLSSS HELP ME…

    ‘SEARCH BUTTON
    Dim CMD As New OleDbCommand
    Dim cnn As New OleDbConnection
    Dim dt As New DataTable
    Dim da As New OleDbDataAdapter
    Dim DS As New DataSet

    cnn.ConnectionString = “provider=Microsoft.SQLSERVER.CE.oleDb.3.5;Data Source=C:\Users\user\Documents\2017TAXPAYERRECORD.sdf;”
    cnn.Open()

    Text = cnn.State.ToString
    Dim str As String
    str = “SELECT TAXTPERSON.LNAME,TAXTPERSON.FNAME,TAXTPERSON.MNAME,TAXPAYMENT.TIN,TAXPAYMENT.QUARTER,TAXPAYMENT.TAXTYPE,TAXPAYMENT.FORMTYPE,TAXPAYMENT.RETURNPERIOD,TAXPAYMENT.TOTALAMOUNT,TAXPAYMENT.DATEOFPAYMENT FROM TAXPAYMENT INNER JOIN TAXTPERSON ON TAXPAYMENT.TIN = TAXTPERSON.TIN WHERE TAXPAYMENT.TIN='” & STIN.Text & “‘”
    da = New OleDbDataAdapter(str, cnn)
    da.Fill(dt)
    DataGridView1.DataSource = dt

    Me.TAXPAYMENTTableAdapter1.Fill(Me.NEWPAYMENTLIST.TAXPAYMENT)

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!




%d bloggers like this: