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