001.
<%@ Page Language=
"VB"
%>
002.
<%@ import
Namespace
=
"System.Data"
%>
003.
<%@ import
Namespace
=
"MySql.Data.MySqlClient"
%>
004.
<script runat=
"server"
>
005.
Dim
objConn
As
New
MySqlConnection
006.
Dim
objCmd
As
New
MySqlCommand
007.
Dim
strConnString,strSQL
As
String
008.
009.
Sub
Page_Load(sender
As
Object
, e
As
EventArgs)
010.
strConnString =
"Server=localhost;User Id=root; Password=root; Database=mydatabase; Pooling=false"
011.
objConn.ConnectionString = strConnString
012.
objConn.Open()
013.
End
Sub
014.
015.
Sub
btnSave_Click(sender
As
Object
, e
As
EventArgs)
016.
Dim
intNumRows
As
Integer
017.
strSQL =
"SELECT * FROM customer WHERE CustomerID = '"
&
Me
.txtCustomerID.Text &
"' "
018.
objCmd =
New
MySqlCommand(strSQL, objConn)
019.
intNumRows = objCmd.ExecuteScalar()
020.
021.
IF intNumRows > 0
Then
022.
Me
.pnlAdd.Visible =
False
023.
Me
.lblStatus.Visible =
True
024.
Me
.lblStatus.Text =
"CustomerID already exist."
025.
Else
026.
strSQL =
"INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) "
& _
027.
" VALUES "
& _
028.
" ('"
&
Me
.txtCustomerID.Text &
"','"
&
Me
.txtName.Text &
"','"
&
Me
.txtEmail.Text &
"', "
& _
029.
" '"
&
Me
.txtCountryCode.Text &
"','"
&
Me
.txtBudget.Text &
"','"
&
Me
.txtUsed.Text &
"')"
030.
031.
objCmd =
New
MySqlCommand
032.
With
objCmd
033.
.Connection = objConn
034.
.CommandText = strSQL
035.
.CommandType = CommandType.Text
036.
End
With
037.
038.
Me
.pnlAdd.Visible =
False
039.
Try
040.
objCmd.ExecuteNonQuery()
041.
Me
.lblStatus.Text =
"Record Inserted"
042.
Me
.lblStatus.Visible =
True
043.
Catch
ex
As
Exception
044.
Me
.lblStatus.Visible =
True
045.
Me
.lblStatus.Text =
"Record can not insert Error ("
& ex.Message &
")"
046.
End
Try
047.
048.
End
IF
049.
050.
End
Sub
051.
052.
Sub
Page_UnLoad()
053.
objConn.Close()
054.
objConn =
Nothing
055.
End
Sub
056.
057.
</script>
058.
<html>
059.
<head>
060.
<title>ThaiCreate.Com ASP.NET - MySQL</title>
061.
</head>
062.
<body>
063.
<form id=
"form1"
runat=
"server"
>
064.
<asp:Panel id=
"pnlAdd"
runat=
"server"
>
065.
<table width=
"353"
border=
"1"
>
066.
<tbody>
067.
<tr>
068.
<td width=
"102"
>
069.
<asp:Label id=
"lblCustomerID"
runat=
"server"
text=
"CustomerID"
></asp:Label></td>
070.
<td width=
"235"
>
071.
<asp:TextBox id=
"txtCustomerID"
runat=
"server"
Width=
"79px"
></asp:TextBox>
072.
</td>
073.
</tr>
074.
<tr>
075.
<td>
076.
<asp:Label id=
"lblName"
runat=
"server"
text=
"Name"
></asp:Label></td>
077.
<td>
078.
<asp:TextBox id=
"txtName"
runat=
"server"
Width=
"177px"
></asp:TextBox>
079.
</td>
080.
</tr>
081.
<tr>
082.
<td>
083.
<asp:Label id=
"lblEmail"
runat=
"server"
text=
"Email"
></asp:Label></td>
084.
<td>
085.
<asp:TextBox id=
"txtEmail"
runat=
"server"
Width=
"155px"
></asp:TextBox>
086.
</td>
087.
</tr>
088.
<tr>
089.
<td>
090.
<asp:Label id=
"lblCountryCode"
runat=
"server"
text=
"CountryCode"
></asp:Label></td>
091.
<td>
092.
<asp:TextBox id=
"txtCountryCode"
runat=
"server"
Width=
"38px"
></asp:TextBox>
093.
</td>
094.
</tr>
095.
<tr>
096.
<td>
097.
<asp:Label id=
"lblBudget"
runat=
"server"
text=
"Budget"
></asp:Label></td>
098.
<td>
099.
<asp:TextBox id=
"txtBudget"
runat=
"server"
Width=
"76px"
></asp:TextBox>
100.
</td>
101.
</tr>
102.
<tr>
103.
<td>
104.
<asp:Label id=
"lblUsed"
runat=
"server"
text=
"Used"
></asp:Label></td>
105.
<td>
106.
<asp:TextBox id=
"txtUsed"
runat=
"server"
Width=
"76px"
></asp:TextBox>
107.
</td>
108.
</tr>
109.
</tbody>
110.
</table>
111.
<br />
112.
<asp:Button id=
"btnSave"
onclick=
"btnSave_Click"
runat=
"server"
Text=
"Save"
></asp:Button>
113.
<br />
114.
</asp:Panel>
115.
<asp:Label id=
"lblStatus"
runat=
"server"
visible=
"False"
></asp:Label>
116.
</form>
117.
</body>
118.
</html>