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:)

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.