locked
Getting error as “specified cast is not valid” in Xamarin forms cross platform RRS feed

  • Question

  • User393719 posted

    Im trying to bind the data from the database to the listview in Xamarin forms based on the if else condition , using below code I'm getting an error for "Specified cast is not valid",also rather than returning the value as B i want tobind the result from the database please help me

    public class Class1: IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var date = (DateTime)value; DateTime dResult = DateTime.ParseExact("00:00:00.0000000", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture); if (date == dResult) return ""; return "B";

    }
    public object Convert1(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var date = (DateTime)value;
        DateTime dResult = DateTime.ParseExact("00:00:00.0000000", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
        if (date == dResult)
            return "";
        return "B";
    
    }
    
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
    

    }

    Thanks in advance

    Wednesday, July 22, 2020 2:14 AM

Answers

  • User371688 posted

    Specified cast is not valid

    For this question, you can recheck code var date = (DateTime)value;.

    What's the original type of your binded value? if it's type is string, then you can refer to the following code:

    ``` public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { string dateString = value.ToString();

            DateTime myDate;
            if (!DateTime.TryParse(dateString, out myDate))
            {
                // handle parse failure
            }
    
            DateTime dResult = DateTime.ParseExact("00:00:00.0000000", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
            int result = DateTime.Compare(myDate, dResult);
            if (result == 0)
                return "";
            return "B";
        }
    

    ```

    Note: 1.To compare two DateTime variables, you can use method DateTime.Compare

            DateTime dResult = DateTime.ParseExact("00:00:00.0000000", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
    
            int result = DateTime.Compare(myDate, dResult);
            if (result == 0)
                return "";
            return "B";
    

    2.You can also use method DateTime myDate0 = DateTime.Parse(dateString); to convert data string.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, July 22, 2020 6:29 AM

All replies

  • User371688 posted

    Specified cast is not valid

    For this question, you can recheck code var date = (DateTime)value;.

    What's the original type of your binded value? if it's type is string, then you can refer to the following code:

    ``` public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { string dateString = value.ToString();

            DateTime myDate;
            if (!DateTime.TryParse(dateString, out myDate))
            {
                // handle parse failure
            }
    
            DateTime dResult = DateTime.ParseExact("00:00:00.0000000", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
            int result = DateTime.Compare(myDate, dResult);
            if (result == 0)
                return "";
            return "B";
        }
    

    ```

    Note: 1.To compare two DateTime variables, you can use method DateTime.Compare

            DateTime dResult = DateTime.ParseExact("00:00:00.0000000", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
    
            int result = DateTime.Compare(myDate, dResult);
            if (result == 0)
                return "";
            return "B";
    

    2.You can also use method DateTime myDate0 = DateTime.Parse(dateString); to convert data string.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, July 22, 2020 6:29 AM
  • User371688 posted

    Hi @DemoCloud, have you resolved this question?

    Wednesday, July 29, 2020 6:25 AM
  • User393719 posted

    @jezh ,Yes I've resolved the issue.Thanks for help

    Wednesday, July 29, 2020 7:35 AM