Custom Validation Check Duplicate Data

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 5
Reputation: Stretcher75 is an unknown quantity at this point 
Solved Threads: 0
Stretcher75 Stretcher75 is offline Offline
Newbie Poster

Custom Validation Check Duplicate Data

 
0
  #1
25 Days Ago
I'm still trying to get my validation to work correctly. So far if a user enters in a Salesman Code that exists in the database it displays the error message when tabbing off the field. The Salesman Code field is also required so I can't get this validator to work either with the customer validator that checks duplicates. What I am looking for is when a user does not enter a Salesman Code for the error to appear and not let the user submit the form. If the user enters a Salesman Code that exists in the database the error message will be displayed and the user should not be allowed to submit the form. Any help or examples would be appreciated. Below is my code that I have using VB.

Below is my aspx page:

  1. <%@ Page Language="VB" %>
  2. <%@ Register TagPrefix="custom" Namespace="myControls" %>
  3. <%@ Import Namespace="System.Data.SqlClient" %>
  4. <%@ Import Namespace="System.Web.Configuration" %>
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  6. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  7. <script runat="server">
  8.  
  9.  
  10. ''' Validation function that is called on both the client and server
  11.  
  12. Protected Sub AjaxValidator1_ServerValidate(ByVal source As Object, ByVal args As ServerValidateEventArgs)
  13. If Salesman_CodeExists(args.Value) Then
  14. args.IsValid = False
  15. Else
  16. args.IsValid = True
  17. Me.btnSubmit.Attributes.Add("onclick", "alert('Please try a different Salesman Code!');return false;")
  18. End If
  19. End Sub
  20.  
  21. ''' <summary>
  22. ''' Returns true when user name already exists
  23. ''' in Users database table
  24. ''' </summary>
  25. Private Function Salesman_CodeExists(ByVal Salesman_Code As String) As Boolean
  26. Dim conString As String = WebConfigurationManager.ConnectionStrings("PennerConnectionString").ConnectionString
  27. Dim con As New SqlConnection(conString)
  28. Dim cmd As New SqlCommand("SELECT COUNT(*) FROM tblSalesman WHERE Salesman_Code=@Salesman_Code", con)
  29. cmd.Parameters.AddWithValue("@Salesman_Code", Salesman_Code)
  30. Dim result As Boolean = False
  31. Using con
  32. con.Open()
  33. Dim count As Integer = CType(cmd.ExecuteScalar(), Integer)
  34. If count > 0 Then
  35. result = True
  36. Else
  37. End If
  38. End Using
  39. Return result
  40. End Function
  41.  
  42.  
  43. ''' Insert new user name to Users database table
  44.  
  45. Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
  46. Dim conString As String = WebConfigurationManager.ConnectionStrings("PennerConnectionString").ConnectionString
  47. Dim con As New SqlConnection(conString)
  48. Dim cmd As New SqlCommand("INSERT tblSalesman (Salesman_Code,Salesman_LastName) VALUES (@Salesman_Code,@Salesman_LastName)", con)
  49. cmd.Parameters.AddWithValue("@Salesman_Code", txtSalesman_Code.Text)
  50. cmd.Parameters.AddWithValue("@Salesman_LastName", txtSalesman_LastName.Text)
  51. Using con
  52. con.Open()
  53. cmd.ExecuteNonQuery()
  54. End Using
  55. txtSalesman_Code.Text = String.Empty
  56. txtSalesman_LastName.Text = String.Empty
  57. End Sub
  58. </script>
  59. <html xmlns="http://www.w3.org/1999/xhtml" >
  60. <head id="Head1" runat="server">
  61. <title>Show AjaxValidator</title>
  62. </head>
  63. <body>
  64. <form id="form1" runat="server">
  65. <div>
  66.  
  67. <asp:Label
  68. id="lblSalesman_Code"
  69. Text="Salesman Code:"
  70. AssociatedControlID="txtSalesman_Code"
  71. Runat="server" />
  72. <asp:TextBox
  73. id="txtSalesman_Code"
  74. Runat="server" />
  75. <custom:AjaxValidator
  76. id="AjaxValidator1"
  77. ControlToValidate="txtSalesman_Code"
  78. Text="User name already taken!"
  79. OnServerValidate="AjaxValidator1_ServerValidate"
  80. Runat="server" />
  81.  
  82. <br /><br />
  83. <asp:Label
  84. id="lblSalesman_LastName"
  85. Text="Last Name:"
  86. AssociatedControlID="txtSalesman_LastName"
  87. Runat="server" />
  88. <asp:TextBox
  89. id="txtSalesman_LastName"
  90. Runat="server" />
  91.  
  92. <br /><br />
  93. <asp:Button
  94. id="btnSubmit"
  95. Text="Submit"
  96. Runat="server" OnClick="btnSubmit_Click" />
  97.  
  98. </div>
  99. </form>
  100. </body>
  101. </html>
  102.  
  103. <%@ Page Language="VB" %>
  104. <%@ Register TagPrefix="custom" Namespace="myControls" %>
  105. <%@ Import Namespace="System.Data.SqlClient" %>
  106. <%@ Import Namespace="System.Web.Configuration" %>
  107. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  108. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  109. <script runat="server">
  110.  
  111.  
  112. ''' Validation function that is called on both the client and server
  113.  
  114. Protected Sub AjaxValidator1_ServerValidate(ByVal source As Object, ByVal args As ServerValidateEventArgs)
  115. If Salesman_CodeExists(args.Value) Then
  116. args.IsValid = False
  117. Else
  118. args.IsValid = True
  119. Me.btnSubmit.Attributes.Add("onclick", "alert('Please try a different Salesman Code!');return false;")
  120. End If
  121. End Sub
  122.  
  123. ''' <summary>
  124. ''' Returns true when user name already exists
  125. ''' in Users database table
  126. ''' </summary>
  127. Private Function Salesman_CodeExists(ByVal Salesman_Code As String) As Boolean
  128. Dim conString As String = WebConfigurationManager.ConnectionStrings("PennerConnectionString").ConnectionString
  129. Dim con As New SqlConnection(conString)
  130. Dim cmd As New SqlCommand("SELECT COUNT(*) FROM tblSalesman WHERE Salesman_Code=@Salesman_Code", con)
  131. cmd.Parameters.AddWithValue("@Salesman_Code", Salesman_Code)
  132. Dim result As Boolean = False
  133. Using con
  134. con.Open()
  135. Dim count As Integer = CType(cmd.ExecuteScalar(), Integer)
  136. If count > 0 Then
  137. result = True
  138. Else
  139. End If
  140. End Using
  141. Return result
  142. End Function
  143.  
  144.  
  145. ''' Insert new user name to Users database table
  146.  
  147. Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
  148. Dim conString As String = WebConfigurationManager.ConnectionStrings("PennerConnectionString").ConnectionString
  149. Dim con As New SqlConnection(conString)
  150. Dim cmd As New SqlCommand("INSERT tblSalesman (Salesman_Code,Salesman_LastName) VALUES (@Salesman_Code,@Salesman_LastName)", con)
  151. cmd.Parameters.AddWithValue("@Salesman_Code", txtSalesman_Code.Text)
  152. cmd.Parameters.AddWithValue("@Salesman_LastName", txtSalesman_LastName.Text)
  153. Using con
  154. con.Open()
  155. cmd.ExecuteNonQuery()
  156. End Using
  157. txtSalesman_Code.Text = String.Empty
  158. txtSalesman_LastName.Text = String.Empty
  159. End Sub
  160. </script>
  161. <html xmlns="http://www.w3.org/1999/xhtml" >
  162. <head id="Head1" runat="server">
  163. <title>Show AjaxValidator</title>
  164. </head>
  165. <body>
  166. <form id="form1" runat="server">
  167. <div>
  168.  
  169. <asp:Label
  170. id="lblSalesman_Code"
  171. Text="Salesman Code:"
  172. AssociatedControlID="txtSalesman_Code"
  173. Runat="server" />
  174. <asp:TextBox
  175. id="txtSalesman_Code"
  176. Runat="server" />
  177. <custom:AjaxValidator
  178. id="AjaxValidator1"
  179. ControlToValidate="txtSalesman_Code"
  180. Text="User name already taken!"
  181. OnServerValidate="AjaxValidator1_ServerValidate"
  182. Runat="server" />
  183.  
  184. <br /><br />
  185. <asp:Label
  186. id="lblSalesman_LastName"
  187. Text="Last Name:"
  188. AssociatedControlID="txtSalesman_LastName"
  189. Runat="server" />
  190. <asp:TextBox
  191. id="txtSalesman_LastName"
  192. Runat="server" />
  193.  
  194. <br /><br />
  195. <asp:Button
  196. id="btnSubmit"
  197. Text="Submit"
  198. Runat="server" OnClick="btnSubmit_Click" />
  199.  
  200. </div>
  201. </form>
  202. </body>
  203. </html>


