Cancel a tab change in Dotnet Winforms

August 24th, 2010

For Dotnet2 and lower there was no easy way of stopping a user from switching a tab in a tab control.  I have written a solution in Dotnet compact framework (2?) that inherited from TabControl and overrided a couple of methods that together with a flag or two made it possible to cancel tab switching.  If I recall correctly it never stopped the control from blinking but it did it’s job.  It wasn’t hard but it took some hours.  If you are forced to implement this in Dotnet2 you have to inherit, the overridable methods used do not all send an event.

With Dotnet 3 to the current 4 it is easier.  Just associate a method with the Selecting event.

I did it in Dotnet3.5 and noticed some flickering when trying to, and cancelling, the change of tab.

Cannot serialize

August 19th, 2010

Or Can’t serialise – whatever you prefer.

When using Dotnet and the built in serialiser from Microsoft it sometimes doesn’t work.  Just so.  To make it more complex the error message sometimes doesn’t say anything either, just something like FileNotFoundException.  Sucks.

One solution might be to use the XmlSerializerPreCompiler tool from SellsBrothers available gratis and with source code.  It is downloadable here.  The download is dotnet2 but I updated it to 3.5 easily with Visual Studio 2010.

There is GUI called XmlPreCompiler for it too.

You might run into that the registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Fusion as to have a DWord called EnableLog set to 1.  The XmlPreCompiler has to be restarted to understand this key has changed.

If you are working with Dotnet > 2 you have to compile the GUI from source.  It is not difficult – the Visual Studio solution file is in the source code.

When running the GUI tool and your assembly is referring to another assembly (DLL has reference to other DLL) it is important that the GUI tool’s exe is in the same folder as the assemblies you are looking at.  Unless the other assemblies are in the GAC I suppose…

Note that you might have to restart Visual Studio 2010 when up/down grading dotnet version.  It seems like VS holds reference to files with the earlier version.  This has happened to me both when downgrading 4->3.5 and upgrading 2->3.5.

This was a lot of chatter about the apps and problem but I ran into them all right now.  And if You are here – so might you.  Happy hacking!

(My problem in this case was that I had set Visual studio to break on all thrown exceptions.  Took me six hours to figure out…)

Which F/OSS license to choose?

August 19th, 2010

There are some hundred open source licenses to choose from and tons of documentation to dig through. Noone is able to grok that so help and guidance is welcome.

An LdBrouwer has written a quite short comparison. It doesn’t give you the full monty but points you in the right direction and above all – gives the noob and non-tech some basic knowledge and directions to steer by.

He also links to another site with some Yes/No questions to find out which license fits you. It doesn’t hold very many licenses and is a tad old but nevertheless worth a try.

Somewhere there is (was) a site. I saw it some years ago. It is also a simple expert system for choosing the right F/OSS license. But I can’t find it.

Update: Another article is here.

exception: Unable to cast object of type ‘System.Windows.Forms.SplitContainer’ to type ‘System.ComponentModel.ISupportInitialize’.

August 18th, 2010

When downgrading a client winform app from Dotnet4 to Dotnet3.5 one might get

Unable to cast object of type 'System.Windows.Forms.SplitContainer' to type 'System.ComponentModel.ISupportInitialize'.

The solution in my case was to just comment out the offending row (BeginInit) and its (EndInit) counterpart.

There is an early bug report on the subject but the error seems to linger.

categories: T4 | 3 comments »

exception: could not impersonate the client during assembly file operation

August 18th, 2010

When running the Create Assembly command in SQLServer for adding my CLR component to the database I got an exception similar to:

could not impersonate the client during assembly file operation

What it really was was a file-not-found error.
I was working on one machine but having the database on another.  Since Create Assembly takes a file name/path as parameter it also has to find it.  This means that the database process on the database machine is looking for the path; not the SQL tool.

The solution is to move the DLL to somewhere the database machine can reach it.

CREATE ASSEMBLY failed because method ‘…’ on type ‘…’ in safe assembly ‘…’ is storing to a static field. Storing to a static field is not allowed in safe assemblies.

August 18th, 2010

When working with CLR inside the SQLserver database one doesn’t have to deploy the DLL through Visual Studio.  Instead use the Create Assembly command.

There are several reasons for this command to fail and

CREATE ASSEMBLY failed because method 'AAA' on type 'BBB' in safe assembly 'CCC' is storing to a static field.
Storing to a static field is not allowed in safe assemblies.

is one of them.

Unless your assembly is very complex the diagnosis is easy.  The AAA is a static something.  If it is a property

public static Colour{ get; set; }

AAA will be

set_Colour

CCC is the assembly that refuses to install.

Find AAA.  Rewrite to be not static.  Compile.  Run create assembly again.

Exception: could not load file or assembly system.data.datasetextensions version 3.5.0.0

August 18th, 2010

When copying an application from a Wndows 7 developer machine to a XP test server i stumbled upon an error message similar to:

could not load file or assembly system.data.datasetextensions version 3.5.0.0

To make a long story short it turned out that the System.Data.DataSetExtenstions.dll was missing.

Dotnet is found in different places in different OSes so instead of searching I opened the project in Visual studio, found the reference to the file and looked at the reference’s properties.  There was the exact path.  Copied the file to the target machine’s application’s folder and it worked.
This solution, copying the DLL to the application’s folder, is not really recommended since the update system of Dotnet does not work any more.  It is better to find the correct place to put it.

I don’t know why this very DLL was missing from the target machine.

categories: tip | no comments »

The type or namespace name ‘log4net’ could not be found (are you missing a using directive or an assembly reference?)

August 17th, 2010

When creating an application with dotnet 4 (dotnet4) there is a caveat regarding log4net.  One has to change the Target Framework from the default .Net Framework 4 Client Profile to the full .Net Framework 4.

This is done through the project properties.

Honour those who should.

A slightly strange side note is that in my VS2010 log4net showed up in the intellisense to start with but disappeared later; until I did the Target-Framework patch.

The tutorial/quickstart I used is here.

categories: tip | 30 comments »

T4 – template and script

July 22nd, 2010

I mentioned earlier a way to write a template to call other templates.  The method worked only for simple solutions.  Now, with VS2010 I recommend following http://www.olegsych.com/2008/09/t4-tutorial-creating-reusable-code-generation-templates/ to create more reusable scripts.

One has to install Oleg’s T4Toolkit to get some new templates.  Mentioned, though hard to parse, is that one must blank the Custom tool property from the “template” template and probably change the output suffix for the “script” template.

TT still has some way to go to get truly simple to use.

categories: T4 | no comments »

T4 – <#@ Assembly name="$(SolutionDir)\T4Helper\bin\debug\T4Helper.dll" #>

July 16th, 2010

As mentioned here and here the assembly referencing has changed from VS2008 to VS2010.

Say you have a T4 helper assembly aptly named T4Helper and you have it in a project called… T4Helper.

MySolution
....MyProject
........MyT4Files
........MyOtherFiles
....T4Helper
........T4Source

The VS-macro-way to reference it from  MyProject is

<#@ Assembly name="$(SolutionDir)\T4Helper\bin\debug\T4Helper.dll" #>

It obviously has problems when switching away from debug compile.  But the line above will help you get the backslashes right at least.

Update:
Something with

$(ProjectDir)$(OutDir)

might do the trick.

categories: T4, tip | no comments »