Hello Guys and Gals,
I need a little help, obviously. I been tring to solve this problem for more than three weeks and I'm having no success. I'm under a little presure on my job at this point. So I have to turn to the experts in this forum.
I'm building an appointment application. I'm struggling with using a "CustomValidator" event proceedure to validate the value from a textbox (dateTextBox) that's entered by the end-user, via "Formview", against the values in the database.
Example:
The end-user selects a date from the calendar and the value is passed to the "dateTextBox" I would like the "CustomValidator" event to check and see if the date exist in the database. If date exist return true/false. The problem I'm having is with the event proceedure args.IsValid. The return on any date selected comes back with date exist. If you select a date for next year it will return that the date exist, when in actuality it doesn't. I have attempted various potential solutions such as: inline coding to do "Sql Select blah blah... If Exist...blah blah", Sql Count(blah blah) AS (blah blah), Create Sql StoreProc with If Exist and/or Cast blah blah. All didn't work or I just did it wrong.
Please See Codes Below. Note: SqlDataSource Codes and Formview Templated Codes are left out for simplicity.
ASPX.CODE
<asp:FormView ID="FormView1" runat="server" DataKeyNames="ID" DataSourceID="SqlDataSource1" DefaultMode="Insert" Width="715px" onpageindexchanging="FormView1_PageIndexChanging">
<asp:TextBox ID="dateTextBox" runat="server" Text='<%# Bind("ApptDate") %>'
Width="141px"></asp:TextBox>
<br />
<asp:CustomValidator ID="DateCustomValidator" runat="server"
ErrorMessage="This date is not available" ControlToValidate="dateTextBox"
OnServerValidate="DateCustomValidator_ServerValidate" Display="Dynamic"></asp:CustomValidator>
<div id="datePicker">
<asp:Calendar ID="calEventDate" runat="server"
OnSelectionChanged="calEventDate_SelectionChanged"
ShowTitle="true" DayNameFormat="FirstTwoLetters"
SelectionMode="Day" BackColor="#ffffff"
FirstDayOfWeek="Sunday" BorderColor="#000000"
ForeColor="#00000" Height="80" Width="220">
<TitleStyle backcolor="#000080" forecolor="#ffffff" />
<NextPrevStyle backcolor="#000080" forecolor="#ffffff" />
<OtherMonthDayStyle forecolor="#c0c0c0" />
</asp:Calendar>
</div>
<asp:Button ID="submitBtn" runat="server" Text="Submit"
ToolTip="Submit ID Form" CommandName="Insert" CausesValidation="True" Height="26px" />
</asp:FormView>
VB SCRIPTING CODE
Sub calEventDate_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim dateTextBox As TextBox
Dim calEventDate As Calendar
dateTextBox = CType(FormView1.FindControl("dateTextBox"), TextBox)
calEventDate = FormView1.FindControl("calEventDate")
dateTextBox.Text = calEventDate.SelectedDate.ToString("d")
End Sub
Protected Sub DateCustomValidator_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
Dim DateCustomValidator As CustomValidator
DateCustomValidator = CType(FormView1.FindControl("DateCustomValidator"), CustomValidator)
Dim SqlDataSource3 As SqlDataSource
SqlDataSource3 = CType(FormView1.FindControl("SqlDataSource3"), SqlDataSource)
Dim dateTextBox As TextBox
dateTextBox = CType(FormView1.FindControl("dateTextBox"), TextBox)
Dim sql As String
Dim conn As New SqlConnection
conn.ConnectionString = "Data Source=HQSSWEB1;Initial Catalog=HSC_Mil_ID_Appointment;Integrated Security=true"
sql = "select ApptDate from Appointments"
conn.Open()
Dim ds As New DataSet
Dim da As SqlDataAdapter
da = New SqlDataAdapter(sql, conn)
da.Fill(ds, "ApptDate")
Dim dt As DataTable
dt = ds.Tables("ApptDate")
conn.Close()
args.IsValid = False
Dim dr As DataRow
For Each dr In ds.Tables("ApptDate").Rows
If dr.ToString = args.Value Then
args.IsValid = True
Exit For
End If
Next
End Sub
------------------------------------------------------------------------------------------------
Thank you for your help. I greatly appreciate it. As my .net learning knowledge reaches a higher level I will pay it forward.
NetBlazer