Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Not IsPostBack) Then
ReadRecords()
End If
End Sub
Private Sub ReadRecords()
Dim conn As OleDbConnection = Nothing
Dim reader As OleDbDataReader = Nothing
Try
conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("db/Database1.mdb"))
conn.Open()
Dim cmd As New OleDbCommand("Select * FROM Table1", conn)
reader = cmd.ExecuteReader()
DataGrid1.DataSource = reader
DataGrid1.DataBind()
Finally
If reader IsNot Nothing Then
reader.Close()
End If
If conn IsNot Nothing Then
conn.Close()
End If
End Try
End Sub
Protected Sub DataGrid1_CancelCommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs)
DataGrid1.EditItemIndex = -1
ReadRecords()
End Sub
Protected Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs)
DataGrid1.EditItemIndex = e.Item.ItemIndex
ReadRecords()
End Sub
Protected Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs)
Dim ID As Integer = CInt(Fix(DataGrid1.DataKeys(CInt(Fix(e.Item.ItemIndex)))))
Dim name As String = (CType(e.Item.Cells(1).Controls(0), TextBox)).Text
Dim type As String = (CType(e.Item.Cells(2).Controls(0), TextBox)).Text
Dim sql As String = "UPDATE Table1 SET PetName=""" & name & """, PetType=""" & type & """" & " WHERE ID=" & ID
ExecuteNonQuery(sql)
DataGrid1.EditItemIndex = -1
ReadRecords()
End Sub
Private Sub ExecuteNonQuery(ByVal sql As String)
Dim conn As OleDbConnection = Nothing
Try
conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("db/Database1.mdb"))
conn.Open()
Dim cmd As New OleDbCommand(sql, conn)
cmd.ExecuteNonQuery()
Finally
If conn IsNot Nothing Then
conn.Close()
End If
End Try
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim sql As String = "INSERT INTO Table1 (PetName, PetType)" & " VALUES (""new"", ""new"")"
ExecuteNonQuery(sql)
ReadRecords()
End Sub
Protected Sub DataGrid1_DeleteCommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs)
Dim ID As Integer = CInt(Fix(DataGrid1.DataKeys(CInt(Fix(e.Item.ItemIndex)))))
Dim sql As String = "DELETE FROM Table1 WHERE ID=" & ID
ExecuteNonQuery(sql)
ReadRecords()
End Sub
End Class