0% found this document useful (0 votes)
36 views

DataGrid Connectivity

This Visual Basic code defines a class called Form1 that connects to a database and performs queries on a student table. It opens a connection to the database using a connection string stored in the app settings. It has event handlers for three text boxes that allow searching or filtering the student records by name, name starting with the text, or exact name match. Any records returned are bound to a data grid for viewing.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

DataGrid Connectivity

This Visual Basic code defines a class called Form1 that connects to a database and performs queries on a student table. It opens a connection to the database using a connection string stored in the app settings. It has event handlers for three text boxes that allow searching or filtering the student records by name, name starting with the text, or exact name match. Any records returned are bound to a data grid for viewing.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

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 AsSystem.Ev entArgs) 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 AsSystem.Ev entArgs) 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) H andlesMyBase.Load DataGridView1.Visible = False End Sub End Class Output

You might also like