Search code examples
c#wpfgetpersian-calendar

Error for insert PersianDate on datagrid :Cannot evaluate expression because the current thread is in a stack overflow state


In WPF project I have got a DataGrid name DG which is bind to Query in SQL in C# side I try to Convert string MiladiDate to PersianDate. But the error that is shown as title said:

Cannot evaluate expression because the current thread is in a stack overflow state

Here is the code where I change get/ set:

namespace DataModelLayer
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;

    public partial class Vw_DD_MM
    {
        public int MMID { get; set; }

        public string MiladiDate
        {
            get
            {
                DateTime englishDate = DateTime.Parse(MiladiDate);
                PersianCalendar pc = new PersianCalendar();
                return string.Format("{0}/{1}/{2}", pc.GetYear(englishDate), pc.GetMonth(englishDate).ToString("00"), pc.GetDayOfMonth(englishDate).ToString("00"));
            }
            set { }
        }
    }
}

Please see the screenshot of the error.


Solution

  • You have a recursion loop in gette body. You see, MiladiDate call itself in DateTime.Parse(MiladiDate). Try out something like this:

    public partial class Vw_DD_MM
    {
        public int MMID { get; set; }
    
        public string MiladiDate { get; set; }
    
        public string PersianDate
        {
            get
            {
                DateTime englishDate = DateTime.Parse(MiladiDate);
                PersianCalendar pc = new PersianCalendar();
                return string.Format("{0}/{1}/{2}", pc.GetYear(englishDate), pc.GetMonth(englishDate).ToString("00"), pc.GetDayOfMonth(englishDate).ToString("00"));
            }
        }
    }
    

    Hope it helps.