none
The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar.

    Question

  • Why am I getting this error, I have a Custom Validator that Checks to see if the StartDate is before the EndDate.  i.e. (StartTime: 05/19/2010 15:30--EndTime: 05/20/2010 15:30).  As you can see I'm using Military Time.  Now if I put in hours above 24 and minutes above 59 i get this error at "StartDate = Convert.ToDateTime(txtStartDate.Text)" or "EndDate = Convert.ToDateTime(txtEndDate.Text)" depending on which textbox the invalid hours or minutes were.

     

      Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
        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
    
        StartDate = Convert.ToDateTime(txtStartDate.Text)
        EndDate = Convert.ToDateTime(txtEndDate.Text)
    
        Difference = EndDate.Subtract(StartDate)
    
        If Not (Difference.TotalMinutes >= 1) Then
          args.IsValid = False
        Else
          args.IsValid = True
        End If
      End Sub
    Any Assistance would be greatly welcomed.
    Wednesday, May 19, 2010 5:56 PM

Answers

  • I assume your culture is USA (otherwise it is always wrong in this case)

    If I test it with simple code, then there goes nothing wrong.

    Module Module1
      Sub Main()
        Threading.Thread.CurrentThread.CurrentCulture = New Globalization.CultureInfo("en-Us")
        Dim StartDate = CDate("05/19/2010 15:30")
        Dim EndDate = CDate("05/20/2010 15:30")
        Dim Difference = EndDate.Subtract(StartDate)
        Console.Write(Difference)
        Console.ReadLine()
      End Sub
    End Module
    
    Result 1 day

    Success
    Cor
    Wednesday, May 19, 2010 6:16 PM

All replies

  • I assume your culture is USA (otherwise it is always wrong in this case)

    If I test it with simple code, then there goes nothing wrong.

    Module Module1
      Sub Main()
        Threading.Thread.CurrentThread.CurrentCulture = New Globalization.CultureInfo("en-Us")
        Dim StartDate = CDate("05/19/2010 15:30")
        Dim EndDate = CDate("05/20/2010 15:30")
        Dim Difference = EndDate.Subtract(StartDate)
        Console.Write(Difference)
        Console.ReadLine()
      End Sub
    End Module
    
    Result 1 day

    Success
    Cor
    Wednesday, May 19, 2010 6:16 PM
  • You are correct that my culture is USA.  The textboxes are located on a Detailsview in a asp.net page. 

    I have tried to apply your code with my code listed above and I am still getting the same error. 

      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 StartDate = CDate(txtStartDate.Text)
        Dim EndDate = CDate(txtEndDate.Text)
    
        Difference = EndDate.Subtract(StartDate)
    
        If Not (Difference.Minutes >= 1) Then
          'If DateDiff(DateInterval.Minute, CDate(txtStartDate.Text), CDate(txtEndDate.Text)) >= 1 Then
          args.IsValid = False
        Else
          args.IsValid = True
        End If
      End Sub
    Wednesday, May 19, 2010 6:31 PM