Jan Schreuder on .Net

.Net code samples, experiences, observations

View my professional profile on LinkedIn

Recent Posts

Tags

News

  • Inappropriate comments will be deleted at my discretion.

    The information and code samples in this weblog is provided "AS IS" without warranty of any kind, either expressed or implied, including but not limited to the merchantability and/or fitness for a particular purpose.

Community

Email Notifications

Tool suppliers

Tools

General

Microsoft

Favorite blogs

Archives

IE8 Release Candidate available for download

You may have noticed already, but the RC1 release of Internet Explorer 8 is now available for download at this location. Internet Explorer 8 offers the following new features:

  • Accellerators. Makes it easier to map directions, translate words, send email, etc. Learn more
  • InPrivate Browsing. IE 8 will not store history information, if you don't want to. Learn more
  • WebSlices. Seems to me like another form of RSS feeds. Learn more
  • Search Suggestions. Type a text in the address bar, and detailed suggestions appear in the drop down list. Learn more
  • Various new security features. We all know what it's for, right? Learn more

The website contains various video's that demonstrate the new features. Follow this link to visit the IE8 Beta web site.

If Microsoft is so good at testing, why does your software suck?

I was triggered to read a blog post by James Whittaker, software architect at Microsoft, through an article in Computable, a Dutch IT magazine. The dutch article was a short interview with James Whittaker about his work at Microsoft and for Visual Studio 10. He mentioned the title of his blog post to show that testers at Microsoft are aware that bugs do.

To read his post about testing at Microsoft, click this link: "if Microsoft is so good at testing, why does your software suck?"

From the same author, a post comparing developer to tester ratios at Microsoft and Google is an interesting read.

Both articles give a open look into Microsoft with regards to software testing.

A quote from the dutch article (poorly translated): "Perfect software? It's not going to happen. People build nothing that is perfect"

Sleepless in Seattle

LEAP2009This week I'm in Seattle as part of LEAP 2009, a Microsoft program for Dutch IT architects. Purpose of this program is to increase knowledge on Microsoft's vision on technical architecture, the future of software technology and enhancing architects knowledge of Microsoft's product range.

We're the guests of Microsoft for a three day conference at Microsoft Conference Center in Redmond. During the conference, Microsoft Architects will share the architecture strategy, technology and roadmap and will go into architectural details.

Some of the subjects:

  • IIS 7 future
  • Office Future
  • New World of Work
  • Visual Studio 10 .Net 4.0
  • Innovations in Identity management

Sessions are from 8:30 till 17:00. So with my current jet lag (I'm writing this at 3:45 local time...) the first day might be a challenge. I'll try to post whatever interesting I hear over the next couple of days.

What the f**k..

I see a lot of code from other developers and every now and then you see some real gems. Today I was looking at a query to see if and how it could be optimised. One of the things I did was look at the query plan to see if indexes were used for various joins. One of the items in the plan made me look at a table definition, to check on it's indexes. 

I then discovered that one of the tables joined into the query contained an index on every column?!?!?! I checked other queries that used the same table and discovered that all joins on that table were made on the column that made up the primary key. That primary key was a clustered index, so barely impossible to try and optimize that.

The F**K factor in this is of course what in heavens name posessed the developer that designed that table was actually thinking. The table contains 10 columns and 10 indexes. I removed all but the primary key and the query still performed as it was. Inserts and updates on the table are flying now, obviously.

Motto: Think before you index a column!!

How to: Delete rows from a table using a join on a second table

In my current project, we have a tables where the primary key consists of two values. Based on the result of a query, we needed to delete a few rows from that table. The query returned the two key values and we needed to join that information in the delete statement. It's not rocket science, but it did cost us some searching before we had a solution. To make sure I don't forget, this small blog shows how we did it.

First of all, when you need to delete information from one table based on information from a second, then this is the way to go:

DELETE t1
FROM MyTable1 AS t1 
inner join MYTABLE2 t2 
    ON t1.KeyValue1= t2.KeyValue1 and t1.KeyValue2 = t2.KeyValue2    

As you can see, you simply add the join and specify the key values for that join. However, we needed the result from a query. To do that, you can use the following solution:

DELETE t1
FROM MyTable1 AS t1 
inner join (SELECT KeyValue1, KeyValue2 from MyTable2) t2 
    ON t1.KeyValue1 = t2.KeyValue1 and t1.KeyValue2 = t2.KeyValue2

I simplified the query, because the original query is a lot more complex than this one. Again, it's not difficult, but could be useful if you run into a similar situation

Have you heard of MinWin yet?

On October 13, Eric Traut from the Core Virtual Machine Team at Microsoft’s Core OS Division, gave a presentation about the vritualization technology Microsoft currently has to offer. The full presentation can be seen here and takes about an hour.

During the presentation, he talks about various versions of windows, among which Windows 7. He demonstrates the previous versions of Windows (including Windows 1.0!!!), but also shows a running version of a minimal Windows 7 Core. Microsoft calls this MinWin and contains only the core components of the new Windows version. The core takes up about 25Mb (!!!) of diskspace and is capable of running in 40Mb of memory. Microsoft does not see a commercial future for this product. The section where he talks about the various windows versions and minwin can be viewed here. It was fun seeing those older versions of Windows again!

CopySourceAsHTML 3.0 has been released

Everyone who pastes code into his or her blog has probably used it. If you haven't, then you have to check it out. It allows you to copy code into your blogpost almost instantly, and produces things like this:

/// <summary>
/// Gets or sets the name of the server.
/// </summary>
/// <value>The name of the server.</value>
public string ServerName
{
    get 
    { 
        return this.serverName; 
    }
    set
    {
        this.serverName = value; 
    }
}

The authors of CopySourceAsHTML sent me an email today to inform me of their latest version. This version includes the following changes:

  • A number of bug fixes
  • The new option Save as HTML, allowing you to save the HTML code rather than posting it to your clip-board

This is also the first version to officially support Visual Studio 2008 and marks the return to active development of this VS add-on.

So if you like to add code to your blog the easy way, be sure to check out the new version. If you're using Community Server, be sure to read this post.. It explains how to change the settings for CopySourceAsHTML so that it resembles the example above.

How to - Create a missing designer.cs file

I was running into this just now and found a quick way to solve it, so I just posted it for future reference.

I have an ASP.Net web application project in .Net 2.0 which was migrated from .Net 1.1 some time ago. I noticed that a few aspx pages did not have a designer.cs file. All controls on those pages were listed as protected variable in the main code behind page, like this example:

/// <summary>
/// An example label
/// </summary>
protected System.Web.UI.WebControls.Label Label2;

And it annoyed me. It was not consistent with the new pages in the application, and most other aspx pages that were also there during migration did have the designer.cs file. So how to solve this, in order to get a more consistent use of ASP.Net. I followed the following steps to do this:

  1. First step - Make sure the latest version of the project is on your machine.
  2. Second step is to remove the protected variables for the controls from the normal code behind cs file.
  3. Now right-Click on the project and select 'Convert to Web Application'

The missing designer.cs is now created and added to the project. The protected variables, which were removed from the main code behind class are now in the partial class in the newly created designer.cs. The class in the original code behind class is also changed to a partial class.

If you forget step 2 and still want to remove the protected variables from your code behind class, then follow the following steps:

  1. Remove the protected variables for the controls from the normal code behind cs file.
  2. Open the ASPX file and make sure you view it in Source (HTML) mode
  3. Select the entire HTML mark up using CTRL-A
  4. Now press CTRL-K followed by CTRL-F

The HTML mark-up will be re-aligned and the designer.cs file will be recreated, including all the protected variables.

Syntax error? Really?!?!

In my current project, we have adopted Microsoft StyleCop as a tool to make sure everyone sticks to the same style of coding. One of the things we currently incorporate in our daily work is making sure our existing code conforms to the rules we agreed on. Today however, StyleCop refused to check a class because of a syntax error in the following line of code:

double fraction = totalStaff > 0 
    ? fraction = (double)completed / (double)totalStaff 
    : fraction = 0;

The code you see here is part of a calculation to get the percentage of users that have completed a certain training module. The compiler sees no problem, and the result is as you would expect. But looking at the code, you do see something really weird. The variable fraction is always assigned twice. Maybe not a syntax problem, but strange and not necessary.

The following code does the same and only assigns the value once.

double fraction = totalStaff > 0 
    ? (double)completed / (double)totalStaff 
    : 0;

So why would someone do it twice?

Thank you, Lutz Roeder

I just received an email from Lutz Roeder informing the users of Reflector that he decided to explore new opportunities. He has reached an agreement with RedGate software to continue work on Reflector. From his email:

I have reached an agreement to have Red Gate Software continue the development of .NET Reflector. Red Gate has a lot of experience creating development tools for both .NET and SQL Server. They have the resources necessary to work on new features, and Reflector fits nicely with other .NET tools the company offers.

Red Gate will continue to provide the free community version and is looking for your feedback and ideas for future versions.

For news and updates on Reflector, sign up for the .NET Developer’s Newsletter from Red Gate. To find out more about the agreement, see the interview on Simple Talk.

I really want to thank Lutz for all the great work he's done with Reflector, allowing us to explore the code inside .Net assemblies. He brought an excellent tool to our community and literally opened up .Net code for all to see

StyleCop - QuickStart tips

After using StyleCop for some weeks, there are some things I think that might be useful to others when they want to start using the tool. So here are some tips.

Tip 1 - Also download the documentation

The documentation for StyleCop is available as a separate download. It explains why the rules are introduced and how you can fix violations. Without this documentation, you may think some of the rules are useless or annoying.

Tip 2 - Do not run the tool on an entire solution

At least, not untill you're satisfied that your code complies with the rules. You get so many warnings in a project or solution where the tool hasn't been used before, that you may think to stop using it immediately.

Tip 3 - Running the tool on one class file

Could have been in tip 2 as well, but I wanted to make a distinction here. When you right-click in the code, you will see the item Run StyleCop. Select this, and the tool will only check that particular class file. The number of warnings is signicantly less than when you run the tool on an entire project or solution, so the results are more obvious and motivate you more to keep using the tool.

Tip 4 - Disable rules you really disagree to

Rules you really don't want to check can be disabled using the StyleCopSettingsEditor. How this can be done is explained in this blog post. I disabled a number of them. I also included a number of overrides to the check for hungarian notation. For example, I have boolean variable names like isValid and isCompliant. StyleCop used to warn me about using hungarian notation in these cases, whereas I'm not. You can add the 'is' part in the Hungarian tab of the StyleCopSettingsEditor and it will no longer warn you about this.

Tip 5 - Re-Align the source before you start

Select all the code using Ctrl-A, then press Ctrl-K followed by F. Visual Studio will re-align much of the code. This automatically eliminates a number of errors found by StyleCop.

 

Version 4.3 of Microsoft StyleCop available

A new version of StyleCop was made available only a few days ago, so I downloaded it and installed it. One thing I had to correct after installation was the Settings.SourceAnalysis file. Because of the name change to StyleCop, the extension of the settings file was also changed. But after that, I was back in business. One of the first things you notice is that the results are now shown as warnings in the standard errors and warnings list. And there are more changes:

  • A separate help file is available that explains the rules and shows how violations can be solved
  • A number of bug have been fixed
  • New rules, including a rule to check if Using directives are sorted, in line with the Visual Studio 2008 Organize Usings functionality

Another announcement on the StyleCop Teamblog suggested that they will be releasing SDK documentation to allow you to add your own rules to the tool. This is of course interesting if you have additional rules on top of the ones already available.

I've been using the tool for some weeks now and I really like the consistency in code style which is now slowly emerging in my projects. Where I thought I was using a consistent style in my own code, I was pointed out by StyleCop that in fact I wasn't. But that's all changing now.

You can download the new version of StyleCop here.

Sandcastle now on CodePlex

The Sandcastle project, Microsoft's tools to generate documentation from your well-documented code, is now available on CodePlex. That was already the case, but until about 2 month's ago, that was without the source code. You can now download Sandcastle including the source code from CodePlex.

You can find the related information about this decision here. And you can find the project in Codeplex here.

Need help with Crystal Reports? Look here!

Because I blog about Crystal Reports occasionally, and created a helper class to assist in integrating it into .Net applications, I get a lot of questions from people that have problems running their reports. Especially in production environments after they deploy the reports. Unfortunately, I'm not a Crystal Expert. I use it in my applications but that's how far it goes. I don't have all the answers people might want.

But I do know where you can find information about Crystal and where you do find answers to any problems you might have. I created a blog post a few weeks ago to sum up a number of resources that can help you find answers to almost all of your Crystal Reports issues. Click here to read that post.

Have your C# coding style analysed by Microsoft, part 2

Just as a small update on the use of StyleCop. I disagreed with some of the rules, like having to use spaces rather than tabs. It's not really an annoying rule, the only thing about it is that you have to agree on using spaces or tabs and then stick to it.

But still, I found out you can tune StyleCop to your needs. When you go the folder where the application is installed (on my machine C:\Program Files\Microsoft StyleCop 4.3) you will see a small application named StyleCopSettingsEditor.exe. Using that tool you can switch the rules supplied by the tool on or off. From a command prompt simply run the following command line:

StyleCopSettingsEditor.exe Settings.StyleCop

You will then see the following user interface:

Using this interface, you can simply select which rules are appropriate in your projects.

More Posts Next page »