001.
Imports
System.Data
002.
Imports
System.Data.OleDb
003.
Partial
Class
GridView1
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.
Sub
BindData()
019.
strSQL =
"SELECT * FROM customer"
020.
021.
Dim
dtReader
As
OleDbDataReader
022.
objCmd =
New
OleDbCommand(strSQL, objConn)
023.
dtReader = objCmd.ExecuteReader()
024.
025.
026.
myGridView.DataSource = dtReader
027.
myGridView.DataBind()
028.
029.
dtReader.Close()
030.
dtReader =
Nothing
031.
032.
End
Sub
033.
034.
Protected
Sub
Page_Unload(
ByVal
sender
As
Object
,
ByVal
e
As
System.EventArgs)
Handles
Me
.Unload
035.
objConn.Close()
036.
objConn =
Nothing
037.
End
Sub
038.
039.
Protected
Sub
myGridView_RowCancelingEdit(
ByVal
sender
As
Object
,
ByVal
e
As
System.Web.UI.WebControls.GridViewCancelEditEventArgs)
Handles
myGridView.RowCancelingEdit
040.
myGridView.EditIndex = -1
041.
myGridView.ShowFooter =
True
042.
BindData()
043.
End
Sub
044.
045.
Protected
Sub
myGridView_RowCommand(
ByVal
sender
As
Object
,
ByVal
e
As
System.Web.UI.WebControls.GridViewCommandEventArgs)
Handles
myGridView.RowCommand
046.
If
e.CommandName =
"Add"
Then
047.
048.
Dim
txtCustomerID
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddCustomerID"
), TextBox)
049.
050.
Dim
txtName
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddName"
), TextBox)
051.
052.
Dim
txtEmail
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddEmail"
), TextBox)
053.
054.
Dim
txtCountryCode
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddCountryCode"
), TextBox)
055.
056.
Dim
txtBudget
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddBudget"
), TextBox)
057.
058.
Dim
txtUsed
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddUsed"
), TextBox)
059.
060.
strSQL =
"INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) "
& _
061.
" VALUES ('"
& txtCustomerID.Text &
"','"
& txtName.Text &
"','"
& txtEmail.Text &
"' "
& _
062.
" ,'"
& txtCountryCode.Text &
"','"
& txtBudget.Text &
"','"
& txtUsed.Text &
"') "
063.
objCmd =
New
OleDbCommand(strSQL, objConn)
064.
objCmd.ExecuteNonQuery()
065.
066.
BindData()
067.
End
If
068.
End
Sub
069.
070.
071.
072.
Protected
Sub
myGridView_RowDeleting(
ByVal
sender
As
Object
,
ByVal
e
As
System.Web.UI.WebControls.GridViewDeleteEventArgs)
Handles
myGridView.RowDeleting
073.
strSQL =
"DELETE FROM customer WHERE CustomerID = '"
& myGridView.DataKeys.Item(e.RowIndex).Value &
"'"
074.
objCmd =
New
OleDbCommand(strSQL, objConn)
075.
objCmd.ExecuteNonQuery()
076.
077.
myGridView.EditIndex = -1
078.
BindData()
079.
End
Sub
080.
081.
Protected
Sub
myGridView_RowEditing(
ByVal
sender
As
Object
,
ByVal
e
As
System.Web.UI.WebControls.GridViewEditEventArgs)
Handles
myGridView.RowEditing
082.
myGridView.EditIndex = e.NewEditIndex
083.
myGridView.ShowFooter =
False
084.
BindData()
085.
End
Sub
086.
087.
088.
Protected
Sub
myGridView_RowUpdating(
ByVal
sender
As
Object
,
ByVal
e
As
System.Web.UI.WebControls.GridViewUpdateEventArgs)
Handles
myGridView.RowUpdating
089.
090.
Dim
txtCustomerID
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditCustomerID"
), TextBox)
091.
092.
Dim
txtName
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditName"
), TextBox)
093.
094.
Dim
txtEmail
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditEmail"
), TextBox)
095.
096.
Dim
txtCountryCode
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditCountryCode"
), TextBox)
097.
098.
Dim
txtBudget
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditBudget"
), TextBox)
099.
100.
Dim
txtUsed
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditUsed"
), TextBox)
101.
102.
strSQL =
"UPDATE customer SET CustomerID = '"
& txtCustomerID.Text &
"' "
& _
103.
" ,Name = '"
& txtName.Text &
"' "
& _
104.
" ,Email = '"
& txtEmail.Text &
"' "
& _
105.
" ,CountryCode = '"
& txtCountryCode.Text &
"' "
& _
106.
" ,Budget = '"
& txtBudget.Text &
"' "
& _
107.
" ,Used = '"
& txtUsed.Text &
"' "
& _
108.
" WHERE CustomerID = '"
& myGridView.DataKeys.Item(e.RowIndex).Value &
"'"
109.
objCmd =
New
OleDbCommand(strSQL, objConn)
110.
objCmd.ExecuteNonQuery()
111.
112.
myGridView.EditIndex = -1
113.
myGridView.ShowFooter =
True
114.
BindData()
115.
End
Sub
116.
End
Class