001.
Imports
System.Data
002.
Imports
System.Data.OleDb
003.
Partial
Class
DetailsView_GridView
004.
Inherits
System.Web.UI.Page
005.
Dim
objConn
As
OleDbConnection
006.
Dim
objCmd
As
OleDbCommand
007.
Dim
strSQL
As
String
008.
Dim
strCusID
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.
GridViewBindData()
018.
End
If
019.
End
Sub
020.
021.
Protected
Sub
GridViewBindData()
022.
strSQL =
"SELECT * FROM customer"
023.
024.
Dim
dtReader
As
OleDbDataReader
025.
objCmd =
New
OleDbCommand(strSQL, objConn)
026.
dtReader = objCmd.ExecuteReader()
027.
028.
029.
myGridView.DataSource = dtReader
030.
myGridView.DataBind()
031.
032.
dtReader.Close()
033.
dtReader =
Nothing
034.
End
Sub
035.
036.
Protected
Sub
DetailsViewBindData()
037.
strSQL =
"SELECT * FROM customer WHERE CustomerID = '"
& strCusID &
"' "
038.
Dim
dtReader
As
OleDbDataReader
039.
objCmd =
New
OleDbCommand(strSQL, objConn)
040.
dtReader = objCmd.ExecuteReader()
041.
042.
043.
myDetailsView.DataSource = dtReader
044.
myDetailsView.DataBind()
045.
046.
dtReader.Close()
047.
dtReader =
Nothing
048.
049.
End
Sub
050.
051.
Protected
Sub
Page_Unload(
ByVal
sender
As
Object
,
ByVal
e
As
System.EventArgs)
Handles
Me
.Unload
052.
objConn.Close()
053.
objConn =
Nothing
054.
End
Sub
055.
056.
057.
Protected
Sub
myGridView_RowEditing(
ByVal
sender
As
Object
,
ByVal
e
As
GridViewEditEventArgs)
Handles
myGridView.RowEditing
058.
myGridView.Visible =
False
059.
strCusID = myGridView.DataKeys(e.NewEditIndex).Value.ToString
060.
myDetailsView.Visible =
True
061.
DetailsViewBindData()
062.
End
Sub
063.
064.
Protected
Sub
myDetailsView_ModeChanging(
ByVal
sender
As
Object
,
ByVal
e
As
DetailsViewModeEventArgs)
Handles
myDetailsView.ModeChanging
065.
Select
Case
e.NewMode
066.
Case
DetailsViewMode.Edit
067.
Dim
lblCusID
As
Label =
CType
(myDetailsView.FindControl(
"lblCusID"
), Label)
068.
strCusID = lblCusID.Text
069.
myDetailsView.ChangeMode(DetailsViewMode.Edit)
070.
DetailsViewBindData()
071.
Case
DetailsViewMode.
ReadOnly
072.
myDetailsView.ChangeMode(DetailsViewMode.
ReadOnly
)
073.
myDetailsView.Visible =
False
074.
myGridView.Visible =
True
075.
GridViewBindData()
076.
End
Select
077.
End
Sub
078.
079.
Protected
Sub
myDetailsView_ItemUpdating(
ByVal
sender
As
Object
,
ByVal
e
As
DetailsViewUpdateEventArgs)
Handles
myDetailsView.ItemUpdating
080.
081.
Dim
lblCusID
As
Label =
CType
(myDetailsView.FindControl(
"lblCusID"
), Label)
082.
strCusID = lblCusID.Text
083.
084.
Dim
txtCustomerID
As
TextBox =
CType
(myDetailsView.FindControl(
"txtEditCustomerID"
), TextBox)
085.
086.
Dim
txtName
As
TextBox =
CType
(myDetailsView.FindControl(
"txtEditName"
), TextBox)
087.
088.
Dim
txtEmail
As
TextBox =
CType
(myDetailsView.FindControl(
"txtEditEmail"
), TextBox)
089.
090.
Dim
txtCountryCode
As
TextBox =
CType
(myDetailsView.FindControl(
"txtEditCountryCode"
), TextBox)
091.
092.
Dim
txtBudget
As
TextBox =
CType
(myDetailsView.FindControl(
"txtEditBudget"
), TextBox)
093.
094.
Dim
txtUsed
As
TextBox =
CType
(myDetailsView.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 = '"
& strCusID &
"'"
103.
104.
objCmd =
New
OleDbCommand(strSQL, objConn)
105.
objCmd.ExecuteNonQuery()
106.
myDetailsView.ChangeMode(DetailsViewMode.
ReadOnly
)
107.
myDetailsView.Visible =
False
108.
myGridView.Visible =
True
109.
GridViewBindData()
110.
End
Sub
111.
112.
End
Class