I am creating a simple program, I really need the code for this..here is my situation..

For example if I input a student no. in a textbox to perform search option then click the search button, I want to see the info of the said student to appear from the list of student in my datagrid..then if I click that particular row in the datagrid it will appear on a textbox..

I would really appreciate your help...thanks:)

Dani AI

Generated

Short summary and recommended fix for : the goal is to search by student number, show matching rows in a DataGridView, and have a clicked row populate a textbox. ’s quick example will display results but has a few practical issues (SQL concatenation, no Using/dispose, potential duplicate rows on repeated searches, and reliance on CellContentClick). ’s suggestion to load a DataTable and use data-binding is the cleaner, safer approach.

Load the student table once into a DataTable, bind that table to a BindingSource, bind the BindingSource to the DataGridView, and bind the textbox to the same BindingSource. Filtering can then be done client-side (BindingSource.Filter) or server-side with a parameterized query. Benefits: no repeated DB round-trips, automatic UI updates when the selection changes, and much simpler code maintenance.

Example (VB.NET, concise):

Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Path\Student.mdb"
Dim dt As New DataTable()
Using conn As New OleDbConnection(connStr)
    Using da As New OleDbDataAdapter("SELECT STUDENTID, NAME, AGE FROM STUDENT", conn)
        da.Fill(dt)
    End Using
End Using

Dim bs As New BindingSource()
bs.DataSource = dt
grdStudent.DataSource = bs

txtStudentNo.DataBindings.Clear()
txtStudentNo.DataBindings.Add("Text", bs, "STUDENTID", True, DataSourceUpdateMode.Never)

' Filter example (adjust quoting if STUDENTID is text)
bs.Filter = "STUDENTID = 123"

Troubleshooting and cautions: validate the search input type (numeric vs text) before filtering; avoid building SQL by concatenation — use parameters for server-side queries; clear or reset the DataGridView.DataSource instead of manually adding rows; prefer SelectionChanged/CurrentChanged or data-binding over CellContentClick; always wrap connections/readers in Using and catch exceptions for a robust UI. This implements ’s DataTable idea while addressing practical gaps in ’s sample.

Recommended Answers

All 2 Replies

Dim conn As OleDbConnection
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Dim sql As String
Private Const OLEDB_CONNECTION = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=~\Student.mdb"


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conn = New OleDbConnection(OLEDB_CONNECTION)
End Sub

Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
sql = "SELECT STUDENTID,NAME,AGE FROM STUDENT WHERE STUDENTID =" & txtStudentNo.Text
cmd = New OleDbCommand(sql, conn)
conn.Open()
dr = cmd.ExecuteReader
While dr.Read
grdStudent.Rows.Add(dr.GetValue(0), dr.GetValue(1), dr.GetValue(2))
End While
conn.Close()
End Sub


Private Sub grdStudent_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdStudent.CellContentClick
sql = "SELECT STUDENTID,NAME,AGE FROM STUDENT WHERE STUDENTID =" & grdStudent.CurrentRow.Cells(0).Value
cmd = New OleDbCommand(sql, conn)
conn.Open()
dr = cmd.ExecuteReader
While dr.Read
txtStudentNo.Text = dr.GetValue(0)
End While
conn.Close()

End Sub

commented: thank's a lot..this is really a big help +0

There is no need to query the database multiple times for the same student record every time they click on an option.

Ardent, I would sugest starting by filling a DataTable with the (class) students you want to work with. Bind the Student Name to the DisplayMember of a Combobox and the StudentId to the ValueMember.

You can display the entire student table/records in a DataGridView (DGV) control with one line of code. When they select a student either from the ComboBox and/or a search TextBox, you can filter your DataTable (which you already have in memory) to display the selected record. Likewise when they double click on a record in the DGV you can bind the record fields right to there appropiate controls on a form for viewing/editing; without the need of re-querying the database or looping thru each record column to assign individual values to controls.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.