New Bug! in the App_Code/Controls/MonthList.cs With Arabic Translation [Fixed]

Topics: ASP.NET 2.0, Themes
Oct 6, 2008 at 9:09 PM
Edited Oct 31, 2008 at 7:47 PM
Hi,
When i'm configuring the BlogEngine.Net to use the Arabic language, i found a bug in the App_Code/Controls/MonthList.cs

Exceptions are thrown when the date in string format is parsed to a DateTime type
DateTime.Parse(date.Year + "-" + date.Month + "-01").
Bug Fixe:
The problem is that the arabic DateTime format HAS this pattern: "yyyy-MM-dd" (inversed):
You Replace this line:

string strDate = [Date].Year + "-"[Date].Month + "-01";

With this:

 DateTime dtDate;
string strDate = [Date].Year + "-" + [Date].Month + "-01";
if (!DateTime.TryParse(strDate, out dtDate))
{
        strDate =
"01-"[Date].Month + "-"[Date].Year ;
        dtDate =
DateTime.Parse(strDate);
}

 

The code look something like this:

 

  private static void BindMonths()
  {
   lock (_SyncRoot)
   {
    _Months.Clear();
    if (Post.Posts.Count == 0) return;

    DateTime first;
    if (Post.Posts.Count > 0 || !DateTime.TryParse(Post.Posts[Post.Posts.Count - 1].DateCreated.Date.ToString("yyyy-MM-") + "01", out first))
     first = DateTime.Now;
    int year = first.Year;
    int month = first.Month;

    while (first <= DateTime.Now)
    {
                    // -------------------------------------
                    // BUG FIXED WITH ARABIC LANGUAGE MODULE
                    // -------------------------------------
                    DateTime dtDate;
                    string strDate = first.Year + "-" + first.Month + "-01";
                    if (!DateTime.TryParse(strDate, out dtDate))
                    {
                        strDate = "01-" + dtDate.Month + "-" + dtDate.Year;
                        dtDate = DateTime.Parse(strDate);
                    }
                    // --------------------------
                    // END BUG FIXED
                    // --------------------------
                    List<Post> list = Post.GetPostsByDate(first, dtDate.AddMonths(1).AddMilliseconds(-1));
     list = list.FindAll(delegate(Post p)
     {
      return p.IsVisible;
     });

     if (list.Count > 0)
     {
                        DateTime date = DateTime.Parse(strDate);
      int posts = list.Count;
      _Months.Add(date, posts);
     }

     first = first.AddMonths(1);
    }
   }
  }

  private string RenderMonths()
  {
   if (_Months.Keys.Count == 0)
    return "<p>" + Resources.labels.none + "</p>";

   HtmlGenericControl ul = new HtmlGenericControl("ul");
   ul.Attributes.Add("id", "monthList");
   HtmlGenericControl year = null;
   HtmlGenericControl list = null;
   int current = 0;

   foreach (DateTime date in _Months.Keys)
   {
    if (current == 0)
     current = date.Year;

    if (date.Year > current || ul.Controls.Count == 0)
    {
     list = new HtmlGenericControl("ul");
     list.ID = "year" + date.Year.ToString();

     year = new HtmlGenericControl("li");
     year.Attributes.Add("class", "year");
     year.Attributes.Add("onclick", "ToggleMonth('year" + date.Year + "')");
     year.InnerHtml = date.Year.ToString();
     year.Controls.Add(list);

     if (date.Year == DateTime.Now.Year)
      list.Attributes.Add("class", "open");

     ul.Controls.AddAt(0, year);
    }

    HtmlGenericControl li = new HtmlGenericControl("li");

    HtmlAnchor anc = new HtmlAnchor();
    anc.HRef=Utils.RelativeWebRoot + date.Year + "/" + date.ToString("MM") + "/default" + BlogSettings.Instance.FileExtension;
                // -------------------------------------
                // BUG FIXED WITH ARABIC LANGUAGE MODULE
                // -------------------------------------
                DateTime dtNewDate;
                string newDate = date.Year + "-" + date.Month + "-01";
                if (!DateTime.TryParse(newDate, out dtNewDate))
                {
                    newDate = "01-" + date.Month + "-" + date.Year;
                    dtNewDate = DateTime.Parse(newDate);
                }
                anc.InnerHtml = dtNewDate.ToString("MMMM") + " (" + _Months[date] + ")";
                // ----------------------------------------
                // END BUG FIXED
                // ----------------------------------------
    li.Controls.Add(anc);
    list.Controls.AddAt(0, li);
    current = date.Year;
   }

   System.IO.StringWriter sw = new System.IO.StringWriter();
   ul.RenderControl(new HtmlTextWriter(sw));
   return sw.ToString();
  }

 

 

Now take a glance and you'll see that it works great :-p

Error details:

String was not recognized as a valid DateTime.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: String was not recognized as a valid DateTime.

Source Error:

HtmlAnchor anc = new HtmlAnchor();
anc.HRef=Utils.RelativeWebRoot + date.Year + "/" + date.ToString("MM") + "/default" + BlogSettings.Instance.FileExtension;
Line 111: anc.InnerHtml = DateTime.Parse(date.Year + "-" + date.Month + "-01").ToString("MMMM") + " (" + _Months[date] + ")";

li.Controls.Add(anc);

Source File: c:\inetpub\wwwroot\Blog\App_Code\Controls\MonthList.cs   
Line:
111 
Stack Trace: 
[FormatException: String was not recognized as a valid DateTime.]
   System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) +2834318
   System.DateTime.Parse(String s) +25
   Controls.MonthList.RenderMonths() in c:\inetpub\wwwroot\Blog\App_Code\Controls\MonthList.cs:111
   Controls.MonthList.RenderControl(HtmlTextWriter writer) in c:\inetpub\wwwroot\Blog\App_Code\Controls\MonthList.cs:144
   ASP.themes_marketplace_site_master.__Renderform1(HtmlTextWriter __w, Control parameterContainer) in c:\inetpub\wwwroot\Blog\themes\MarketPlace\site.master:105
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer) +8673113
   System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +32
   System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output) +51
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer) +40
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.Control.Render(HtmlTextWriter writer) +10
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.Page.Render(HtmlTextWriter writer) +29
   BlogEngine.Core.Web.Controls.BlogBasePage.Render(HtmlTextWriter writer) in C:\Documents and Settings\Mads Kristensen\Dokumenter\Visual Studio 2005\Projects\BlogEngine.NET\DotNetSlave.BusinessLogic\Web\Controls\BlogBasePage.cs:329
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266