001.
Imports
System.Data
002.
Imports
System.Data.OleDb
003.
Partial
Class
DetailsView1
004.
Inherits
System.Web.UI.Page
005.
006.
Dim
objConn
As
OleDbConnection
007.
Dim
objCmd
As
OleDbCommand
008.
Dim
strSQL
As
String
009.
Dim
strCusID
As
String
=
"C001"
010.
011.
Protected
Sub
Page_Load(
ByVal
sender
As
Object
,
ByVal
e
As
System.EventArgs)
Handles
Me
.Load
012.
Dim
strConnString
As
String
013.
strConnString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
& Server.MapPath(
"database/mydatabase.mdb"
) &
";"
014.
objConn =
New
OleDbConnection(strConnString)
015.
objConn.Open()
016.
017.
If
Not
Page.IsPostBack()
Then
018.
BindData()
019.
End
If
020.
End
Sub
021.
022.
Protected
Sub
BindData()
023.
strSQL =
"SELECT * FROM customer WHERE CustomerID = '"
& strCusID &
"' "
024.
025.
Dim
dtReader
As
OleDbDataReader
026.
objCmd =
New
OleDbCommand(strSQL, objConn)
027.
dtReader = objCmd.ExecuteReader()
028.
029.
030.
myDetailsView.DataSource = dtReader
031.
myDetailsView.DataBind()
032.
033.
dtReader.Close()
034.
dtReader =
Nothing
035.
036.
End
Sub
037.
038.
Protected
Sub
Page_Unload(
ByVal
sender
As
Object
,
ByVal
e
As
System.EventArgs)
Handles
Me
.Unload
039.
objConn.Close()
040.
objConn =
Nothing
041.
End
Sub
042.
043.
Protected
Sub
myDetailsView_ItemInserting(
ByVal
sender
As
Object
,
ByVal
e
As
DetailsViewInsertEventArgs)
Handles
myDetailsView.ItemInserting
044.
045.
Dim
txtCustomerID
As
TextBox =
CType
(myDetailsView.FindControl(
"txtAddCustomerID"
), TextBox)
046.
047.
Dim
txtName
As
TextBox =
CType
(myDetailsView.FindControl(
"txtAddName"
), TextBox)
048.
049.
Dim
txtEmail
As
TextBox =
CType
(myDetailsView.FindControl(
"txtAddEmail"
), TextBox)
050.
051.
Dim
txtCountryCode
As
TextBox =
CType
(myDetailsView.FindControl(
"txtAddCountryCode"
), TextBox)
052.
053.
Dim
txtBudget
As
TextBox =
CType
(myDetailsView.FindControl(
"txtAddBudget"
), TextBox)
054.
055.
Dim
txtUsed
As
TextBox =
CType
(myDetailsView.FindControl(
"txtAddUsed"
), TextBox)
056.
057.
strSQL =
"INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) "
& _
058.
" VALUES ('"
& txtCustomerID.Text &
"','"
& txtName.Text &
"','"
& txtEmail.Text &
"' "
& _
059.
" ,'"
& txtCountryCode.Text &
"','"
& txtBudget.Text &
"','"
& txtUsed.Text &
"') "
060.
objCmd =
New
OleDbCommand(strSQL, objConn)
061.
objCmd.ExecuteNonQuery()
062.
063.
strCusID = txtCustomerID.Text
064.
myDetailsView.ChangeMode(DetailsViewMode.
ReadOnly
)
065.
BindData()
066.
End
Sub
067.
068.
Protected
Sub
myDetailsView_ItemUpdating(
ByVal
sender
As
Object
,
ByVal
e
As
DetailsViewUpdateEventArgs)
Handles
myDetailsView.ItemUpdating
069.
070.
Dim
txtCustomerID
As
TextBox =
CType
(myDetailsView.FindControl(
"txtEditCustomerID"
), TextBox)
071.
072.
Dim
txtName
As
TextBox =
CType
(myDetailsView.FindControl(
"txtEditName"
), TextBox)
073.
074.
Dim
txtEmail
As
TextBox =
CType
(myDetailsView.FindControl(
"txtEditEmail"
), TextBox)
075.
076.
Dim
txtCountryCode
As
TextBox =
CType
(myDetailsView.FindControl(
"txtEditCountryCode"
), TextBox)
077.
078.
Dim
txtBudget
As
TextBox =
CType
(myDetailsView.FindControl(
"txtEditBudget"
), TextBox)
079.
080.
Dim
txtUsed
As
TextBox =
CType
(myDetailsView.FindControl(
"txtEditUsed"
), TextBox)
081.
082.
strSQL =
"UPDATE customer SET CustomerID = '"
& txtCustomerID.Text &
"' "
& _
083.
" ,Name = '"
& txtName.Text &
"' "
& _
084.
" ,Email = '"
& txtEmail.Text &
"' "
& _
085.
" ,CountryCode = '"
& txtCountryCode.Text &
"' "
& _
086.
" ,Budget = '"
& txtBudget.Text &
"' "
& _
087.
" ,Used = '"
& txtUsed.Text &
"' "
& _
088.
" WHERE CustomerID = '"
& strCusID &
"'"
089.
objCmd =
New
OleDbCommand(strSQL, objConn)
090.
objCmd.ExecuteNonQuery()
091.
092.
myDetailsView.ChangeMode(DetailsViewMode.
ReadOnly
)
093.
BindData()
094.
End
Sub
095.
096.
Protected
Sub
myDetailsView_ModeChanging(
ByVal
sender
As
Object
,
ByVal
e
As
DetailsViewModeEventArgs)
Handles
myDetailsView.ModeChanging
097.
Select
Case
e.NewMode
098.
Case
DetailsViewMode.Edit
099.
myDetailsView.ChangeMode(DetailsViewMode.Edit)
100.
Case
DetailsViewMode.
ReadOnly
101.
myDetailsView.ChangeMode(DetailsViewMode.
ReadOnly
)
102.
Case
DetailsViewMode.Insert
103.
myDetailsView.ChangeMode(DetailsViewMode.Insert)
104.
End
Select
105.
BindData()
106.
End
Sub
107.
108.
End
Class