The Simplest CRUD Operations for Lookup Tables in ASP.NET - Retrieving information from the database
(Page 4 of 7 )
To retrieve information from the database onto the grid, you try to define a simple method something like the following:
PrivateSub BindGrid()
Me.DataGrid1.DataSource = db.getDataTable("select * from
dept")
Me.DataGrid1.DataBind()
End Sub
The above would simply retrieve all of the information from the table "dept" and assign it to "datagrid." When any button in the datagrid is hit, it must execute the respective method. The following code will do that based on the definitions of the columns you added to the datagrid (please refer to the first section).
PrivateSub DataGrid1_ItemCommand(ByVal source As Object, ByVal
e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.ItemCommand
Dim DeptId As String = e.Item.Cells(2).Text
Select Case e.CommandName
Case "OnEdit"
ShowEdit(DeptId)
Case "OnDel"
Delete(DeptId)
End Select
End Sub
When you click on "edit" or "delete," you must find the "departmentID" present in the row on which the user clicked. The following statement would give you the same:
Dim DeptId As String = e.Item.Cells(2).Text
You must make sure that the "CommandName" you specify in the above code matches with the columns defined earlier.
Next: Dealing with DML operations >>
More ASP.NET Articles
More By Jagadish Chaterjee