none
ConvertBack string to DateTime

    Question

  • I have a database that records students and dates. I can record the data ok.

    I also have a Reports.xaml that shows the data depending on a query.

    I have written a converter class to convert the dates to a short date on the Reports.xaml.

    Reports.xaml is also editable but this is where I have a problem.

    Do I need to use convertback to change the textbox string into a DateTime for the database to update.

    So far I have this converter. Any additional information please ask.

    [

    ValueConversion(typeof(DateTime), typeof(String))]

    class DateConverter : IValueConverter
        {
            #region IValueConverter Members
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                return string.Format("{0:dd/MMM/yyyy}", value);
            }
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                 throw new NotImplementedException();
            }
            #endregion
        } 

    Friday, March 19, 2010 7:15 PM

Answers

  • <GridViewColumn Header="Pack Issued" Width="100" >
                        <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBox Text="{Binding Path=PackIssued,Converter={StaticResource myConverter}}" 
                                             Margin="-6,0,-6,0" Width="100"/>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>

    PackIssued is a field in SQLServer2008 (datetime).

    private void btnSave_Click(object sender, RoutedEventArgs e)
            {
                try
                {
                    this.db.SubmitChanges();
                    
                    MessageBox.Show("Saved");
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show(ex.ToString());
                    System.Windows.MessageBox.Show(" " + (this.taManager.UpdateAll(this.AbsenceData)));
                }
            }
    
    When I edit the field I do get the "saved" messagebox but on running a query to repopulate it appears that the value is being commited.
    • Marked as answer by surfluds Wednesday, September 15, 2010 11:15 AM
    Monday, March 22, 2010 10:00 AM

All replies

  • You could use DateTime.TryParseExact. It allows you to specify one or more allowed formats and to trap invalid dates.

    HTH
    --mc

    Friday, March 19, 2010 7:53 PM
  • Yes, if you have the data moving from the UI to the bound property then you will have to provide implementation for the ConvertBack.  See IValueConverter.

    Ken

    Friday, March 19, 2010 9:36 PM
  • [ValueConversion(typeof(DateTime), typeof(String))]
        class DateConverter : IValueConverter
        {
           #region IValueConverter Members
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                DateTime date = (DateTime)value;
                return date.ToLongDateString();
            }

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                string strValue = value as string;
                DateTime resultDateTime;
                if (DateTime.TryParse(strValue, out resultDateTime))
                {
                    return resultDateTime;
                }
                return DependencyProperty.UnsetValue;
            }
            #endregion
        }

    THis is ok in that it converts the DateTime taken from SQLSqerver and displays it as a LongDateString but I cannot get the ConvertBack to return it to the yyyy/mm/dd that SQL uses.

    Am I missing something

    regards Mike

     

    Saturday, March 20, 2010 12:37 AM
  • Mike,
    your code looks fine: it's converting strings back to DateTime objects, which don't have any intrinsic format.

    Even to SQL Server dates are just binary objects. The only case in which you would actually need that specific format is when you build a T-SQL query by concatenating some strings, and that is not a good idea anyway. Using a parameter will solve that issue (and others).

    HTH
    --mc

    Saturday, March 20, 2010 1:09 AM
  • If your having problems only when the value in the TextBox is not a valid date then I think that you need to change the Dependency.UnsetValue to something else.  When you bind to TextBox.Text and your converter.ConvertBack returns Dependency.UnsetValue, the Binding Engine will first try to use the FallbackValue and if that doesn't exist, it will use the DefaultValue of the Dependency Property that the binding is linked to.  In this case, the TextBox.Text dependency DefaultValue is String.Empty.

    Ken

    Monday, March 22, 2010 5:15 AM
  • Hi  Surfluds,

    The workaround KenWolter mentioned make sense in this case. For more detailed information, how did you define your binding in XAML markup? please share it with us so that we can reproduce your scenario and focus on the issue.

    Thanks.

    Sincerely.

     


    Jim Zhou -MSFT
    Monday, March 22, 2010 9:27 AM
  • <GridViewColumn Header="Pack Issued" Width="100" >
                        <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBox Text="{Binding Path=PackIssued,Converter={StaticResource myConverter}}" 
                                             Margin="-6,0,-6,0" Width="100"/>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>

    PackIssued is a field in SQLServer2008 (datetime).

    private void btnSave_Click(object sender, RoutedEventArgs e)
            {
                try
                {
                    this.db.SubmitChanges();
                    
                    MessageBox.Show("Saved");
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show(ex.ToString());
                    System.Windows.MessageBox.Show(" " + (this.taManager.UpdateAll(this.AbsenceData)));
                }
            }
    
    When I edit the field I do get the "saved" messagebox but on running a query to repopulate it appears that the value is being commited.
    • Marked as answer by surfluds Wednesday, September 15, 2010 11:15 AM
    Monday, March 22, 2010 10:00 AM