001.
Imports
System.Data
002.
Imports
System.Data.OleDb
003.
Partial
Class
DataGrid1
004.
Inherits
System.Web.UI.Page
005.
Dim
objConn
As
OleDbConnection
006.
Dim
objCmd
As
OleDbCommand
007.
Dim
strSQL
As
String
008.
Protected
Sub
Page_Load(
ByVal
sender
As
Object
,
ByVal
e
As
System.EventArgs)
Handles
Me
.Load
009.
Dim
strConnString
As
String
010.
strConnString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
& Server.MapPath(
"database/mydatabase.mdb"
) &
";"
011.
objConn =
New
OleDbConnection(strConnString)
012.
objConn.Open()
013.
014.
If
Not
Page.IsPostBack()
Then
015.
BindData()
016.
End
If
017.
End
Sub
018.
019.
Protected
Sub
BindData()
020.
strSQL =
"SELECT * FROM customer"
021.
022.
Dim
dtReader
As
OleDbDataReader
023.
objCmd =
New
OleDbCommand(strSQL, objConn)
024.
dtReader = objCmd.ExecuteReader()
025.
026.
027.
myDataGrid.DataSource = dtReader
028.
myDataGrid.DataBind()
029.
030.
dtReader.Close()
031.
dtReader =
Nothing
032.
033.
End
Sub
034.
035.
Protected
Sub
Page_Unload(
ByVal
sender
As
Object
,
ByVal
e
As
System.EventArgs)
Handles
Me
.Unload
036.
objConn.Close()
037.
objConn =
Nothing
038.
End
Sub
039.
040.
Protected
Sub
myDataGrid_CancelCommand(
ByVal
source
As
Object
,
ByVal
e
As
System.Web.UI.WebControls.DataGridCommandEventArgs)
Handles
myDataGrid.CancelCommand
041.
myDataGrid.EditItemIndex = -1
042.
myDataGrid.ShowFooter =
True
043.
BindData()
044.
End
Sub
045.
046.
Protected
Sub
myDataGrid_DeleteCommand(
ByVal
source
As
Object
,
ByVal
e
As
System.Web.UI.WebControls.DataGridCommandEventArgs)
Handles
myDataGrid.DeleteCommand
047.
strSQL =
"DELETE FROM customer WHERE CustomerID = '"
& myDataGrid.DataKeys.Item(e.Item.ItemIndex) &
"'"
048.
objCmd =
New
OleDbCommand(strSQL, objConn)
049.
objCmd.ExecuteNonQuery()
050.
051.
myDataGrid.EditItemIndex = -1
052.
BindData()
053.
End
Sub
054.
055.
Protected
Sub
myDataGrid_EditCommand(
ByVal
source
As
Object
,
ByVal
e
As
System.Web.UI.WebControls.DataGridCommandEventArgs)
Handles
myDataGrid.EditCommand
056.
myDataGrid.EditItemIndex = e.Item.ItemIndex
057.
myDataGrid.ShowFooter =
False
058.
BindData()
059.
End
Sub
060.
061.
Protected
Sub
myDataGrid_ItemCommand(
ByVal
source
As
Object
,
ByVal
e
As
System.Web.UI.WebControls.DataGridCommandEventArgs)
Handles
myDataGrid.ItemCommand
062.
If
e.CommandName =
"Add"
Then
063.
064.
Dim
txtCustomerID
As
TextBox =
CType
(e.Item.FindControl(
"txtAddCustomerID"
), TextBox)
065.
066.
Dim
txtName
As
TextBox =
CType
(e.Item.FindControl(
"txtAddName"
), TextBox)
067.
068.
Dim
txtEmail
As
TextBox =
CType
(e.Item.FindControl(
"txtAddEmail"
), TextBox)
069.
070.
Dim
txtCountryCode
As
TextBox =
CType
(e.Item.FindControl(
"txtAddCountryCode"
), TextBox)
071.
072.
Dim
txtBudget
As
TextBox =
CType
(e.Item.FindControl(
"txtAddBudget"
), TextBox)
073.
074.
Dim
txtUsed
As
TextBox =
CType
(e.Item.FindControl(
"txtAddUsed"
), TextBox)
075.
076.
strSQL =
"INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) "
& _
077.
" VALUES ('"
& txtCustomerID.Text &
"','"
& txtName.Text &
"','"
& txtEmail.Text &
"' "
& _
078.
" ,'"
& txtCountryCode.Text &
"','"
& txtBudget.Text &
"','"
& txtUsed.Text &
"') "
079.
objCmd =
New
OleDbCommand(strSQL, objConn)
080.
objCmd.ExecuteNonQuery()
081.
082.
BindData()
083.
End
If
084.
End
Sub
085.
086.
087.
Protected
Sub
myDataGrid_UpdateCommand(
ByVal
source
As
Object
,
ByVal
e
As
System.Web.UI.WebControls.DataGridCommandEventArgs)
Handles
myDataGrid.UpdateCommand
088.
089.
Dim
txtCustomerID
As
TextBox =
CType
(e.Item.FindControl(
"txtEditCustomerID"
), TextBox)
090.
091.
Dim
txtName
As
TextBox =
CType
(e.Item.FindControl(
"txtEditName"
), TextBox)
092.
093.
Dim
txtEmail
As
TextBox =
CType
(e.Item.FindControl(
"txtEditEmail"
), TextBox)
094.
095.
Dim
txtCountryCode
As
TextBox =
CType
(e.Item.FindControl(
"txtEditCountryCode"
), TextBox)
096.
097.
Dim
txtBudget
As
TextBox =
CType
(e.Item.FindControl(
"txtEditBudget"
), TextBox)
098.
099.
Dim
txtUsed
As
TextBox =
CType
(e.Item.FindControl(
"txtEditUsed"
), TextBox)
100.
101.
strSQL =
"UPDATE customer SET CustomerID = '"
& txtCustomerID.Text &
"' "
& _
102.
" ,Name = '"
& txtName.Text &
"' "
& _
103.
" ,Email = '"
& txtEmail.Text &
"' "
& _
104.
" ,CountryCode = '"
& txtCountryCode.Text &
"' "
& _
105.
" ,Budget = '"
& txtBudget.Text &
"' "
& _
106.
" ,Used = '"
& txtUsed.Text &
"' "
& _
107.
" WHERE CustomerID = '"
& myDataGrid.DataKeys.Item(e.Item.ItemIndex) &
"'"
108.
objCmd =
New
OleDbCommand(strSQL, objConn)
109.
objCmd.ExecuteNonQuery()
110.
111.
myDataGrid.EditItemIndex = -1
112.
myDataGrid.ShowFooter =
True
113.
BindData()
114.
End
Sub
115.
End
Class