Monday, 18 February 2008

How to set the connection string in your LINQ dbml file dynamically (based on web.config)

I've used this technique in my last 3 projects. Ideally, you want your LINQ database connection strings defined outside your compiled code and to be held in the Web.Config instead.

Unfortunately, by default, they are added to your Settings.cs file which is then compiled away into your code. This does the following:



  1. reduces your ability to easily configure your applications for different environments (ie you would need to have different compiled dlls for dev, test and production).

  2. It is also LESS secure because you can easily use reflection to examine your dlls for passwords - whereas you can encrypt the web.config so it is only viewable to people who have permissions to the IIS console.This is more difficult to break.

The recommendations to remedy this issue in the following MSDN blog http://blogs.msdn.com/jongallant/archive/2007/11/25/linq-and-web-application-connection-strings.aspx is slightly wrong. He suggests that you remove the default constructor in the designer. This is bad because you would have to fix up the file every time you regenerate your dbml file. Instead, you should use what is provided to you and set the property on the designer for "Application Settings" to false.






This then allows you to define a default constructor in your own partial class that extends your dbml context designer classes:



//

// Copyright (C) 2007 ddkonline

// All rights reserved.

// http://www.ddkonline.com

//

// File - InvestmentManagementDb.cs

// Author – David Klein

// Date Created – 18/02/2008

//

// History of changes

// ---------------------------------------------------------------------------------------

// Version Author Date Jira No.

// 1.1 David Klein 18/02/2008 LPP-59

//

// Description of changes

// ---------------------------------------------------------------------------------------

// Version Description

// 1.1 DBML now reads from Web.Config via ConfigManager.ConnectionStrings

// ---------------------------------------------------------------------------------------



using System.Configuration;

using ddkonline.InvestmentManagement.Domain.Properties;

using System;



namespace ddkonline.InvestmentManagement.Domain.Entity

{

public partial class InvestmentManagementDataContext

{

public InvestmentManagementDataContext()

: base(ConfigurationManager.ConnectionStrings["InvestmentManagementConnectionString"].ConnectionString, mappingSource)

{



}







}

}

Sunday, 17 February 2008

Installing Multiple Versions of Internet Explorer for testing

There are a few different ways to get multiple versions of IE running on the same machine (e.g. IE 6 and 7 running together to test IE Compatibility):
  1. Hack the registry (e.g. http://www.positioniseverything.net/articles/multiIE.html)
  2. Run a VPC Image for testing purposes (you can obtain some of these test VPCs from the Microsoft IE Compatibility VHD dowload site at http://www.microsoft.com/Downloads/details.aspx?FamilyID=21eabb90-958f-4b64-b5f1-73d0a413c8ef&displaylang=en)
  3. Testing on your build & test machine via terminal services (mstsc)

I didn't like the idea of a 500MB VPC on my HDD just for testing. This makes it more difficult and painful to test my application regularly. Instead, I have found that the simplest approach is to use the following install package:

http://tredosoft.com/Multiple_IE

This setup package uses a feature found in Windows 2000 and above called DLL redirection. Just select the versions you want and test away. No mess, no fuss.

Thursday, 14 February 2008

ASP.NET double-postback bug strikes again!

A bug in ASP.NET has returned from the dead to haunt me today. I sat with one of my colleagues for over an hour to try and work out the issue to no avail. The problem was that the first Save worked fine - but I kept getting Optimistic Concurrency data errors when I saved the record for the second time (never the first). The big problem with ASP.NET bugs is that you tend to look in your code for the problem - that single attribute that shouldn't be there, the new code in our custom ObjectContainer data source or translation layer, that line of code that nullifies the value, that AJAX control not set up correctly. But there was none. After stepping through the code line by line, removing any Telerik Ajax Managers and Postback Panels -and anything remotely suspicious at - the problem was still there. After a bit more Divide and Konquering (DK for short) with CTL+E, CTL+C (the VS Shortcut for commenting out code), I finally found my old nemesis of the single empty image tag (which still exists in ASP.NET 3.5).

THE BUG:
An example of this bug is detailed here:
http://www.velocityreviews.com/forums/t119525-repost-gridview-imagebutton-causes-double-postback.html

It is also detailed here:
http://www.dotnetspider.com/qa/Question8706.aspx

The issue occurs if you have any image tags rendered by your controls which have an empty source. As soon as the browser hits that <img src=""/> tag it does a refresh of the page. The main problem with this double postback is that the second run is NOT a postback - the Page.IsPostBack property is false - but I still have all my viewstate values. This issue is incredibly frustrating as the natural inclination is to look at controls causing partial postbacks - but you'd be looking in the wrong place. I had this issue in IE 6,7 and Firefox 2.

When I have some spare time, I'll look into how the problem occurs with Lutz Roeder's handy Reflector and find out who to tell to fix this reocurring issue :o)

THE FIX:
To stop the double post-backs, just make sure all your ASP.NET controls (Image Buttons, Image Columns in grids, normal Images), all have a src attribute filled in - or otherwise, make them invisible. Otherwise, you will have phantom postbacks coming to haunt you when you least need it!

Monday, 11 February 2008

Visual Studio 2008 Freezing Problem fixed!

Finally! My problem with ongoing freezes in HTML source view (not to mention the designer) have been fixed. Rick has written about some of these issues here: http://www.west-wind.com/WebLog/posts/201463.aspx

See http://weblogs.asp.net/scottgu/archive/2008/02/08/vs-2008-web-development-hot-fix-roll-up-available.aspx for the hotfix and download details. Thanks to Scott Guthrie and his team. Bring on the MVC framework & VS2008 SP1!

Tuesday, 5 February 2008

Rehash: Dave's simple SQL Workbench/Query Analyzer Code Generator

This is a perenially handy tool for generating database-oriented code using the most basic of tools: SQL 2005 Workbench. It makes use of system tables or the information_schema and is a very basic (but very useful) tool for code generation - especially when you have hundreds of tables.

Here is one I just made today to avoid having to create a CodeSmith template (which we don't have licences for) or doing the monkey work of manually typing in these attributes. Just set the output mode in isqlw/SQL Workbench to text and generate away! (minus the chimps..)


SELECT
'[EntityPropertyMapping("' + column_name + '")]' + CHAR(10) +
'public ' + CASE data_type
WHEN 'nvarchar' THEN 'string'
WHEN 'int' THEN 'int'
WHEN 'datetime' THEN 'DateTime'
END
+ ' ' + column_name + ' { get; set; }' + CHAR(10) + CHAR(10)
FROM information_schema.columns
WHERE table_name = 'Asset'

Output is:


[EntityPropertyMapping("AssetName")]
public string AssetName { get; set; }

[EntityPropertyMapping("Description")]
public string Description { get; set; }


[EntityPropertyMapping("Comments")]
public string Comments { get; set; }

Fixing the Compile-Time Exception "'MethodName' is is not supported by the language"


Today I got a few cryptic errors when trying to compile a project in which I had updated some referenced dlls:


Error 2 'GetImages' is not supported by the language D:\DataSourceControl\Global\ddkonline.IM\dev\ddkonline.InvestmentManagement\CodeBase\Modules\AssetMaintenance\Services\AssetService.cs 301 55 AssetMaintenance (Modules\AssetMaintenance\AssetMaintenance)


At first glance, this error doesn't make sense. Did they remove a feature of the language when I wasn't looking :o). The cause and solution of this problem is simple. At Lend Lease, we have a common .NET framework that all projects reference by dll. I updated the reference Dlls for some of the projects - but not the whole set. When you update a dll that other referenced dlls depend on, you will get the cryptic error above (reminds me of binary/project compatability problems in the non-.NET days!). To fix, just get the current version of all related dlls.

This fix didn't help me much though, as I needed features from 2 separate branches of the same project - BOTH the old version of the dlls (which had updates to the Business objects) the new version of the dlls (which had updates to the Sharepoint Integration components). Looks like I've got some subversion merging to do :o)



Sunday, 3 February 2008

Downloading YouTube videos via Google Cache

You don't need an application to download YouTube videos simply - you can just download them via the folks @ google video. Just replace the video ID with the one on your youtube clip:

e.g. to download the number 1 video on Google at the moment ( a Russel Peters comedy clip) , you can enter:
http://cache.googlevideo.com/get_video?video_id=24Ryj1ywoqw&origin=youtube.com

Note that the cache only works for videos uploaded after YouTube was acquired by Google.

Alternatively, you can go to http://keepvid.com/ - which handles many other sites as well.

Thanks again Google! :o)