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 > 0)
  {
   for (int ii = 0; ii < args.Length && mode == RunMode.NotSpecified; ii++)
   {
    //Default commands for support -h and /?
    if (args[ii].ToLower().Trim() == "-h" || args[ii].Trim() == "/?")
    {
     PrintHelpText();
     return (false);
    }
    else
    {
     //Get clear input params
     string inputParam = args[ii].ToLower().Trim();
     inputParam = Regex.Replace(inputParam, "[/-]", "");

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

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

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


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:


<asp:ImageButton ImageUrl="~/Includes/Images/delete_icon.gif" runat="server" ID="ibtDeleteClip" OnCommand="ibtDeleteClip_Command" CommandArgument='' />


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

Running WCF on IIS 5 (Windows XP)

After running WCF on my Vista laptop (IIS7) I needed to deploy the application on some Windows XP computers. Again some strange errors occurred:

[code:html]

Error Description: "This collection already contains an address with scheme http.  There can be at most one address per scheme in this collection.
Parameter name: item"

[/code]

The problem is that WCF cannot handle more than one identity (host headers) per website. At first I configured IIS to have one HostHeader. That solution was just to dirty. So I kept on searching the internet.


You can fix this problem by adding prefix-key(s) in the baseAddressPrefixFilters section of the Web.Config:

[code:xml]
<system.serviceModel>
<serviceHostingEnvironment>
<baseAddressPrefixFilters>
        <add prefix=”http://www.local.develop”/>

</baseAddressPrefixFilters>
</serviceHostingEnvironment>
</system.serviceModel>
[/code]


Hope this helps.


Sources:
http://geekswithblogs.net/robz/archive/2007/10/02/WCF-in-IIS-with-Websites-that-have-Multiple-Identities.aspx
http://blogs.msdn.com/rampo/archive/2008/02/11/how-can-wcf-support-multiple-iis-binding-specified-per-site.aspx

Asp.Net: Databinding a array of strings

You can use a string array as datasource and view the string values by using the Container.DataItem property.

Code example

Codebehind:

[code:c#]

string[] testData = {"1","two","3","4"};
rptDemo.DataSource = testData;
rptDemo.DataBind();

[/code]


And in the .aspx:

[code:html]

<asp:Repeater runat="server" ID="rptDemo">
    <ItemTemplate>
        <%# Container.DataItem %>
    </ItemTemplate>
</asp:Repeater>

[/code]