That exception "System.InvalidCastException" is raised by .NET, not by SQL Server.
DatePicker.SelectedDate Property returns an object of type System.DateTimeOffset, which can not be implicit converted to date; you have to convert it first in .NET.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Failed to convert parameter value from a DateTimeOffset to a DateTime . I have a datapicker where I choose the date with sql server to enter the date in the database that then in another window of the uwp app comes date.
That exception "System.InvalidCastException" is raised by .NET, not by SQL Server.
DatePicker.SelectedDate Property returns an object of type System.DateTimeOffset, which can not be implicit converted to date; you have to convert it first in .NET.
Adding to @Olaf Helper answer, the SQL Server datetime type has no notion of an offset so you'll need to convert it to a datetime value of a specific timezone. If the client and server are in the same timezone, you could use DateNac.SelectedDate.Value.ToLocalTime()
. For different timezones, standardize on storing values for a specific timezone to avoid ambiguity.. UTC is must commonly used (e.g. DateNac.SelectedDate.Value.ToUniversalTime()
).
Alternatively change the column type to datetimeoffset. This allows you to preserve the original value and compare naturally compare datetime values with different offsets in T-SQL.
Hi,
Welcome to Microsoft Q&A!
The reason for this issue is that the parameter type does not match the corresponding value type. Please check the following steps to do this.
Change the code to:
cmd.Parameters.Add("@duoDate",SqlDbType.DateTimeOffset).Value=DateNac.SelectedDate.Value;
In addition, you need to set the corresponding column type of sql server database to datetimeoffset
.
After my test, it is recommend to use SqlDbType.Date, which makes the time clearer, only the year, month and day, no minutes, seconds.
For example:
cmd.Parameters.Add("@duoDate", SqlDbType.Date).Value = DateNac.SelectedDate.Value.Date;
Note: set the corresponding column type of sql server database to date
.
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
for the database for the datepicker that best option Date or datetime so that the date is saved in the database.