here is my code, for all the validation control
ASP.net
etc....
and here is my code for my mysql connection
VB.net
The thing is sometimes it works properly, then sometimes not. Then after clicking Save, I'm gonna check my database then it has the data.
ASP.net
Code:
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtName" ErrorMessage="Email is required."
SetFocusOnError="True">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator9" runat="server"
ControlToValidate="txtName"
ErrorMessage="Please input your real email address."
ValidationExpression=".*@.*\..*">abc@xyz.com</asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator17" runat="server"
ControlToValidate="txtPassword" ErrorMessage="Password is required."
SetFocusOnError="True" Display="Dynamic">*</asp:RequiredFieldValidator>
and here is my code for my mysql connection
VB.net
Code:
Imports MySql.Data.MySqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Dim ServerString As String = "Server=localhost;User Id=root;Password=**;Database=aa"
Dim SQLConnection As MySqlConnection = New MySqlConnection
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
SQLConnection.ConnectionString = ServerString
Try
If SQLConnection.State = Data.ConnectionState.Closed Then
SQLConnection.Open()
MsgBox("Succesfully connected to MySQL database!")
Else
SQLConnection.Close()
MsgBox("Connection is closed!")
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Protected Sub cmdSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSave.Click
Dim SQLStatement As String = "INSERT INTO tbluserinfo(userName,userPassword,userFname,userMname,userLname,userBmonth,userBday,userByear,userGender,userCstatus,userCitizenship,userCountry,userOccupation,userSquestion,userSanswer) VALUES('" & txtName.Text & "', '" & txtPassword.Text & "', '" & txtFname.Text & "', '" & txtMname.Text & "', '" & txtLname.Text & "','" & cmbBmonth.Text & "','" & cmbBday.Text & "','" & cmbByear.Text & "','" & cmbGender.Text & "','" & cmbCstatus.Text & "','" & txtCitizenship.Text & "','" & txtCountry.Text & "','" & txtOccupation.Text & "','" & cmbSquestion.Text & "','" & txtSanswer.Text & "' )"
SaveNames(SQLStatement)
End Sub
Public Sub SaveNames(ByRef SQLStatement As String)
Dim cmd As MySqlCommand = New MySqlCommand
With cmd
.CommandText = SQLStatement
.CommandType = Data.CommandType.Text
.Connection = SQLConnection
.ExecuteNonQuery()
End With
SQLConnection.Close()
MsgBox("Succesfully Added!")
SQLConnection.Dispose()
End Sub
End Class
Comment