Galin Iliev's blog

Software Architecture & Development

T-SQL Number Generator

Here is some more T-SQL fun: number generator Smile I found this code in comments of this blog post and it is so cools so I decided to save it here for reference:

-- use number generation
WITH Numbers3( n ) AS ( SELECT 1 UNION SELECT 0 ),
     Numbers2( n ) AS ( SELECT 1 FROM Numbers3 n1 CROSS JOIN Numbers3 n2 ),
     Numbers1( n ) AS ( SELECT 1 FROM Numbers2 n1 CROSS JOIN Numbers2 n2 ),
     Numbers0( n ) AS ( SELECT 1 FROM Numbers1 n1 CROSS JOIN Numbers1 n2 ),
     NumbersBase ( n ) AS ( SELECT 1 FROM Numbers0 n1 CROSS JOIN Numbers0 n2 ),
     Numbers ( Number ) AS 
         SELECT n
         FROM NumbersBase ) D ( n )
         WHERE n <= 50

T-SQL: Get Date and Hour

I am back in T-SQL fun and had interesting problem to solve – group rows by date and hour but eliminate minutes, seconds and milliseconds. Thus here is a handy function to do that:

DECLARE @Date datetime= getdate();

SELECT DATEADD(ms, -1*DATEPART(ms, @date), -- remove milliseconds
    DATEADD(SECOND, -1*DATEPART(SECOND, @date), -- remove seconds
    DATEADD(MINUTE, -1*DATEPART(MINUTE, @date), -- remove minutes

I hope you know how to create function from this code Smile.

Have fun!

MSBuild - Debug a msbuild script–enters the debug switch

It would be great if it was possible to debug msbuild script. Step in, step out, see properties and values… Seems like there is a way to do it via VS2010 although unsupported. What needs to be done is:

  1. Ensure you debug “Just my code”:
  2. Enable undocumented "/debug" switch on MSBuild.exe by adding registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\4.0 key to have debuggerenabled=true. Execute this in command prompt:
    reg add HKLM\SOFTWARE\Microsoft\MSBuild\4.0 /v DebuggerEnabled /t REG_SZ /d true
  3. Check /debug switch:
    MSBuild /?
  4. Run your build with /debug switch:
    MSBuild MyCode.proj /debug
  5. Once you get standard JIT debugging prompt make sure you select "Manually choose the debugging engines".
  6. Select Managed mode only:
  7. Enjoy


Thanks to Visual Studio blog and Dan Smile

Also John Robins has a way to do it but I didn’t try it…

VS2010: Cannot change the specified .NET framework version or profile for a test project

I received the error message "attempted re-targeting of the project has been canceled. You cannot change the specified .NET framework version or profile for a test project" when trying to target the .NET 3.5 framework after upgrading to Visual Studio 2010.

And this was tricky – we want to move to VS 2010 but still compile against .NET .3.5. Although having tests target .NET 4 is not critical is not very nice. We don’t want to have bugs slipped out because production code and test code are compiled against different .NET versions.

Luckily there is a solution – manually editing .csproj file and trick VS2010 to think it is not a test class library but simply class library:

  1. Open .csproj file with notepad.
  2. Search for this line:
  3. <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};
  4. Remove {3AC096D0-A1C2-E12C-1390-A8335801FDAB} guid
  5. Save file and Open in VS 2010
  6. It is ready to be re-targeted

Hope this helps

TFS 2010 Basic: Server build of Windows Phone 7 project

I just started taking advantage of TFS 2010 Basic (or TFS 2010 for SourceSafe users as Brian Harry named it Smile) and found something interesting to share. The project I was testing with is Windows Phone 7 one and it was building perfectly form VS 2010. But when triggered a server build the agent fails with following error:

C:\Program Files (x86)\MSBuild\Microsoft\Silverlight for Phone\v4.0\Microsoft.Silverlight.Common.targets (185): The "CompileXaml" task failed unexpectedly. System.IO.FileLoadException: Could not load file or assembly 'PresentationCore, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047) File name: 'PresentationCore, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35'  Server stack trace:     at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)    at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection, Boolean suppressSecurityChecks)    at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)    at System.Reflection.Assembly.Load(String assemblyString)    at Microsoft.Silverlight.Build.Tasks.CompileXaml.LoadSilverlightAssemblies(String[] frameworkPaths)    at Microsoft.Silverlight.Build.Tasks.CompileXaml.GenerateCode(ITaskItem item, Boolean isApplication)    at Microsoft.Silverlight.Build.Tasks.CompileXaml.Execute()    at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs)    at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg, Int32 methodPtr, Boolean fExecuteInContext)  Exception rethrown at [0]:     at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)    at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)    at Microsoft.Build.Framework.ITask.Execute()    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()    at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask, Boolean& taskResult) 

As the name of TFS 2010 Basic suggests I have everything on same machine – VS 2010, TFS server, build controller, build agent… and it still failed.

The solution is in Build definitions: set MSBuild platform to X86:


Seems like Windows Phone 7 projects need to be build using x86 as a target platform only…

Hope this helps

VS 2010: Executing Unit Tests in parallel

This almost slipped out of my sight. Visual Studio 2010 adds an option to execute unit tests in parallel. Probably it would be obvious but let me emphasis that all multithreading issues could arise and let tests fails in such case. So all tests must be thread safe and tested code as well…

The option to enable this is very hidden Smile It is not exposed via VS GUI but in .testsettings file instead. It has to be edited in XML editor and add to <Execution> parallelTestCount attribute greater than 1(e.g. parallelTestCount=”4”)

Hope this helps

SQL Server 2008 Management Studio – Saving changes is not permitted error.

I’ve been away from database side for a while but these days I am designing one. And I had a strange error while working in SQL Server Management Studio 2008. I made some changes and while trying to same the diagram I was shown this message:

“Saving changes is not permitted. The change you have made require the following tables to be dropped and re-created. You have either made changes to a table that can’t be re-created or enable the option Prevent saving changes that require the table to be re-created.”


While message explains the issue well I couldn’t spot this option right away. So here is it: Tools->Options->Designers

Uncheck the box and that’s all..:


Hope this helps.

How to install the Windows Phone 7 Developer Tools on Windows Server 2008

Probably many of of you are like me – working on Windows Server (2008 R2) and wondering how to install the Windows Phone 7 Developer Tools on server OS. Luckily Aaron Stebner posted a solution on his blog:

There is a way you can work around the Windows Server 2008 setup block if needed.  Please note that this is not officially supported, so if you try these steps, you are doing so at your own risk.

  1. Download the Windows Phone Developer Tools web bootstrapper and save it to your hard drive
  2. Extract the contents of the setup package by running vm_web.exe /x and choosing a path to extract to
  3. Go to the folder you extracted to in step 2 and open the file baseline.dat in notepad
  4. Look for the section named [gencomp7788]
    Note - you have to change this exact section - this is the one that controls the OS version blocking behavior in Windows Phone Developer Tools setup.
  5. Change the value InstallOnLHS from 1 to 0
  6. Change the value InstallOnWin7Server from 1 to 0
  7. Save and close baseline.dat
  8. Run setup.exe /web from the folder you extracted to in step 2
    Note - please make sure that you include the /web command line parameter in step 8.  If you don't, setup will not attempt to download the packages it needs to install, and it will fail to install correctly as a result.

Neat… Thanks Aaron!

Silverlight 4: Setting a global application theme

I am creating a SL4 application and I was looking for a way to make more shinier. Of course themes come to play here but the big questions is “How to setup a global theme?”

Search results returned a bunch of ideas with a lot of code like merging dictionaries etc… And I was thinking it should be simple, very simple.

I came across this post about Silverlight4 Toolkit April 2010 release. But still what suggested there didn’t work for me:

<Application xmlns=""

For some reason the highlighted part failed come into play. No result was seen…

So I found this very, very simple way. Just put those two lines in App.xaml.cs:

using ActiveTheme = System.Windows.Controls.Theming.BureauBlackTheme;
private void Application_Startup(object sender, StartupEventArgs e)
    ActiveTheme.SetIsApplicationTheme(this, true);
    this.RootVisual = new MainPage();

Hope this helps,