This tutorial is all about Retrieve and Update Year Level.
This time, i’m going to retrieve the records that have been saved in Ms Access Database. I have made some changes here regarding with the design of the Form.
Let’s begin:
Open the project that I have created last time which is “StudentInformation” and do the Form just like this.
After setting up the Form, we’ re going to retrieve all the records in Ms Access Database. Now, double click the “Load” button to fire the event handler of it and do this following code to display all the records in the DatagridView.
- Private Sub btnload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnload.Click
- Try
- 'opening the connection
- con.Open()
- 'store a query to a variable that you have declared.
- query = "SELECT * FROM tbllevel"
- 'set a query and a connection string to a class that you have declared.
- da = New OleDb.OleDbDataAdapter(query, con)
- dt = New DataTable
- 'Filling the data in the datatable
- da.Fill(dt)
- 'Get or set the datasource of a datagridview to display the data that came from the database.
- dtgyllist.DataSource = dt
- Catch ex As Exception
- 'catching error
- End Try
- End Sub
Go back to the Form Design and click the DatagridView. After that, go to the properties and click the lightning symbol. Scroll down and find the “DoubleClick
” event handler of the DatagridView and double click it.
In the DoubleClick
event handler of the DatagridView, add this following code for passing the record from the DatagridView to a TextBox.
- Private Sub dtgyllist_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtgyllist.DoubleClick
- Try
- 'passing the value to a variable
- levelid = dtgyllist.CurrentRow.Cells(0).Value
- 'passing the value to a textbox
- txtyl.Text = dtgyllist.CurrentRow.Cells(1).Value
- Catch ex As Exception
- 'catching error
- End Try
- End Sub
Lastly, go back to the Form Design and double click the “Update” Button to fire the event handler of it. After that, add this following code in the method.
- Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
- Try
- 'opening the connection
- con.Open()
- 'store a query to a variable that you have declared.
- query = "UPDATE tbllevel SET YLEVEL = '" & txtyl.Text & "' WHERE LEVELID =" & levelid & ""
- 'set a query and a connection string to a class that you have declared.
- da = New OleDb.OleDbDataAdapter(query, con)
- dt = New DataTable
- 'Filling the data in the datatable
- da.Fill(dt)
- txtyl.Clear()
- levelid = 0
- Catch ex As Exception
- 'catching error
- End Try
- End Sub
Readers might read also:
Hi, i feel that i noticed you visited my web site thus i came to go back the
favor?.I’m attempting to to find things to enhance my web site!I suppose its ok to
make use of a few of your ideas!!