01.
Imports
system.Data
02.
Imports
MySql.Data.MySqlClient
03.
Partial
Class
DataList1
04.
Inherits
System.Web.UI.Page
05.
Dim
objConn
As
MySqlConnection
06.
Dim
objCmd
As
MySqlCommand
07.
Dim
strSQL
As
String
08.
09.
Protected
Sub
Page_Load(
ByVal
sender
As
Object
,
ByVal
e
As
System.EventArgs)
Handles
Me
.Load
10.
Dim
strConnString
As
String
11.
strConnString =
"Server=localhost;User Id=root; Password=root; Database=mydatabase; Pooling=false"
12.
objConn =
New
MySqlConnection(strConnString)
13.
objConn.Open()
14.
15.
If
Not
Page.IsPostBack()
Then
16.
BindData()
17.
End
If
18.
End
Sub
19.
20.
Protected
Sub
BindData()
21.
strSQL =
"SELECT * FROM category"
22.
23.
Dim
dtReader
As
MySqlDataReader
24.
objCmd =
New
MySqlCommand(strSQL, objConn)
25.
dtReader = objCmd.ExecuteReader()
26.
27.
28.
myDataList.DataSource = dtReader
29.
myDataList.DataBind()
30.
31.
dtReader.Close()
32.
dtReader =
Nothing
33.
34.
End
Sub
35.
36.
Protected
Sub
Page_Unload(
ByVal
sender
As
Object
,
ByVal
e
As
System.EventArgs)
Handles
Me
.Unload
37.
objConn.Close()
38.
objConn =
Nothing
39.
End
Sub
40.
41.
Protected
Sub
myDataList_CancelCommand(
ByVal
source
As
Object
,
ByVal
e
As
DataListCommandEventArgs)
Handles
myDataList.CancelCommand
42.
myDataList.EditItemIndex = -1
43.
BindData()
44.
End
Sub
45.
46.
Protected
Sub
myDataList_EditCommand(
ByVal
source
As
Object
,
ByVal
e
As
DataListCommandEventArgs)
Handles
myDataList.EditCommand
47.
myDataList.EditItemIndex = e.Item.ItemIndex
48.
BindData()
49.
End
Sub
50.
51.
Protected
Sub
myDataList_ItemDataBound(
ByVal
sender
As
Object
,
ByVal
e
As
DataListItemEventArgs)
Handles
myDataList.ItemDataBound
52.
53.
Dim
img
As
Image =
CType
(e.Item.FindControl(
"imgPicture"
), Image)
54.
If
Not
IsNothing(img)
Then
55.
img.ImageUrl = e.Item.DataItem(
"Picture"
)
56.
57.
58.
End
If
59.
60.
61.
Dim
hplCate
As
HyperLink =
CType
(e.Item.FindControl(
"hplCategory"
), HyperLink)
62.
If
Not
IsNothing(hplCate)
Then
63.
hplCate.Text = e.Item.DataItem(
"CategoryName"
)
64.
hplCate.ToolTip = e.Item.DataItem(
"CategoryName"
)
66.
End
If
67.
End
Sub
68.
69.
Protected
Sub
myDataList_UpdateCommand(
ByVal
source
As
Object
,
ByVal
e
As
DataListCommandEventArgs)
Handles
myDataList.UpdateCommand
70.
71.
Dim
lblCateID
As
Label =
CType
(e.Item.FindControl(
"lblCateID"
), Label)
72.
73.
Dim
txtCategory
As
TextBox =
CType
(e.Item.FindControl(
"txtCategory"
), TextBox)
74.
75.
76.
strSQL =
"UPDATE category SET CategoryName = '"
& txtCategory.Text &
"' "
& _
77.
" WHERE CategoryID = "
& lblCateID.Text &
" "
78.
objCmd =
New
MySqlCommand(strSQL, objConn)
79.
objCmd.ExecuteNonQuery()
80.
81.
82.
Dim
filPicture
As
HtmlInputFile =
CType
(e.Item.FindControl(
"filPicture"
), HtmlInputFile)
83.
Dim
strFileName
As
String
84.
If
Trim(filPicture.PostedFile.FileName) <>
""
Then
85.
strFileName = System.IO.Path.GetFileName(filPicture.Value)
86.
filPicture.PostedFile.SaveAs(Server.MapPath(
"images/"
& strFileName))
87.
strSQL =
"UPDATE category SET Picture = 'images/"
& strFileName &
"' "
& _
88.
" WHERE CategoryID = "
& lblCateID.Text &
" "
89.
objCmd =
New
MySqlCommand(strSQL, objConn)
90.
objCmd.ExecuteNonQuery()
91.
End
If
92.
93.
myDataList.EditItemIndex = -1
94.
BindData()
95.
End
Sub
96.
End
Class