Below is my AjaxValidator.vb file:

  1. Imports System
  2. Imports System.Web
  3. Imports System.Web.UI
  4. Imports System.Web.UI.WebControls
  5.  
  6. Namespace myControls
  7.  
  8. ''' <summary>
  9. ''' Enables you to perform custom validation on both the client and server
  10. ''' </summary>
  11. Public Class AjaxValidator
  12. Inherits BaseValidator
  13. Implements ICallbackEventHandler
  14.  
  15. Public Event ServerValidate As ServerValidateEventHandler
  16.  
  17. Dim _controlToValidateValue As String
  18.  
  19. Protected Overrides Sub OnPreRender(ByVal e As EventArgs)
  20. Dim eventRef As String = Page.ClientScript.GetCallbackEventReference(Me, "", "", "")
  21.  
  22. ' Register include file
  23. Dim includeScript As String = Page.ResolveClientUrl("~/js/AjaxValidator.js")
  24. Page.ClientScript.RegisterClientScriptInclude("AjaxValidator", includeScript)
  25.  
  26. ' Register startup script
  27. Dim startupScript As String = String.Format("document.getElementById('{0}').evaluationfunction = 'AjaxValidatorEvaluateIsValid'", Me.ClientID)
  28. Page.ClientScript.RegisterStartupScript(Me.GetType(), "AjaxValidator", startupScript, True)
  29.  
  30. MyBase.OnPreRender(e)
  31. End Sub
  32.  
  33. ''' <summary>
  34. ''' Only do the AJAX call on browsers that support it
  35. ''' </summary>
  36. Protected Overrides Function DetermineRenderUplevel() As Boolean
  37. Return Context.Request.Browser.SupportsCallback
  38. End Function
  39.  
  40. ''' <summary>
  41. ''' Server method called by client AJAX call
  42. ''' </summary>
  43. Public Function GetCallbackResult() As String Implements ICallbackEventHandler.GetCallbackResult
  44. Return ExecuteValidationFunction(_controlToValidateValue).ToString()
  45. End Function
  46.  
  47. ''' <summary>
  48. ''' Return callback result to client
  49. ''' </summary>
  50. Public Sub RaiseCallbackEvent(ByVal eventArgument As String) Implements ICallbackEventHandler.RaiseCallbackEvent
  51. _controlToValidateValue = eventArgument
  52. End Sub
  53.  
  54. ''' <summary>
  55. ''' Server-side method for validation
  56. ''' </summary>
  57. Protected Overrides Function EvaluateIsValid() As Boolean
  58. Dim controlToValidateValue As String = Me.GetControlValidationValue(Me.ControlToValidate)
  59. Return ExecuteValidationFunction(controlToValidateValue)
  60. End Function
  61.  
  62. ''' <summary>
  63. ''' Performs the validation for both server and client
  64. ''' </summary>
  65. Private Function ExecuteValidationFunction(ByVal controlToValidateValue As String) As Boolean
  66. Dim args As New ServerValidateEventArgs(controlToValidateValue, Me.IsValid)
  67. RaiseEvent ServerValidate(Me, args)
  68. Return args.IsValid
  69. End Function
  70.  
  71. End Class
  72.  
  73. End Namespace


Below is my AjaxValidator.js file:

  1. // Performs AJAX call back to server
  2. function AjaxValidatorEvaluateIsValid(val) {
  3. var value = ValidatorGetValue(val.controltovalidate);
  4. WebForm_DoCallback(val.id, value, AjaxValidatorResult, val,
  5. AjaxValidatorError, true);
  6. return true;
  7. }
  8.  
  9. // Called when result is returned from server
  10. function AjaxValidatorResult(returnValue, context) {
  11. if (returnValue == 'True')
  12. context.isvalid = true;
  13. else
  14. context.isvalid = false;
  15. ValidatorUpdateDisplay(context);
  16. }
  17.  
  18. // If there is an error, show it
  19. function AjaxValidatorError(message) {
  20. alert('Error: ' + message);
  21. }
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC