Passed 70-529 today

I just passed the 70-529 exam and now I’m a MCTS Web and Distributed Applications. For those that are still thinking of taking this exam here are a few tips.

Usually I use Microsoft preparation books to study for certifications. This time I started using a book from the same collection:

MCTS Self-Paced Training Kit (Exam 70-529): Microsoft .NET Framework 2.0 Distributed Application Development

I have to say that I didn’t like this book. I didn’t think that the content was well approached and the writing style was also not for me. I still followed the book to the end but I had to complement it with two other books, one the had a good web services section and another that had a good remoting section. These books are:

Pro ASP.NET 2.0 in C# 2005 (This one complements the web services part)

Pro C# 2005 and the .NET 2.0 Platform (This one complements the remoting part)

Also a good starting point is the articles on the 4 Guys from Rolla that will give you a pretty good introduction to web services and it’s best if read before you read any of the books above.

Next week I’ll go on vacation and when I come back I’ll start my studies towards the MCPD Web Developer certification. I should take the test by the end of November. I’ll let you know how this goes.

LINQ: OrderBy with nullable columns in TypedDataSets

This article shows a tip on how you can do sorting using the LINQ OrderBy with Typed DataSets. For our example I’ll use the the Northwind Database and it’s Customers table. I’ll use two columns in this table. One is the ContactName which doesn’t have any null values and the Region column which has null values.

First lets populate the DataTable we want to query using TableAdapter I had previously generated in the DataSet.

CustomersTableAdapter ta = new CustomersTableAdapter();
DataSetNorthwind.CustomersDataTable dt = ta.GetData();

I want to use LINQ select all values ordering them by the Region column (which may be null). So here is the initial idea:

 var query = from r in dt.AsEnumerable()
                        orderby r.Region
                        select r;

Ok, no compilation errors but when you run the query you will get an error:

The value for column ‘Region’ in table ‘Customers’ is DBNull. -> System.InvalidCastException: ...

If you think about it this is expected. The nullable columns in the DataSet all have an IsNull method to check if the column is null or not and avoid this kind of error. We are going to use the IsRegionNull() method in our favor to help us solve the problem:

var query = from r in dt.AsEnumerable()
        orderby (r.IsRegionNull() ? "" : r.Region)
        select r;

I used the ternary operator to check if the column region is null or not. If it is then I’ll use an empty string as the value, if it’s not null I can call the Region property to get the value.

To see the ending result you can print all the values:

foreach (DataSetNorthwind.CustomersRow row in query)
{
     Console.WriteLine("{0} - {1}", row.ContactName, 
          row.IsRegionNull() ? "" : row.Region);
}

Of course you could have filtered out the rows with null regions, but the goal here is to show what you can do to order the rows when you have a column with values that may be null.

Get the number of business days between two dates

Here’s a simple way of getting the number of business days between two dates using C#.

DateTime dtBegin = new DateTime(2008, 8, 6);
            DateTime dtEnd = new DateTime(2008, 8, 13);

            int dayCount = 0;

            //while the End date is not reached
            while (dtEnd.CompareTo(dtBegin) > 0)
            {
                //check if the day is not a weekend day
                if ((dtBegin.DayOfWeek != DayOfWeek.Saturday) && (dtBegin.DayOfWeek != DayOfWeek.Sunday))
                {
                    dayCount++;
                }
                //go to next day
                dtBegin = dtBegin.AddDays(1);
            }

You can enhance this method consulting a list of holidays to check if the current date is indeed a business day. For this you have to have the list of holidays somewhere. In our example I’ll create a fake method that will provide the holidays list.

public static void CalculateNumberOfWeekdays()
        {
            DateTime dtBegin = new DateTime(2008, 8, 6);
            DateTime dtEnd = new DateTime(2008, 8, 13);
            List<DateTime> holidays = GetHolidays();

            int dayCount = 0;

            //while the End date is not reached
            while (dtEnd.CompareTo(dtBegin) > 0)
            {
                //check if the day is not a weekend day
                if ((dtBegin.DayOfWeek != DayOfWeek.Saturday) 
                    && (dtBegin.DayOfWeek != DayOfWeek.Sunday)
                    && (!holidays.Contains(dtBegin)))
                {
                    dayCount++;
                }
                //go to next day
                dtBegin = dtBegin.AddDays(1);
            }
            Console.WriteLine(dayCount);
        }

        public static List<DateTime> GetHolidays()
        {
            List<DateTime> holidays = new List<DateTime>();
            holidays.Add(new DateTime(2008, 8, 7));
            return holidays;
        }            

See you next time.

Asp.Net: UpdatePanel concurrent requests

Yesterday I was coding the infrastructure to start a thread in the server that would take too long to finish and would timeout the browser. The idea is that you could start a thread to run your long operation and let the server return the response to the browser. In order to give some feedback to the user I would query the server from time to time to check on the progress of the operation.

Everything was going great but then someone said the client wanted to be able to cancel the task running on the server. At first though it would not mess up what I had done already. The thing is that if the server was already querying the status of the task and I tried to cancel at the same time I would get an error.

Long story short, you have problem when the UpdatePanel makes multiple requests concurrently. When you do this the last request being executed wins. I looked around and found an interesting solution to this problem.

http://geekswithblogs.net/rashid/archive/2007/08/08/Asp.net-Ajax-UpdatePanel-Simultaneous-Update—-A-Remedy.aspx

What this guy did was to intercept the asyn requests and enqueue them so they are only done one at a time. Of all the things I found on the Internet this was the most interesting solution.

There is also an interesting solution in the Asp.Net site that allows you to define on among several UpdatePanel that gets precedence over the others. So, if one request is being executed and the preferred UpdatePanel trying to execute the other one is canceled. See it here