VB.Net - Search Data In SQL And Display On DataGridView

VB.NET - How To Search Data In SQL And Show It On DataGridView Using Visual Basic.Net

                                                                                                                         

In This VB.NET Tutorial We Will See How To Search Records From SqlServer Database On Button Click Event And Text Changed Event With Microsoft SQL DataBase Using Visual Basic .NET Programming Language.


Project Source Code:

Imports System.Data.SqlClient

Public Class VBNET_SQL_Datagridview_Search

    Dim connection As New SqlConnection("Server= SAMSNG-PC; Database = TestDB; Integrated Security = true")

    Private Sub VBNET_SQL_Datagridview_Search_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        FilterData("")

    End Sub

    Public Sub FilterData(valueToSearch As String)
        'SELECT * From Users WHERE CONCAT(fname, lname, age) like '%F%'
        Dim searchQuery As String = "SELECT * From Users WHERE CONCAT(fname, lname, age) like '%" & valueToSearch & "%'"

        Dim command As New SqlCommand(searchQuery, connection)
        Dim adapter As New SqlDataAdapter(command)
        Dim table As New DataTable()

        adapter.Fill(table)

        DataGridView1.DataSource = table

    End Sub

    Private Sub BTN_FILTER_Click(sender As Object, e As EventArgs) Handles BTN_FILTER.Click

        FilterData(TextBox1.Text)

    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

        FilterData(TextBox1.Text)

    End Sub
End Class

///////////////OUTPUT:

vb.net datagridview search






Share this

Related Posts

Previous
Next Post »

1 comments:

comments
Anonyme
13 juillet 2021 à 07:51 delete

Hello, how can I add information to de DataGridView? Like "1", "Vb", "26", etc.
Thanks for the tutorial, it helps me a lot.

Reply
avatar