By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,168 Members | 2,599 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,168 IT Pros & Developers. It's quick & easy.

Validation Control, no error, but it doesn't work properly

P: 21
here is my code, for all the validation control
ASP.net
Expand|Select|Wrap|Line Numbers
  1.  
  2. <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
  3.             ControlToValidate="txtName" ErrorMessage="Email is required." 
  4.             SetFocusOnError="True">*</asp:RequiredFieldValidator>
  5.  
  6. <asp:RegularExpressionValidator ID="RegularExpressionValidator9" runat="server" 
  7.             ControlToValidate="txtName" 
  8.             ErrorMessage="Please input your real email address." 
  9.             ValidationExpression=".*@.*\..*">abc@xyz.com</asp:RegularExpressionValidator>
  10.  
  11.  
  12.  
  13. <asp:RequiredFieldValidator ID="RequiredFieldValidator17" runat="server" 
  14.             ControlToValidate="txtPassword" ErrorMessage="Password is required." 
  15.             SetFocusOnError="True" Display="Dynamic">*</asp:RequiredFieldValidator>
  16.  
  17.  
etc....


and here is my code for my mysql connection
VB.net

Expand|Select|Wrap|Line Numbers
  1. Imports MySql.Data.MySqlClient
  2. Partial Class _Default  
  3.     Inherits System.Web.UI.Page
  4.     Dim ServerString As String = "Server=localhost;User Id=root;Password=**;Database=aa"
  5.     Dim SQLConnection As MySqlConnection = New MySqlConnection
  6.  
  7.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  8.  
  9.  
  10.         SQLConnection.ConnectionString = ServerString
  11.  
  12.         Try
  13.             If SQLConnection.State = Data.ConnectionState.Closed Then
  14.                 SQLConnection.Open()
  15.                 MsgBox("Succesfully connected to MySQL database!")
  16.             Else
  17.                 SQLConnection.Close()
  18.                 MsgBox("Connection is closed!")
  19.  
  20.             End If
  21.         Catch ex As Exception
  22.             MsgBox(ex.ToString)
  23.  
  24.         End Try
  25.     End Sub
  26.  
  27.     Protected Sub cmdSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSave.Click
  28.  
  29.  
  30.  
  31.         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 & "' )"
  32.         SaveNames(SQLStatement)
  33.  
  34.  
  35.     End Sub
  36.  
  37.     Public Sub SaveNames(ByRef SQLStatement As String)
  38.         Dim cmd As MySqlCommand = New MySqlCommand
  39.  
  40.         With cmd
  41.             .CommandText = SQLStatement
  42.             .CommandType = Data.CommandType.Text
  43.             .Connection = SQLConnection
  44.             .ExecuteNonQuery()
  45.  
  46.         End With
  47.  
  48.         SQLConnection.Close()
  49.         MsgBox("Succesfully Added!")
  50.         SQLConnection.Dispose()
  51.     End Sub
  52.  
  53.  
  54. End Class
  55.  
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.
Aug 28 '12 #1
Share this Question
Share on Google+
6 Replies


PsychoCoder
Expert Mod 100+
P: 465
Ok since you have 2 validation controls on txtName when it doesn't work what error message is displayed (because both validators have different error messages)
Aug 28 '12 #2

PsychoCoder
Expert Mod 100+
P: 465
This is a far better regular expression pattern to validate an email address (I would know because I wrote it ;) )

Expand|Select|Wrap|Line Numbers
  1. @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"
Aug 28 '12 #3

P: 21
It doesn't work, actually i have 16 requiredfieldvalidator and 6 regular expression.
Aug 28 '12 #4

PsychoCoder
Expert Mod 100+
P: 465
Do you know what part is failing?
Aug 28 '12 #5

P: 21
.*@.*\..* - I use this for email validation, it works
For confirmation of password, i use the compare validator it works. But the rest of the required field validator doesn't work, I am able to send data to my mysql database even though other fields are empty. Then after saving the data, suddenly the validators will work.. though the regular expression doesn't work
Aug 28 '12 #6

P: 21
^[a-zA-Z]+$+_ i use this so that users can only input letters
Aug 28 '12 #7

Post your reply

Sign in to post your reply or Sign up for a free account.