ARTICLE

Search records using textbox in VB.NET

Posted by Satyapriya Nayak Articles | Windows Forms VB.NET August 12, 2011
In this article we will search records from database using textbox and show respective data in the datagridview in three different ways.
Reader Level:

In this article we will search records from database using textbox and show respective data in the datagridview in three different ways.

1.Enter the know data in the textbox and click the search button to display respective data from the datagridview.

2.Enter the first letter of the unknown data in the textbox and all data relating data to that letter will be shown in the datagridview, so that we can able to search our own data.

3.Enter the know data in the textbox and find its all related data in the datagridview

Program

Imports System.Data

Imports System.Data.OleDb

Public Class Form1

    Dim ConnectionString As String = System.Configuration.ConfigurationSettings.AppSettings("dsn")

    Dim con As OleDbConnection = New OleDbConnection(ConnectionString)

    Dim com As OleDbCommand

    Dim oledbda As OleDbDataAdapter

    Dim ds As DataSet

    Dim dt As DataTable

    Dim str As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If TextBox1.Text = "" Then

            ErrorProvider1.SetError(TextBox1, "please provide name")

        Else

            Try

                con.Open()

                str = "select * from student where sname ='" & TextBox1.Text & "'"

                com = New OleDbCommand(str, con)

                oledbda = New OleDbDataAdapter(com)

                ds = New DataSet

                oledbda.Fill(ds, "student")

                con.Close()

                DataGridView1.DataSource = ds

                DataGridView1.DataMember = "student"

            Catch ex As Exception

                MsgBox(ex.Message)

            End Try

        End If

        TextBox1.Clear()

        DataGridView1.Visible = True

    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

        If TextBox2.Text = "" And TextBox2.Text.Length = 0 Then

            DataGridView1.Visible = False

        Else

            Try

                con.Open()

                str = "select * from student where sname like '" & TextBox2.Text & "%'"

                com = New OleDbCommand(str, con)

                oledbda = New OleDbDataAdapter(com)

                ds = New DataSet

                oledbda.Fill(ds, "student")

                con.Close()

                DataGridView1.DataSource = ds

                DataGridView1.DataMember = "student"

                DataGridView1.Visible = True

            Catch ex As Exception

                MsgBox(ex.Message)

            End Try

        End If

    End Sub

    Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged

        Try

            con.Open()

            str = "select * from student where sname ='" & TextBox3.Text & "'"

            com = New OleDbCommand(str, con)

            oledbda = New OleDbDataAdapter(com)

            ds = New DataSet

            oledbda.Fill(ds, "student")

            con.Close()

            DataGridView1.DataSource = ds

            DataGridView1.DataMember = "student"

            DataGridView1.Visible = True

        Catch ex As Exception

            MsgBox(ex.Message)

        End Try

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        DataGridView1.Visible = False

    End Sub

End Class

Output

search-record.gif

Thanks for reading.

Login to add your contents and source code to this article
share this article :
post comment
 

can u send the coding n sample project tu my email... are_shuk88@yahoo.com

Posted by shuk ri May 10, 2012

can u give me the practical for this project...i already write this code...but some problem apear...such as the connectionstring cannot be initialize...how i want initialize this connectionstring...im a new bie in this vb...i hope u can help me..

Posted by shuk ri May 10, 2012
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor