Asp.Net: Charting Control (Error ChartImg.axd)

After installing and playing with the Asp.Net Charting control I decided to use it for a customer. When integrating the control to the project I got the following error

"Error executing child request for ChartImg.axd"

This error occurred because I did not update my web.config file. You have to add the following appSettingkey, httpHandler and handler.

[code:xml]

<appSettings>
    <add key="ChartImageHandler" value="storage=file;timeout=20;dir=c:TempImageFilesDit;" />
</appSettings>
<httpHandlers>
    <add path="ChartImg.axd" verb="GET,HEAD" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false" />
</httpHandlers>

<handlers>
    <add name="ChartImageHandler" preCondition="integratedMode" verb="GET,HEAD" path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</handlers>

[/code]

More info about the Charting control:
http://weblogs.asp.net/scottgu/archive/2008/11/24/new-asp-net-charting-control-lt-asp-chart-runat-quot-server-quot-gt.aspx

Enjoy charting,
Pieter

Asp.Net: Menu control remove MenuItem (MenuItemDataBound)

For a project I needed to remove menu items to the pages in the folder ‘Subscriberpages’ when a the user is in the Role of ‘Marketing’ and ‘AccountManagement’. To do this I added the following code to the MenuItemDataBound event.


protected void mainMenu_MenuItemDataBound(object sender, MenuEventArgs e)
{
 SiteMapNode node = e.Item.DataItem as SiteMapNode;

 if (node.Url.Contains("<em>Subscriberpages</em>"))
 {
  if (HttpContext.Current.User.IsInRole("<em>Marketing</em>") || HttpContext.Current.User.IsInRole("<em>AccountManagement'</em>"))
  {
   // Get menu.
   Menu topMenu = (Menu)sender;

   // Remove dataitem from menu.
   topMenu.Items.Remove(e.Item);
  }
 }
}

To generate the menu I used the Web.sitemap and the asp:Menu control.

Asp.Net: Method to get week number (getweekofyear)

This method return the integer weeknumber of a specific date. 


public static int GetWeekNumber(DateTime date)
{
 // Gets the Calendar instance associated with a CultureInfo.
 CultureInfo myCI = new CultureInfo("nl-NL");
 Calendar myCal = myCI.Calendar;

 // Gets the DTFI properties required by GetWeekOfYear.
 CalendarWeekRule myCWR = myCI.DateTimeFormat.CalendarWeekRule;
 DayOfWeek myFirstDOW = myCI.DateTimeFormat.FirstDayOfWeek;

 return myCal.GetWeekOfYear(date, myCWR, myFirstDOW);
}


More info:
http://msdn.microsoft.com/en-us/library/system.globalization.calendar.getweekofyear.aspx

.Net: Example basic consoleapplication with parameters

This is a basic example of an console-application with parameters used for a scheduled tasks.

The application will be scheduled to run every day with the -n parameter. But the application can also run on a specific date by using the -d parameter.


class Program
{
 //Parameters
 private static DateTime theDate;
 private static RunMode mode = RunMode.NotSpecified;

 static void Main(string[] args)
 {
  if (CollectParameters(args))
  {
   Console.WriteLine("Beginning appliation in mode "{0}" with date {1}.", mode, theDate);
   Logger.LogEvent("Starting StatisticConsoleApp", args, (int)Logger.LogType.Message);

  }
 }

