UniqueValueValidatorBase Class
Life With Nate
Nate's Poetry Page
Resume (PDF)
Nate's Code
ASP.NET 2.0/3.5
ButtonExtensions
ConditionalRequiredTextValidator
CSSImageMap
CustomStyle
DateTimePicker
DateTimePickerTemplated
LengthValidator
RestrictInputTextBox
RestrictInputValidator
Rollovers
RotatedTextHandler
RotatedTextImage
Rotators
SelectedCountValidator
SlideIntoView
StatesDropDownList
UniqueValueValidatorBase
OleDbUniqueValueValidator
OracleUniqueValueValidator
SqlClientUniqueValueValidator
VB.NET 2.0/3.5
Just For Fun
Windows Phone 7
Description
The UniqueValueValidatorBase class is the base class for
OleDbUniqueValueValidator
,
OracleUniqueValueValidator
, and
SqlClientUniqueValueValidator
classes. UniqueValueValidatorBase inherits the BaseValidator class and must be inherited, as demonstrated in the
OleDbUniqueValueValidator
,
OracleUniqueValueValidator
, and
SqlClientUniqueValueValidator
classes.
Properties & Methods
UniqueValueValidatorBase
The UniqueValueValidatorBase class is the base class for
OleDbUniqueValueValidator
,
SqlClientUniqueValueValidator
, and
OracleUniqueValueValidator
classes.
ConnectionString
- The string used to connect to the database containing the table to check uniqueness against. This property is ignored if ConnectionStringKey is specified.
ConnectionStringKey
- The key of the value in System.Configuration.ConfigurationManager.AppSettings used to connect to the database containing the table to check uniqueness against. This value is used when a value is specified for both ConnectionString and ConnectionStringKey.
ConnectionStringsName
- The name of the connection string in System.Configuration.ConfigurationManager.ConnectionStrings used to connect to the database containing the table to check uniqueness against. This value is used even if a value is specified for ConnectionString and/or ConnectionStringKey.
FieldName
- The name of the field in the table specified in TableName to compare uniqueness against.
TableName
- The name of the table in the database that will be used for determining uniqueness.
FieldType
- The datatype that determines what the generated sql query will look like.
Protected Function ValidationSql(ByVal value As String) As String
This function returns a string containing an sql query to be used for validating the uniqueness of the passed value.
value
- The value used in the sql query to validate uniqueness.
Public MustOverride Function FormatDate(ByVal value As DateTime) As String
This function returns a date formatted for the selected database.
value
- The DateTime value to be formatted.
Public MustOverride Function FormatTime(ByVal value As DateTime) As String
This function returns a time formatted for the selected database.
value
- The DateTime value to be formatted.
Public MustOverride Function FormatDateTime(ByVal value As DateTime) As String
This function returns a date and time formatted for the selected database.
value
- The DateTime value to be formatted.
Public MustOverride Function DoValidation(ByVal value As String) As Boolean
This function returns a boolean value indicating whether the value is unique.
value
- The value which needs to be determined whether is unique.
Public Enum DBTypes As Byte
An enumeration used to determine what type of data the field in the database table contains.
Character
- Used for text formats such as varchar, char, text, etc.
DateTime
- Used for dates and/or times
NoQuotes
- Used for formats that do not require additional formatting, such as numeric values
Source Code
UniqueValueValidatorBase.vb:
Namespace NathanSokalski Public MustInherit Class UniqueValueValidatorBase : Inherits BaseValidator : Implements ICallbackEventHandler Private connstring As String = "" Private connstringkey As String = "" Private connstringsname As String = "" Private dbfield As String = "" Private dbtable As String = "" Private dbtype As DBTypes = DBTypes.Character Private value As String Public Property ConnectionString() As String Get Return Me.connstring End Get Set(ByVal value As String) If Me.connstringkey = "" AndAlso Me.connstringsname = "" Then Me.connstring = value End Set End Property Public Property ConnectionStringKey() As String Get Return Me.connstringkey End Get Set(ByVal value As String) If Me.connstringsname = "" Then Me.connstring = System.Configuration.ConfigurationManager.AppSettings(value) Me.connstringkey = value End Set End Property Public Property ConnectionStringsName() As String Get Return Me.connstringsname End Get Set(ByVal value As String) Me.connstring = System.Configuration.ConfigurationManager.ConnectionStrings(value).ConnectionString Me.connstringsname = value End Set End Property Public Property FieldName() As String Get Return Me.dbfield End Get Set(ByVal value As String) Me.dbfield = value End Set End Property Public Property TableName() As String Get Return Me.dbtable End Get Set(ByVal value As String) Me.dbtable = value End Set End Property <System.ComponentModel.DefaultValue("Character")> Public Property FieldType() As DBTypes Get Return Me.dbtype End Get Set(ByVal value As DBTypes) Me.dbtype = value End Set End Property Protected Overrides Function EvaluateIsValid() As Boolean Return Me.EvaluateIsValid(Me.GetControlValidationValue(Me.ControlToValidate)) End Function Private Overloads Function EvaluateIsValid(ByVal value As String) As Boolean Select Case Me.dbtype Case DBTypes.Character Return Me.DoValidation("'" & value.Replace("'", "''") & "'") Case DBTypes.DateTime If IsDate(value) Then If value.Contains(":") Then If value.Contains("/") OrElse value.Contains("-") Then Return Me.DoValidation(Me.FormatDateTime(CDate(value))) Else Return Me.DoValidation(Me.FormatTime(CDate(value))) End If Else Return Me.DoValidation(Me.FormatDate(CDate(value))) End If End If Case DBTypes.NoQuotes Return Me.DoValidation(value) End Select End Function Protected Function ValidationSql(ByVal value As String) As String Return String.Format("SELECT {0} FROM {1} WHERE {0}={2}", Me.dbfield, Me.dbtable, value) End Function Public Function GetCallbackResult() As String Implements System.Web.UI.ICallbackEventHandler.GetCallbackResult Dim id As String = Me.value.Split(" "c)(0) Dim value As String = Me.value.Split(" "c)(1) Dim returnvalue As String = String.Format("{0} {1}", id, Me.EvaluateIsValid(value).ToString().ToLower()) Return returnvalue End Function Public Sub RaiseCallbackEvent(ByVal eventArgument As String) Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent Me.value = eventArgument End Sub Private Sub UniqueValueValidatorBase_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender Dim uvv As New System.Text.StringBuilder() uvv.Append("function UniqueValueValidatorIsValid(val){") uvv.Append(Me.Page.ClientScript.GetCallbackEventReference(Me, "val.id+' '+document.getElementById(val.controltovalidate).value", "UniqueValueValidators_UpdatePageValidators", Nothing)) uvv.AppendLine(";return true;}") uvv.Append("function UniqueValueValidators_UpdatePageValidators(id_isvalid){") uvv.Append("var id=id_isvalid.split(' ')[0];") uvv.Append("document.getElementById(id).isvalid=id_isvalid.split(' ')[1]=='true';") uvv.Append("ValidatorUpdateDisplay(document.getElementById(id));") uvv.Append("ValidatorUpdateIsValid();") uvv.AppendLine("}") If Not Me.Page.ClientScript.IsClientScriptBlockRegistered("UniqueValueValidator") Then Me.Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "UniqueValueValidator", uvv.ToString(), True) End Sub Protected Overrides Sub AddAttributesToRender(ByVal writer As System.Web.UI.HtmlTextWriter) MyBase.AddAttributesToRender(writer) writer.AddAttribute("evaluationfunction", "UniqueValueValidatorIsValid") End Sub Public MustOverride Function FormatDate(ByVal value As DateTime) As String Public MustOverride Function FormatTime(ByVal value As DateTime) As String Public MustOverride Function FormatDateTime(ByVal value As DateTime) As String Public MustOverride Function DoValidation(ByVal value As String) As Boolean End Class Public Enum DBTypes As Byte Character 'Include single quotes, used for text types such as VARCHAR, CHAR, etc. DateTime 'Use the appropriate format for DATETIME for the selected provider NoQuotes 'Do not modify, used for numeric and other types that do not require formatting End Enum End Namespace
Remarks
Because the validation involves connecting with a database, the validation is only done server-side. I am currently working on finding a way to use AJAX and client callbacks to modify the control to allow more immediate validation.