Date update problem

I have a ListView doing an update to a table in SQL Server 2008R2.  When I try to remove the date I get the error below (using delete key to remove all numbers.  I am using a masked edit extender to help format the date.  The error does not happen when I
first enter the date and Update, just when I want to clear it.  Any ideas?  Also below is the web page entry and parameter.  The column on the table is a Date data type.  Thanks.

<asp:TextBox ID="txtServiceEnded" runat="server" Text=’<%# Bind("ServiceEnded","{0:d}") %>’ Width="80" Font-Size="12pt" />
<asp:MaskedEditExtender ID="MaskedEditExtender2" runat="server" Mask="99/99/9999"

        TargetControlID="txtServiceEnded" ClearMaskOnLostFocus="True">
</asp:MaskedEditExtender>

<asp:Parameter Name="ServiceEnded" DbType="Date" />

The error is below.

Message: String was not recognized as a valid DateTime.

 

 Stack Trace:

   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)

   at System.Convert.ToDateTime(String value, IFormatProvider provider)

   at System.String.System.IConvertible.ToDateTime(IFormatProvider provider)

   at System.Convert.ChangeType(Object value, TypeCode typeCode, IFormatProvider provider)

   at System.Web.UI.WebControls.Parameter.GetValue(Object value, String defaultValue, TypeCode type, Boolean convertEmptyStringToNull, Boolean ignoreNullableTypeChanges)

   at System.Web.UI.WebControls.Parameter.GetValue(Object value, String defaultValue, DbType dbType, Boolean convertEmptyStringToNull, Boolean ignoreNullableTypeChanges)

   at System.Web.UI.WebControls.Parameter.GetValue(Object value, Boolean ignoreNullableTypeChanges)

   at System.Web.UI.WebControls.SqlDataSourceView.AddParameters(DbCommand command, ParameterCollection reference, IDictionary parameters, IDictionary exclusionList, String oldValuesParameterFormatString)

   at System.Web.UI.WebControls.SqlDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues)

   at System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback)

   at System.Web.UI.WebControls.ListView.HandleUpdate(ListViewItem item, Int32 itemIndex, Boolean causesValidation)

   at System.Web.UI.WebControls.ListView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup)

   at System.Web.UI.WebControls.ListView.OnBubbleEvent(Object source, EventArgs e)

   at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)

   at System.Web.UI.WebControls.ListViewDataItem.OnBubbleEvent(Object source, EventArgs e)

   at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)

   at System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)

   at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)

   at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)

   at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)

   at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)

   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Best way to validate MM/dd/yyyy date 

<asp:TextBox ID="txtServiceEnded" runat="server" Width="80" Font-Size="12pt" />
<asp:CompareValidator ID="dateValidator" runat="server" Type="Date" Operator="DataTypeCheck"
    ControlToValidate="txtServiceEnded" ErrorMessage="Please enter a valid date.">
</asp:CompareValidator>

dlchase89

But there is no mask.  User wants a data entry mask like __/__/____ .. Just set ClearMaskOnLostFocus=”false”

You can keep your mask with validator

<asp:TextBox ID="txtServiceEnded" runat="server" Width="80" Font-Size="12pt" />
<asp:CompareValidator ID="dateValidator" runat="server" Type="Date" Operator="DataTypeCheck"
    ControlToValidate="txtServiceEnded" ErrorMessage="Please enter a valid date.">
</asp:CompareValidator>
<asp:MaskedEditExtender ID="MaskedEditExtender2" runat="server" Mask="99/99/9999" 
        TargetControlID="txtServiceEnded" ClearMaskOnLostFocus="True">
</asp:MaskedEditExtender>

Note: Compare validator validate for leap year also.

Regards.

But there is no mask.  User wants a data entry mask like __/__/____

Leave a Reply