Search records using textbox in VB.NET

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

  1. Imports System.Data  
  2. Imports System.Data.OleDb  
  3. Public Class Form1  
  4.     Dim ConnectionString As String = System.Configuration.ConfigurationSettings.AppSettings("dsn")  
  5.     Dim con As OleDbConnection = New OleDbConnection(ConnectionString)  
  6.     Dim com As OleDbCommand  
  7.     Dim oledbda As OleDbDataAdapter  
  8.     Dim ds As DataSet  
  9.     Dim dt As DataTable  
  10.     Dim str As String  
  11.     Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs)Handles Button1.Click  
  12.         If TextBox1.Text = "" Then  
  13.             ErrorProvider1.SetError(TextBox1, "please provide name")  
  14.         Else  
  15.             Try  
  16.                 con.Open()  
  17.                 str = "select * from student where sname ='" & TextBox1.Text & "'"  
  18.                 com = New OleDbCommand(str, con)  
  19.                 oledbda = New OleDbDataAdapter(com)  
  20.                 ds = New DataSet  
  21.                 oledbda.Fill(ds, "student")  
  22.                 con.Close()  
  23.                 DataGridView1.DataSource = ds  
  24.                 DataGridView1.DataMember = "student"  
  25.             Catch ex As Exception  
  26.                 MsgBox(ex.Message)  
  27.             End Try  
  28.         End If  
  29.         TextBox1.Clear()  
  30.         DataGridView1.Visible = True  
  31.     End Sub  
  32.     Private Sub TextBox2_TextChanged(ByVal sender As System.ObjectByVal e AsSystem.EventArgs)   
  33. Handles TextBox2.TextChanged  
  34.        If TextBox2.Text = "" And TextBox2.Text.Length = 0 Then  
  35.              DataGridView1.Visible = False  
  36.         Else  
  37.             Try  
  38.                con.Open()  
  39.                 str = "select * from student where sname like '" & TextBox2.Text & "%'"  
  40.                 com = New OleDbCommand(str, con)  
  41.                 oledbda = New OleDbDataAdapter(com)  
  42.                 ds = New DataSet  
  43.                 oledbda.Fill(ds, "student")  
  44.                 con.Close()  
  45.                 DataGridView1.DataSource = ds  
  46.                 DataGridView1.DataMember = "student"  
  47.                 DataGridView1.Visible = True  
  48.             Catch ex As Exception  
  49.                 MsgBox(ex.Message)  
  50.             End Try  
  51.         End If  
  52.     End Sub  
  53.     Private Sub TextBox3_TextChanged(ByVal sender As System.ObjectByVal e AsSystem.EventArgs)   
  54. Handles TextBox3.TextChanged  
  55.         Try  
  56.             con.Open()  
  57.             str = "select * from student where sname ='" & TextBox3.Text & "'"  
  58.             com = New OleDbCommand(str, con)  
  59.             oledbda = New OleDbDataAdapter(com)  
  60.             ds = New DataSet  
  61.             oledbda.Fill(ds, "student")  
  62.             con.Close()  
  63.             DataGridView1.DataSource = ds  
  64.             DataGridView1.DataMember = "student"  
  65.             DataGridView1.Visible = True  
  66.         Catch ex As Exception  
  67.             MsgBox(ex.Message)  
  68.         End Try  
  69.     End Sub  
  70.     Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) HandlesMyBase.Load  
  71.         DataGridView1.Visible = False  
  72.     End Sub  
  73. End Class  

Output

search-record-VB.NET.gif


Similar Articles