Home/ASP.NET Forums/Advanced ASP.NET/Custom Server Controls/DateTime represented by the string is not supported in calendar Syste...

DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar [Answered]RSS

15 replies

Last post May 27, 2010 03:04 AM by Qin Dian Tang - MSFT

  • rinotomthomas

    rinotomthoma...

    Member

    238 Points

    48 Posts

    Re: DateTime represented by the string is not supported in calendar System.Globalization.Gregoria...

    May 20, 2010 12:39 PM|rinotomthomas|LINK

    Hi,

    Your validator will only check whether the end date to start date only if the date you enter is in correct format. If the date you entered is not in correct format, it will get to exception, before validating the values. It can be solved with an exception handling to handle the invalid date entered.


    Regards,
    Rino Tom Thomas

    Please mark as answer if the reply helped you
  • NihirPorecha

    NihirPorecha

    Contributor

    3400 Points

    616 Posts

    Re: DateTime represented by the string is not supported in calendar System.Globalization.Gregoria...

    May 20, 2010 12:44 PM|NihirPorecha|LINK

    Use comparevalidator to confirm that a valid date has been entered by the user.

    Please Mark as answer if it helps you.


    My blog
  • tmacdonaldmpower

    tmacdonaldmp...

    Member

    88 Points

    226 Posts

    Re: DateTime represented by the string is not supported in calendar System.Globalization.Gregoria...

    May 20, 2010 12:46 PM|tmacdonaldmpower|LINK

    Ok, Why is my invalid datetime check in the  MaskedEditValidator working on the StartDate if there is no EndDate but not on the EndDate and not working on the StartDate if there is a EndDate.  Both StartDate and EndDate have Validators to check if the DateTime is correct.

    This is my first ASP.Net project so i'm still learning.

    Any assistance would be greatly welcomed.

  • tmacdonaldmpower

    tmacdonaldmp...

    Member

    88 Points

    226 Posts

    Re: DateTime represented by the string is not supported in calendar System.Globalization.Gregoria...

    May 20, 2010 12:48 PM|tmacdonaldmpower|LINK

     Can I use a comparevalidator with a custom Validator?

     

     

  • NihirPorecha

    NihirPorecha

    Contributor

    3400 Points

    616 Posts

    Re: DateTime represented by the string is not supported in calendar System.Globalization.Gregoria...

    May 20, 2010 12:59 PM|NihirPorecha|LINK

    yes you can. For valid date check your validator code may look like below

    <asp:CompareValidator ID="" runat="server" ControlToValidate="" Display="Dynamic" Operator="DataTypeCheck" Type="Date" ValidationGroup=""></asp:CompareValidator>

    Please Mark as answer if it helps you.


    My blog
  • tmacdonaldmpower

    tmacdonaldmp...

    Member

    88 Points

    226 Posts

    Re: DateTime represented by the string is not supported in calendar System.Globalization.Gregoria...

    May 20, 2010 01:22 PM|tmacdonaldmpower|LINK

    I have tried your suggestion everything seemed to work fine during the error check, however the CompareValidator error message will not go away even after a correct time has been inserted. 

    How do I deal with this?

     

  • NihirPorecha

    NihirPorecha

    Contributor

    3400 Points

    616 Posts

    Re: DateTime represented by the string is not supported in calendar System.Globalization.Gregoria...

    May 20, 2010 01:25 PM|NihirPorecha|LINK

    The error message gets refreshed when the page gets submitted. So, if it is valid date and time then it will allow you to post the data back to the server. This is how .NET validators work. They dont work on lost focus.


    Please Mark as answer if it helps you.


    My blog
  • tmacdonaldmpower

    tmacdonaldmp...

    Member

    88 Points

    226 Posts

    Re: DateTime represented by the string is not supported in calendar System.Globalization.Gregoria...

    May 20, 2010 01:54 PM|tmacdonaldmpower|LINK

    I understand what you are saying and agree with you that that is the way it should be reacting however it is not.  It is not allowing me to post the date even with a valid date and time.  I have made sure that it has not lost focus on the error.  The error message is just not going away therefore it won't allow me to post.

     

    Any thoughts? 

  • Qin Dian Tang - MSFT

    Qin Dian Tan...

    All-Star

    113550 Points

    12480 Posts

    Microsoft

    Re: DateTime represented by the string is not supported in calendar System.Globalization.Gregoria...

    May 24, 2010 08:41 AM|Qin Dian Tang - MSFT|LINK

    Hi tmacdonaldmpower,

    You didn't check the valid of DateTime using Convert.ToDateTime. You need to check them manually like this:

    bool start = DateTime.TryParse(txtStartDate.Text, out StartDate);

    This will return a bool data to check it whether it is a valid DateTime value. Then output to the variable "StartDate". You can do anything if it pass or not after this.

    Thanks,

    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
    Microsoft One Code Framework
  • tmacdonaldmpower

    tmacdonaldmp...

    Member

    88 Points

    226 Posts

    Re: DateTime represented by the string is not supported in calendar System.Globalization.Gregoria...

    May 24, 2010 01:54 PM|tmacdonaldmpower|LINK

    I'm a little confused as to how I put that into the rest of the code block

     

    Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
            Threading.Thread.CurrentThread.CurrentCulture = New Globalization.CultureInfo("en-Us")
            Dim row As DetailsViewRow = DirectCast(TryCast(source, Control).Parent.Parent, DetailsViewRow)
            Dim txtStartDate As TextBox = DirectCast(row.FindControl("STEditCalendartxt"), TextBox)
            Dim txtEndDate As TextBox = DirectCast(row.FindControl("StopTSEdittxt"), TextBox)
    
    
            Dim StartDate As DateTime = Nothing
            Dim EndDate As DateTime = Nothing
            Dim Difference As New TimeSpan
            Dim Start As Boolean = DateTime.TryParse(txtStartDate.Text, StartDate)
            Dim Stop As Boolean = DateTime.TryParse(txtEndDate.Text, EndDate)
            'StartDate = Convert.ToDateTime(txtStartDate.Text)
            'EndDate = Convert.ToDateTime(txtEndDate.Text)
    
    
            Difference = EndDate.Subtract(StartDate)
    
    
            If Not (Difference.Minutes >= 1) Then
                args.IsValid = False
            Else
                args.IsValid = True
            End If
        End Sub


     

  • Qin Dian Tang - MSFT

    Qin Dian Tan...

    All-Star

    113550 Points

    12480 Posts

    Microsoft

    Re: DateTime represented by the string is not supported in calendar System.Globalization.Gregoria...

    May 25, 2010 03:23 AM|Qin Dian Tang - MSFT|LINK

    Hi tmacdonaldmpower,

    Try this:

    If DateTime.TryParse(txtStartDate.Text, StartDate) AndAlso DateTime.TryParse(txtEndDate.Text, EndDate) Then
        Dim Difference As TimeSpan = EndDate.Subtract(StartDate)
        If Not (Difference.Minutes >= 1) Then
            args.IsValid = False
        Else
            args.IsValid = True
        End If
    Else
        args.IsValid = False
    End If

    Thanks,

    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
    Microsoft One Code Framework
  • tmacdonaldmpower

    tmacdonaldmp...

    Member

    88 Points

    226 Posts

    Re: DateTime represented by the string is not supported in calendar System.Globalization.Gregoria...

    May 25, 2010 03:47 PM|tmacdonaldmpower|LINK

    That seems to have resolved the validation issue, although now it is posting both the CustomValidation error as well as the MaskedEditValidator, so I am getting both the "Date and/or Time is Invalid" and "End Date must be greater than Start Date" error.  

     

    Any Ideas.

    Thank you for your assistance so far

  • Qin Dian Tang - MSFT

    Qin Dian Tan...

    All-Star

    113550 Points

    12480 Posts

    Microsoft

    Re: DateTime represented by the string is not supported in calendar System.Globalization.Gregoria...

    May 26, 2010 02:43 AM|Qin Dian Tang - MSFT|LINK

    Hi tmacdonaldmpower,

    The workaround is that you can use CustomValidator control only, and remove MaskedEditValidator control. Then in the ServerValidate event you can check the data type, date value and compare the datetime manaully as what we did, and set the ErrorMessage according to different error we checked.

    Thanks,

     

    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
    Microsoft One Code Framework
  • tmacdonaldmpower

    tmacdonaldmp...

    Member

    88 Points

    226 Posts

    Re: DateTime represented by the string is not supported in calendar System.Globalization.Gregoria...

    May 26, 2010 12:21 PM|tmacdonaldmpower|LINK

    The MaskedEditValidator checks several other items though, such as minimium value in datetime,  sets the minimiumvalue error message, whether or not it IsValidEmpty, Error Font Color, and of course the ErrorMessage. 

    How do I handle that in the customvalidator?

    I know I'm asking quite a few question, still a newbie.

    Any additional assistance would be greatly welcomed.

  • Qin Dian Tang - MSFT

    Qin Dian Tan...

    All-Star

    113550 Points

    12480 Posts

    Microsoft

    Re: DateTime represented by the string is not supported in calendar System.Globalization.Gregoria...

    May 27, 2010 03:04 AM|Qin Dian Tang - MSFT|LINK

    Hi tmacdonaldmpower,

    When you check DateTime.TryParse(), it can avoid empty value and invalid data type. For mini value you can directly check it "EndDate > Convert.ToDateTime("01/01/2010 00:00")". For font color I think you can set ForeColor property of CustomValidator control. Remember CustomValidator control is a customized validator. If you give any related validation like required, regular checking, it can be acted as any kind of Validator control like MaskedEdit one.

    Just checking different kind of conditions for two textbox and output the related error message. Have a try.

    Thanks,

    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
    Microsoft One Code Framework
  • ‹ Previous Thread|Next Thread ›