001.
Partial
Class
AspNetAccessDataSourceVS2005
002.
Inherits
System.Web.UI.Page
003.
Dim
myDSource
As
AccessDataSource
004.
Dim
strSQL
As
String
005.
Protected
Sub
Page_Load(
ByVal
sender
As
Object
,
ByVal
e
As
System.EventArgs)
Handles
Me
.Load
006.
myDSource =
New
AccessDataSource()
007.
myDSource.DataFile =
"~/database/mydatabase.mdb"
008.
009.
If
Not
Page.IsPostBack()
Then
010.
BindData()
011.
End
If
012.
End
Sub
013.
014.
Private
Sub
BindData()
015.
016.
With
myDSource
017.
.SelectCommand =
"SELECT * FROM [customer]"
018.
End
With
019.
020.
021.
myGridView.DataSource = myDSource
022.
myGridView.DataBind()
023.
024.
myDSource =
Nothing
025.
026.
End
Sub
027.
028.
Protected
Sub
myGridView_RowCancelingEdit(
ByVal
sender
As
Object
,
ByVal
e
As
GridViewCancelEditEventArgs)
Handles
myGridView.RowCancelingEdit
029.
myGridView.EditIndex = -1
030.
myGridView.ShowFooter =
True
031.
BindData()
032.
End
Sub
033.
034.
Protected
Sub
myGridView_RowCommand(
ByVal
sender
As
Object
,
ByVal
e
As
GridViewCommandEventArgs)
Handles
myGridView.RowCommand
035.
If
e.CommandName =
"Add"
Then
036.
037.
Dim
txtCustomerID
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddCustomerID"
), TextBox)
038.
039.
Dim
txtName
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddName"
), TextBox)
040.
041.
Dim
txtEmail
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddEmail"
), TextBox)
042.
043.
Dim
txtCountryCode
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddCountryCode"
), TextBox)
044.
045.
Dim
txtBudget
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddBudget"
), TextBox)
046.
047.
Dim
txtUsed
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddUsed"
), TextBox)
048.
049.
strSQL =
"INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) "
& _
050.
" VALUES ('"
& txtCustomerID.Text &
"','"
& txtName.Text &
"','"
& txtEmail.Text &
"' "
& _
051.
" ,'"
& txtCountryCode.Text &
"','"
& txtBudget.Text &
"','"
& txtUsed.Text &
"') "
052.
053.
With
myDSource
054.
.InsertCommand = strSQL
055.
.InsertCommandType = SqlDataSourceCommandType.Text
056.
.Insert()
057.
End
With
058.
059.
BindData()
060.
End
If
061.
End
Sub
062.
063.
Protected
Sub
myGridView_RowDeleting(
ByVal
sender
As
Object
,
ByVal
e
As
GridViewDeleteEventArgs)
Handles
myGridView.RowDeleting
064.
strSQL =
"DELETE FROM customer WHERE CustomerID = '"
& myGridView.DataKeys.Item(e.RowIndex).Value &
"'"
065.
066.
With
myDSource
067.
.DeleteCommand = strSQL
068.
.DeleteCommandType = SqlDataSourceCommandType.Text
069.
.Delete()
070.
End
With
071.
072.
myGridView.EditIndex = -1
073.
BindData()
074.
End
Sub
075.
076.
Protected
Sub
myGridView_RowEditing(
ByVal
sender
As
Object
,
ByVal
e
As
GridViewEditEventArgs)
Handles
myGridView.RowEditing
077.
myGridView.EditIndex = e.NewEditIndex
078.
myGridView.ShowFooter =
False
079.
BindData()
080.
End
Sub
081.
082.
Protected
Sub
myGridView_RowUpdating(
ByVal
sender
As
Object
,
ByVal
e
As
GridViewUpdateEventArgs)
Handles
myGridView.RowUpdating
083.
084.
Dim
txtCustomerID
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditCustomerID"
), TextBox)
085.
086.
Dim
txtName
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditName"
), TextBox)
087.
088.
Dim
txtEmail
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditEmail"
), TextBox)
089.
090.
Dim
txtCountryCode
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditCountryCode"
), TextBox)
091.
092.
Dim
txtBudget
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditBudget"
), TextBox)
093.
094.
Dim
txtUsed
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditUsed"
), TextBox)
095.
096.
strSQL =
"UPDATE customer SET CustomerID = '"
& txtCustomerID.Text &
"' "
& _
097.
" ,Name = '"
& txtName.Text &
"' "
& _
098.
" ,Email = '"
& txtEmail.Text &
"' "
& _
099.
" ,CountryCode = '"
& txtCountryCode.Text &
"' "
& _
100.
" ,Budget = '"
& txtBudget.Text &
"' "
& _
101.
" ,Used = '"
& txtUsed.Text &
"' "
& _
102.
" WHERE CustomerID = '"
& myGridView.DataKeys.Item(e.RowIndex).Value &
"'"
103.
104.
With
myDSource
105.
.UpdateCommand = strSQL
106.
.UpdateCommandType = SqlDataSourceCommandType.Text
107.
.Update()
108.
End
With
109.
110.
myGridView.EditIndex = -1
111.
myGridView.ShowFooter =
True
112.
BindData()
113.
End
Sub
114.
End
Class