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

October 2008 - Posts

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.