001.
<%@ Page Language=
"VB"
%>
002.
<%@ import
Namespace
=
"System.Data"
%>
003.
<%@ import
Namespace
=
"System.Data.Odbc"
%>
004.
<script runat=
"server"
>
005.
Dim
objConn
As
OdbcConnection
006.
Dim
objCmd
As
OdbcCommand
007.
Dim
strSQL
As
String
008.
009.
Sub
Page_Load(sender
As
Object
, e
As
EventArgs)
010.
Dim
strConnString
As
String
011.
strConnString =
"Driver={MySQL ODBC 5.1 Driver}; SERVER=localhost;UID=root;PWD=root;database=mydatabase;option=16384;"
012.
objConn =
New
OdbcConnection(strConnString)
013.
objConn.Open()
014.
015.
IF
Not
Page.IsPostBack()
Then
016.
BindData()
017.
End
IF
018.
End
Sub
019.
020.
Sub
BindData()
021.
strSQL =
"SELECT * FROM customer"
022.
023.
Dim
dtReader
As
OdbcDataReader
024.
objCmd =
New
OdbcCommand(strSQL, objConn)
025.
dtReader = objCmd.ExecuteReader()
026.
027.
028.
myGridView.DataSource = dtReader
029.
myGridView.DataBind()
030.
031.
dtReader.Close()
032.
dtReader =
Nothing
033.
034.
End
Sub
035.
036.
Sub
Page_UnLoad()
037.
objConn.Close()
038.
objConn =
Nothing
039.
End
Sub
040.
041.
Sub
modEditCommand(sender
As
Object
, e
As
GridViewEditEventArgs)
042.
myGridView.EditIndex = e.NewEditIndex
043.
myGridView.ShowFooter =
False
044.
BindData()
045.
End
Sub
046.
047.
Sub
modCancelCommand(sender
As
Object
, e
As
GridViewCancelEditEventArgs)
048.
myGridView.EditIndex = -1
049.
myGridView.ShowFooter =
True
050.
BindData()
051.
End
Sub
052.
053.
Sub
modDeleteCommand(sender
As
Object
, e
As
GridViewDeleteEventArgs)
054.
strSQL =
"DELETE FROM customer WHERE CustomerID = '"
& myGridView.DataKeys.Item(e.RowIndex).Value &
"'"
055.
objCmd =
New
OdbcCommand(strSQL, objConn)
056.
objCmd.ExecuteNonQuery()
057.
058.
myGridView.EditIndex = -1
059.
BindData()
060.
End
Sub
061.
062.
063.
Sub
myGridView_RowCommand(source
As
Object
, e
As
GridViewCommandEventArgs)
064.
If
e.CommandName =
"Add"
Then
065.
066.
Dim
txtCustomerID
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddCustomerID"
), TextBox)
067.
068.
Dim
txtName
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddName"
), TextBox)
069.
070.
Dim
txtEmail
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddEmail"
), TextBox)
071.
072.
Dim
txtCountryCode
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddCountryCode"
), TextBox)
073.
074.
Dim
txtBudget
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddBudget"
), TextBox)
075.
076.
Dim
txtUsed
As
TextBox =
CType
(myGridView.FooterRow.FindControl(
"txtAddUsed"
), TextBox)
077.
078.
strSQL =
"INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) "
& _
079.
" VALUES ('"
& txtCustomerID.Text &
"','"
& txtName.Text &
"','"
& txtEmail.Text &
"' "
& _
080.
" ,'"
& txtCountryCode.Text &
"','"
& txtBudget.Text &
"','"
& txtUsed.Text &
"') "
081.
objCmd =
New
OdbcCommand(strSQL, objConn)
082.
objCmd.ExecuteNonQuery()
083.
084.
BindData()
085.
End
If
086.
End
Sub
087.
088.
089.
Sub
modUpdateCommand(s
As
Object
, e
As
GridViewUpdateEventArgs)
090.
091.
092.
Dim
txtCustomerID
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditCustomerID"
), TextBox)
093.
094.
Dim
txtName
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditName"
), TextBox)
095.
096.
Dim
txtEmail
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditEmail"
), TextBox)
097.
098.
Dim
txtCountryCode
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditCountryCode"
), TextBox)
099.
100.
Dim
txtBudget
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditBudget"
), TextBox)
101.
102.
Dim
txtUsed
As
TextBox =
CType
(myGridView.Rows(e.RowIndex).FindControl(
"txtEditUsed"
), TextBox)
103.
104.
strSQL =
"UPDATE customer SET CustomerID = '"
& txtCustomerID.Text &
"' "
& _
105.
" ,Name = '"
& txtName.Text &
"' "
& _
106.
" ,Email = '"
& txtEmail.Text &
"' "
& _
107.
" ,CountryCode = '"
& txtCountryCode.Text &
"' "
& _
108.
" ,Budget = '"
& txtBudget.Text &
"' "
& _
109.
" ,Used = '"
& txtUsed.Text &
"' "
& _
110.
" WHERE CustomerID = '"
& myGridView.DataKeys.Item(e.RowIndex).Value &
"'"
111.
objCmd =
New
OdbcCommand(strSQL, objConn)
112.
objCmd.ExecuteNonQuery()
113.
114.
myGridView.EditIndex = -1
115.
myGridView.ShowFooter =
True
116.
BindData()
117.
End
Sub
118.
119.
</script>
120.
<html>
121.
<head>
122.
<title>ThaiCreate.Com ASP.NET - System.Data.Odbc</title>
123.
</head>
124.
<body>
125.
<form id=
"form1"
runat=
"server"
>
126.
<asp:GridView id=
"myGridView"
runat=
"server"
AutoGenerateColumns=
"False"
127.
ShowFooter=
"True"
128.
DataKeyNames=
"CustomerID"
129.
OnRowEditing=
"modEditCommand"
130.
OnRowCancelingEdit=
"modCancelCommand"
131.
OnRowDeleting=
"modDeleteCommand"
132.
OnRowUpdating=
"modUpdateCommand"
133.
OnRowCommand=
"myGridView_RowCommand"
>
134.
135.
<Columns>
136.
137.
<asp:TemplateField HeaderText=
"CustomerID"
>
138.
<ItemTemplate>
139.
<asp:Label id=
"lblCustomerID"
runat=
"server"
Text=
140.
</ItemTemplate>
141.
<EditItemTemplate>
142.
<asp:TextBox id=
"txtEditCustomerID"
size=
"5"
runat=
"server"
Text=
143.
</EditItemTemplate>
144.
<FooterTemplate>
145.
<asp:TextBox id=
"txtAddCustomerID"
size=
"5"
runat=
"server"
></asp:TextBox>
146.
</FooterTemplate>
147.
</asp:TemplateField>
148.
149.
<asp:TemplateField HeaderText=
"Name"
>
150.
<ItemTemplate>
151.
<asp:Label id=
"lblName"
runat=
"server"
Text=
152.
</ItemTemplate>
153.
<EditItemTemplate>
154.
<asp:TextBox id=
"txtEditName"
size=
"10"
runat=
"server"
Text=
155.
</EditItemTemplate>
156.
<FooterTemplate>
157.
<asp:TextBox id=
"txtAddName"
size=
"10"
runat=
"server"
></asp:TextBox>
158.
</FooterTemplate>
159.
</asp:TemplateField>
160.
161.
<asp:TemplateField HeaderText=
"Email"
>
162.
<ItemTemplate>
163.
<asp:Label id=
"lblEmail"
runat=
"server"
Text=
164.
</ItemTemplate>
165.
<EditItemTemplate>
166.
<asp:TextBox id=
"txtEditEmail"
size=
"20"
runat=
"server"
Text=
167.
</EditItemTemplate>
168.
<FooterTemplate>
169.
<asp:TextBox id=
"txtAddEmail"
size=
"20"
runat=
"server"
></asp:TextBox>
170.
</FooterTemplate>
171.
</asp:TemplateField>
172.
173.
<asp:TemplateField HeaderText=
"CountryCode"
>
174.
<ItemTemplate>
175.
<asp:Label id=
"lblCountryCode"
runat=
"server"
Text=
176.
</ItemTemplate>
177.
<EditItemTemplate>
178.
<asp:TextBox id=
"txtEditCountryCode"
size=
"2"
runat=
"server"
Text=
179.
</EditItemTemplate>
180.
<FooterTemplate>
181.
<asp:TextBox id=
"txtAddCountryCode"
size=
"2"
runat=
"server"
></asp:TextBox>
182.
</FooterTemplate>
183.
</asp:TemplateField>
184.
185.
<asp:TemplateField HeaderText=
"Budget"
>
186.
<ItemTemplate>
187.
<asp:Label id=
"lblBudget"
runat=
"server"
Text=
188.
</ItemTemplate>
189.
<EditItemTemplate>
190.
<asp:TextBox id=
"txtEditBudget"
size=
"6"
runat=
"server"
Text=
191.
</EditItemTemplate>
192.
<FooterTemplate>
193.
<asp:TextBox id=
"txtAddBudget"
size=
"6"
runat=
"server"
></asp:TextBox>
194.
</FooterTemplate>
195.
</asp:TemplateField>
196.
197.
<asp:TemplateField HeaderText=
"Used"
>
198.
<ItemTemplate>
199.
<asp:Label id=
"lblUsed"
runat=
"server"
Text=
200.
</ItemTemplate>
201.
<EditItemTemplate>
202.
<asp:TextBox id=
"txtEditUsed"
size=
"6"
runat=
"server"
Text=
203.
</EditItemTemplate>
204.
<FooterTemplate>
205.
<asp:TextBox id=
"txtAddUsed"
size=
"6"
runat=
"server"
></asp:TextBox>
206.
<asp:Button id=
"btnAdd"
runat=
"server"
Text=
"Add"
CommandName=
"Add"
></asp:Button>
207.
</FooterTemplate>
208.
</asp:TemplateField>
209.
210.
<asp:CommandField ShowEditButton=
"True"
CancelText=
"Cancel"
DeleteText=
"Delete"
EditText=
"Edit"
UpdateText=
"Update"
HeaderText=
"Modify"
/>
211.
<asp:CommandField ShowDeleteButton=
"True"
HeaderText=
"Delete"
/>
212.
213.
</Columns>
214.
</asp:GridView>
215.
</form>
216.
</body>
217.
</html>