Click here to Skip to main content
15,868,340 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is the C# function which is throwing random
'System.OutOfMemoryException'
,

public string GetFormattedDate(string datetoparse, string format = "MM/dd/yyyy")
       {

           DateTime dt;
           string parsedDate = "";
           string[] otherValidFormats = CommonFunction.BindEnum(typeof(Enums.ValidDateFormat)).Select(x => x.Value).ToArray<string>();

           System.Globalization.CultureInfo vCulture = new System.Globalization.CultureInfo("en-US");
           vCulture.DateTimeFormat.FullDateTimePattern = "MM/dd/yyyy hh:mm tt";
           vCulture.DateTimeFormat.ShortDatePattern = "MM/dd/yyyy";

           if (!DateTime.TryParse(datetoparse, vCulture, System.Globalization.DateTimeStyles.None, out dt))
           {
               vCulture.DateTimeFormat.FullDateTimePattern = "yyyy/MM/dd hh:mm tt";
               vCulture.DateTimeFormat.ShortDatePattern = "yyyy/MM/dd";
               if (!DateTime.TryParse(datetoparse, vCulture, System.Globalization.DateTimeStyles.None, out dt))
               {
                   vCulture.DateTimeFormat.FullDateTimePattern = "dd/MM/yyyy hh:mm tt";
                   vCulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
                   if (!DateTime.TryParse(datetoparse, vCulture, System.Globalization.DateTimeStyles.None, out dt))
                   {
                       vCulture.DateTimeFormat.FullDateTimePattern = "yyyy/dd/MM hh:mm tt";
                       vCulture.DateTimeFormat.ShortDatePattern = "yyyy/dd/MM";
                       if (!DateTime.TryParse(datetoparse, vCulture, System.Globalization.DateTimeStyles.None, out dt))
                       {
                           if (!DateTime.TryParseExact(datetoparse,otherValidFormats, vCulture, System.Globalization.DateTimeStyles.None, out dt))
                           {
                               return string.Empty;
                           }
                       }
                   }
               }
           }

           vCulture.DateTimeFormat.DateSeparator = "/";
           parsedDate = dt.ToString(format, vCulture);

           return parsedDate;

       }


Exception of type 'System.OutOfMemoryException' was thrown.   at System.Globalization.CalendarData.nativeGetCalendarData(CalendarData data, String localeName, Int32 calendar)
   at System.Globalization.CalendarData..ctor(String localeName, Int32 calendarId, Boolean bUseUserOverrides)
   at System.Globalization.CultureData.GetCalendar(Int32 calendarId)
   at System.Globalization.DateTimeFormatInfo.internalGetAbbreviatedMonthNames()
   at System.Globalization.DateTimeFormatInfo.get_FormatFlags()
   at System.__DTString..ctor(String str, DateTimeFormatInfo dtfi)
   at System.DateTimeParse.TryParse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles, DateTimeResult& result)
   at System.DateTimeParse.TryParse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles, DateTime& result)
   at ADMISION.CommonCS.Common.GetFormattedDate(String datetoparse, String format)
   at ADMISION.Task.API.Slate.GetGenericFields(MyParameters myParameters, Int64 triggerId, Int64 universityId, Nullable`1 taskLastModifyDate, Nullable`1 taskLastRunDate)
   at ADMISION.Task.API.Slate.FetchDataToProcess(TaskObject taskObject, HubApiTask task)
   at ADMISION.Task.TaskCS.Processor.ProcessTask(TaskObject taskObject, HubApiTask apiTask, HubTaskHistory hubTaskHistory, String& returndata) 


What I have tried:

I am not sure how to resolve this bug. 
Posted
Comments
Ashwin. Shetty 17-Jan-17 3:45am    
Can you share function input data.
Richard MacCutchan 17-Jan-17 4:22am    
You must be doing something at a level above this method to cause this exception. Check the stack trace to see where you are calling it from. Either that or you have some code somewhere that is using all the available memory.

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



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