DatePicker throws System.InvalidCastException When Entering Valid Date Manually and Hit Enter #231

Closed
opened 2026-01-29 17:33:51 +00:00 by claunia · 2 comments
Owner

Originally created by @nlpMbordogna on GitHub (Oct 20, 2021).

[2021-10-20T19:41:52.472Z] Error: System.InvalidCastException: Unable to cast object of type 'System.DateTime' to type 'System.Nullable1[System.DateTimeOffset]'. at Radzen.Blazor.RadzenDatePicker1.ParseDate()
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)
e.log @ blazor.server.js:21

html brackets removed so it the code can show in Git

` RadzenDatePicker TValue="DateTimeOffset?"
DateFormat="@this.FormatMask"
ValueChanged="@(async (value) => { await this.ValueChanged(value); })"
Value="@this.LowerBoundValue"
Name="from_date" /

@code{
public DateTimeOffset? LowerboundValue { get; set; }

public string FormatMask {get; set;} = "MM/dd/yyyy";

public Task ValueChanged(DatetimeOffset? val){
// do something
}

}
`

Originally created by @nlpMbordogna on GitHub (Oct 20, 2021). [2021-10-20T19:41:52.472Z] Error: System.InvalidCastException: Unable to cast object of type 'System.DateTime' to type 'System.Nullable`1[System.DateTimeOffset]'. at Radzen.Blazor.RadzenDatePicker`1.ParseDate() at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task) at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle) e.log @ blazor.server.js:21 html brackets removed so it the code can show in Git ` RadzenDatePicker TValue="DateTimeOffset?" DateFormat="@this.FormatMask" ValueChanged="@(async (value) => { await this.ValueChanged(value); })" Value="@this.LowerBoundValue" Name="from_date" / @code{ public DateTimeOffset? LowerboundValue { get; set; } public string FormatMask {get; set;} = "MM/dd/yyyy"; public Task ValueChanged(DatetimeOffset? val){ // do something } } `
Author
Owner

@enchev commented on GitHub (Oct 21, 2021):

I'm unable to reproduce such exception using our demos with the same configuration:
image
image

You can attach the source code to debug the exception at your end.

@enchev commented on GitHub (Oct 21, 2021): I'm unable to reproduce such exception using our demos with the same configuration: ![image](https://user-images.githubusercontent.com/5804953/138221515-8a649ba6-1f3d-4414-9819-ba5afb5b3951.png) ![image](https://user-images.githubusercontent.com/5804953/138221468-22fbfac9-7b76-42f0-bde1-ed8746a7e5ab.png) You can attach the source code to debug the exception at your end.
Author
Owner

@nlpMbordogna commented on GitHub (Oct 21, 2021):

Actually that isn't the same setup....

  1. Set TValue="DateTimeOffset?"
  2. you are using bind- not the separate Value and ValueChange event (which i know bind is syntactical sugar for the same thing but please keep your test apples to apples).
    I looked at the source in the parse method. if you configure it the same way then it will blow up

Another note is your event is sending back datetime not datetime offset, this is because your TValue isn't set to DatetimeOffset?. its a minor fix to do the check and the proper cast based on TValue.

@nlpMbordogna commented on GitHub (Oct 21, 2021): Actually that isn't the same setup.... 1. Set TValue="DateTimeOffset?" 2. you are using bind- not the separate Value and ValueChange event (which i know bind is syntactical sugar for the same thing but please keep your test apples to apples). I looked at the source in the parse method. if you configure it the same way then it will blow up Another note is your event is sending back datetime not datetime offset, this is because your TValue isn't set to DatetimeOffset?. its a minor fix to do the check and the proper cast based on TValue.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/radzen-blazor#231