Monday, 14 September 2009


If you have a control on an ASP.NET page and Visual Studio is having trouble rendering it in design view, then you can add the VIEWASTEXT attribute to the tag, and visual studio will then render this as text.

<object id=factory style="display:none"
  codebase=",5,439,30 VIEWASTEXT>

    <param name="template" value="MeadCo://IE7" />

Friday, 11 September 2009

Error MSB4019: The imported project "C:\Microsoft.CSharp.targets" was not found.

If you've received this error, it usually means that the targets file can not be found/identified by the current versiuon of visual studio....

I usually receive this error when attempting to import a .NET 3.5 project into a .NET 2.0 solution.

To tackle this particular scenario, you will need to open the project file (.csproj).... and if you wish to make the project .NET 2.0 ready, you will need to find the following line (specific to 3.0/3.5)...

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

and replace with...

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

Thursday, 10 September 2009

Visual Studio Debug Problem (Debugger Detaching)

During the development of an ASP.NET application, have you ever had the debugger randomly detach itself from the IE instance?

This is usually because VS gets confused regarding which isntance of IE to detach itself to... goto into Task Manager and see if there are any iexplore processes running that do not have a window handle. If so, end them and re-try...

If not, take a look at this post regarding new features introduced with IE8 that can cause this issue...

Friday, 4 September 2009

C# Conditional Operator ( ?: ) and Null-Coalescing Operator ( ?? )

Often times in code you want to assign one variable to another, but only if the variable is not null. If it is null, you want to populate the target variable with another ( perhaps default ) value. The code normally may look like this using the C# Conditional Operator:

string fileName = tempFileName !=null ? tempFileName : "Untitled;

If tempFileName is not null, fileName = tempFileName, else fileName = “Untitled“.

This can now be abbreviated as follows using the Null-Coalescing Operator:

string fileName = tempFileName ??"Untitled";

The logic is the same. If tempFileName is not null, fileName = tempFileName, else fileName = “Untitled“.

The Null-Coalescing Operator comes up a lot with nullable types, particular when converting from a nullable type to its value type:

int? count = null;
int amount = count ?? default(int);

Since count is null, amount will now be the default value of an integer type ( zero ).

These Conditional and Null-Coalescing Operators aren't the most self-describing operators :), but I do love programming in C#!

Thursday, 3 September 2009

C# - Difference between 'out' and 'ref'

When passing objects as parameters to functions, there may be a certain scenario where you wish to modify a parameter before a function call, modify its valur within the function, and retain its value after the function call... This example behaviour describes the actions of the 'ref' qualifier to a function argument. 'out' however, is quite similar...

ref qualifier
When passing an object to function, specifying it as ref requires the object to be initialised beforehand. You can then modify its value inside the function and continute to use the object once the function call has ended.

        private void ModifyByFive(ref int number)
            number += 5;

out qualifier
When passing an object to function, specifying it as out does not require the object to be initialised beforehand. However, the object can then be modified for this purpose within the function, and can be used outside the function once the function has executed. This can be treated as an 'extra return value'. However, using too many out parameters might require an architectural re-think; as a class or struct might prove more benificial.

        private void ModifyByFive(out int number)
            number += 5;

Tuesday, 1 September 2009

Registering a DLL as an MSI Custom Action

1. In Visual Studio, open your solution and right click on the setup project. Select "View" and then select "Custom Actions".

2. Select a custom action for which you which to register the DLL, this would usually be "Install".

3. Right Click on the Install directory and select "Add Custom Action".

4. You will then need to add a script which will be run as part of the MSI installation process. For this, we will use VB Script. Paste the below code into Notepad and save it as a VB Script (.vbs) file.

Dim WshShell
Set WshShell = CreateObject("Wscript.Shell") "regsvr32 /s DLLFILENAME.dll"
Set WshShell = nothing

5. Add this to your project and reference it as a custom action for your "Install" process.

6. You may wish to unregister the DLL file during the uninstall phase. Perform the previous processes which the uninstall VB Script below...

Dim WshShell
Set WshShell = CreateObject("Wscript.Shell") "regsvr32 /u/s DLLFILENAME.dll
Set WshShell = nothing