Friday, 19 December 2008

Loading this assembly would produce a different grant set from other instances

If you get this exception message when trying to browse an ASP.NET site you are developing, first stop the site in IIS, then clear out the ASP.NET Temporary Files folder that relate to your site (default location is under C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files). Then start IIS and try browsing the site.

Localhost on IIS7

By default, browsing to localhost on IIS7 brings up a welcome screen. To associate localhost with a particular web site, right-click the web site in IIS7 and select "Edit Bindings" (or just "Bindings", depending on which pane you were in when you right-clicked; IIS is still as inconsistent as ever!). In the "Add Site Binding" dialogue box, add localhost as a host name. Apply the changes. You will now be able to browse to localhost.

Thursday, 18 December 2008

Visual Studio External Tools Menu

Further to my post about setting up Windows Explorer as an external tool within Visual Studio (http://www.neilpullinger.co.uk/2008/03/open-visual-studio-project-folder-in.html), I have set up a number of other tools set up in the same way. Here's my list now:







TitleCommandArgumentsNotes
Run test(full path to nunit.exe)$(ItemPath)
Reflector(full path to reflector.exe)$(ItemPath)
FxCop(full path to fxcop.exe)$(TargetPath)
Explorer(full path to explorer.exe)/e, /select, $(ItemPath)
Open in Notepad(full path to notepad.exe)$(ItemPath)
Open(full path to a batch file containing just "%1")$(ItemPath)
Error Lookup(errlook.exe, in VS\Common\Tools folder)(none)Not sure where this one came from; installed by default?
Spy++(spyxx.exe, in VS\Common\Tools folder)(none)Installed by default

I've excluded a couple of the others that are there by default (eg. Create Guid and Dotfuscator).

(Edited 19/12/2008)

Tuesday, 14 October 2008

Always set AutoPage to true in LinqDataSource

If you are binding a control to a LinqDataSource, always set the AutoPage property on the LinqDataSource to true (AutoPage=“true”) if you have enabled paging on the data-bound control. Without the AutoPage option set, the LinqDataSource will pull back all records in the data source every time a page is browsed. Not only is this inefficient and not scalable, but it allows the client to determine the level of resources required, which is not good practice.

<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="MyDataContext" TableName="MyTable" AutoPage="true"></asp:LinqDataSource>

Sadly, the AutoPage option doesn’t seem to be available in other DataSource objects, so a manual implementation is necessary for these.

Thursday, 4 September 2008

CustomValidator does not fire

Recently, I needed to ensure that a user fill in one of two TextBox controls on a form I was designing. I couldn't use two RequiredFieldValidator controls as that would mandate that both fields be completed. I therefore created a CustomValidator control and added it to the page. It did not work. There was a flaw in my thinking. The control did not ever fire when needed (ie. when both fields are blank), because the control does not, by default, validate blank fields. Thankfully, there is a way to change this. You can set the ValidateEmptyText to true:

<asp:CustomValidator ID="myValidator" runat="server" ErrorMessage="Error message" OnServerValidate="myValidator_ServerValidate ValidateEmptyText="true"></asp:CustomValidator>

Wednesday, 3 September 2008

Windows does not shut down

Slightly off-topic, but if your computer does not respond to an ordinary shutdown command, you can force a shutdown. To do this, select Start, Shutdown in the usual way and, as you click OK, hold down the control key. Use with care as this might terminate processes uncleanly. Always back up your data.

Thursday, 21 August 2008

FindControl Causes Null Reference Exception with NMock

When using NMock on code that uses FindControl, a null reference exception is thrown:

PlaceHolder placeHolder =

(PlaceHolder) _view.MyPanel.FindControl("myplaceholder");

You could use the indexer instead:

PlaceHolder placeHolder =

(PlaceHolder) _view.MyPanel.Controls[0];

But if you insert another control in the panel, you break your code. So, the alternative is to loop through the controls and find a match by ID:

ControlCollection ctrls = _view.MyPanel.Controls;

foreach( Control ctrl in ctrls )
{
if( ctrl.ID == "myplaceholder" )
{
placeHolder = (PlaceHolder) ctrl;
break;
}
}