 private static bool CollectParameters(string[] args)
 {
  if (args.Length &gt; 0)
  {
   for (int ii = 0; ii &lt; args.Length &amp;&amp; mode == RunMode.NotSpecified; ii++)
   {
    //Default commands for support -h and /?
    if (args[ii].ToLower().Trim() == &quot;-h&quot; || args[ii].Trim() == &quot;/?&quot;)
    {
     PrintHelpText();
     return (false);
    }
    else
    {
     //Get clear input params
     string inputParam = args[ii].ToLower().Trim();
     inputParam = Regex.Replace(inputParam, &quot;[/-]&quot;, &quot;&quot;);

     switch (inputParam)
                    {
                        case &quot;n&quot;:
                            mode = RunMode.Normal;
                            theDate = DateTime.Now;
                            break;

                        case &quot;d&quot;:
                            mode = RunMode.SpecificDate;
                            if ((ii + 1) &lt; args.Length)
                            {
                                theDate = Convert.ToDateTime(args[ii + 1]);
                            }
                            break;
       default:
        if(args[ii].Substring(0,1) == &quot;-&quot;)
         Console.WriteLine(&quot;Unknown switch {0}. Ignoring this switch.&quot;, args[ii]);
        break;
      }
    }
   }

   if (mode != RunMode.NotSpecified)
   {
    return true;
   }
   else
   {
    Console.WriteLine(&quot;No valid parameter specified. Aborting processing.....&quot;);
    return false;
   }
  }
  else
  {
   PrintHelpText();
   return false;
  }
 }
 private static void PrintHelpText()
 {
  Console.WriteLine(&quot;nUsage: Statisticprocess application&quot;);
  Console.WriteLine(&quot;&quot;);
  Console.WriteLine(&quot;  -n&quot;);
  Console.WriteLine(&quot;    The application will run in normal mode.&quot;);
  Console.WriteLine(&quot;    The statistics will be processed with the current date.&quot;);
  Console.WriteLine(&quot;  -d&quot;);
  Console.WriteLine(&quot;    The application runs in &#039;specific date&#039; mode.&quot;);
  Console.WriteLine(&quot;    The statistics will be processed with the input date&quot;);
  Console.WriteLine(&quot;    The date may be specified in any format as long as it can be &quot;);
  Console.WriteLine(&quot;    translated unambiguously into a proper date.&quot;);
  Console.WriteLine(&quot;&quot;);
  Console.WriteLine(&quot;&quot;);
  Console.WriteLine(&quot;    Note: Only one mode can be specified.&quot;);
  Console.WriteLine(&quot;          If two modes are specified, the second mode is ignored.&quot;);
  Console.WriteLine(&quot;&quot;);
  Console.WriteLine(&quot;    Example: In this example application runs in &quot;specific date&quot; mode &quot;);
  Console.WriteLine(&quot;             for the date &quot;10-sep-2006&quot;.&quot;);
  Console.WriteLine(&quot;&quot;);
  Console.WriteLine(&quot;    TOOLNAME [-d 1-jan-2009]&quot;);
  Console.WriteLine(&quot;&quot;);
 }
}


If you copy this class into your program.cs you will get some errors concerning my own loghandler and the missing RunMode enum. To fix this delete the code lines for the logging and add the following enum to your console application.



public enum RunMode : byte
 {
  NotSpecified,
  Normal,
  SpecificDate
 }


This is not the clearest example but if you have any questions don’t hesitate to ask.

Asp.Net: Using the OnCommand Event with CommandArgument

When using a button, linkbutton or imagebutton with CommandArguments or CommandName you can use the OnCommand event instead of the OnClick event. Using the OnCommand Event you use less code to extract the CommandArgument and CommandName from the Event comparing to the OnClick event (because you don’t need to cast the control).
Code example

The Aspx:


&lt;asp:ImageButton ImageUrl=&quot;~/Includes/Images/delete_icon.gif&quot; runat=&quot;server&quot; ID=&quot;ibtDeleteClip&quot; OnCommand=&quot;ibtDeleteClip_Command&quot; CommandArgument=&#039;' /&gt;


And the codebehind (C#):



protected void ibtDeleteClip_Command(object sender, CommandEventArgs e)
{
   string commandArg = e.CommandArgument;
}


Microsoft Certified Professional Developer: Webapplications (MCPD: Web)

After finishing my MCTS: Sql Server 2005 I started learning for my last exam for becoming MCPD: Web applications. Yesterday I passed this exam with a score of 875. This MCPD status is for Asp.Net 2.0. Soon I will do my exam for .Net 3.5. 

Now I will start focusing more on WPF and Windows developing and start building more Silverlight applications.

Cheers, Pieter