Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having a WPF xaml page, where I am trying to modify the date value.

<DatePicker  HorizontalAlignment="Left" Height="25" Margin="300,460,0,0" VerticalAlignment="Top" Width="250" Text="{Binding SelectedProduct.DateApplication, Mode=TwoWay}"  />


I have used a DatePicker. When I am trying to Save value in database which is more than 12/02/2019 is not inserting or saving. But dates which are below 12/02/2019 is saved and updating into database.

Can anyone help me on this?

What I have tried:

Xaml:

<DatePicker  HorizontalAlignment="Left" Height="25" Margin="300,460,0,0" VerticalAlignment="Top" Width="250" Text="{Binding SelectedProduct.DateApplication, Mode=TwoWay}"  />





Modal:

public DateTime? DateApplication
     {
         get;
         set;
     }



Validate Method:

private void Validate(object obj)
       {
           try
           {
               ProduitClient Product = obj as ProduitClient;

               if (IsEditMode)
               {
                   //update the product

                   editWindow.Close();
               }
Posted
Updated 26-Feb-19 5:32am

One of the complications of dates is related to how they are written in different regions of the world. Most likely something is picking up the date as MM/dd/yyyy while you expect it to be dd/MM/yyyy - or the other way around.

Debug the model as the value is being set - is it set as you expect (do not just look at the string, check the actual month and day values to be sure).

Check how you communicate with the database. Always use a date related type where possible. Avoid strings. So update/insert using parameterized queries (which you should anyway) and use the .net type DateTime or DateTimeOffset. Consider - and test - how timezones and switch to and from daylight saving time will affect your data.

If you have to use a string for anything besides displaying to the user, always use yyyy-MM-dd (ISO 8601)
 
Share this answer
 
Comments
TheRealSteveJudge 26-Feb-19 2:56am    
Good explanation! 5*
Member 9956700 26-Feb-19 3:34am    
Thanks for replying, But I have taken Datetime, When i am debbuging the code, I can see -if i am selecting the dates between 1 to 12 in datepicker its working es expected but when I am trying to select dates as 13th feb, its not allowing.Means In run time, my Datepicker is showing red box. Like a validation. But i have not used any validation.
lmoelleb 26-Feb-19 4:33am    
Please update your question with this information, and as much detail as possible (what exactly do you see then debugging your model if setting for example 01/02/2020), what regional settings do you use. And from what you write here it has nothing to do with the database? So remove the part about inserting/saving to the database it just makes the question less clear.
Member 9956700 26-Feb-19 4:44am    
My model is updating only when dates are between 1 to 12 for a particular month. Otherwise its not reflecting. You can remove the insert/update because if values of model comes correctly- it will update /insert.
Member 9956700 26-Feb-19 4:45am    
Actually i am seeing- issue only with date picker. When i am trying to select dates from 13 to 31.. its not happening.
Quote:
XML
<DatePicker ... Text="{Binding SelectedProduct.DateApplication, Mode=TwoWay}"  />

You are binding the date picker's Text property[^]. You need to bind the SelectedDate property[^] instead.
XML
<DatePicker ... SelectedDate="{Binding SelectedProduct.DateApplication, Mode=TwoWay}"  />

When you bind to the Text property, your DateTime value is converted to a string using the current culture settings. The WPF binding then uses the US English culture to try to convert that string back to a DateTime.

This "feature" of WPF binding - which many would (correctly) argue is actually a bug - has been around for over a decade:
WPF Bindings and CurrentCulture Formatting - Rick Strahl's Web Log[^]
Binding with Respect to CurrentCulture[^]
 
Share this answer
 
Comments
Maciej Los 26-Feb-19 11:47am    
Hawk eye!
Member 9956700 1-Mar-19 2:23am    
Yes.. Hawk eye...!!!1 Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900