safish.com

home of a small fish

03 May 2012 at 09:58
In code, you can check to see if your component is in design mode with the following code:
if (!DesignerProperties.GetIsInDesignMode(this))
{
    // do stuff
}
This is useful when you want to add code in the constructor that won't work when rendering at design-time.
11 April 2012 at 07:56

My machine was dying a miserable death today, as it has done often lately, and it was all down to a bug between Bluetooth software and Skype. What I noticed is literally hundreds of the same process "BluetoothHeadsetProxy.exe", with a new one spawning every few seconds.

My first problem was working out how to kill all the processes in one go - this turned out to be very simple, using the following command:

taskkill /F /IM BluetoothHeadsetProxy.exe

The next problem was working out why the processes were starting up, and I found the answer on StackExchange as usual.

To get rid of the problem:

  1. Go to Tools -> Options -> Advanced
  2. Click on the link at the bottom that says 'Manage other programs access to Skype', and remove all bluetooth software there
  3. Shut down Skype
  4. Run the command mentioned above from the command line to kill any processes that are hanging around, and restart Skype.
  5. When Skype asks you to connect with bluetooth software, click on Deny.
01 March 2012 at 15:24

Default constraints can be added in Management Studio by setting a default value for a field in the designer, or by specifying it in your table create script. This SUCKS though, because it generates a random name for the constraint, which can lead to inconsistencies across databases - if you ever need to drop the constraint you can't guarantee will work everywhere (assuming you have multiple development, staging, UAT instances).

The better approach is to create your defaults manually once the table already exists - this allows you to specify a name:

ALTER TABLE dbo.MyTable ADD CONSTRAINT
   DF_MyDefault DEFAULT 1 FOR MyColumn
GO
08 February 2012 at 01:19

I'm perpetually restoring databases, and doing it via the GUI using SQL Server Management Studio is tedious. Instead, I like to use a script to restore my database. This involves the creation of a .bat file and an accompanying .sql file, as follows:

The batch file (e.g. restore.bat)

There are three things to note here that you may need to change:

  1. The SQL Server instance
  2. The database name - in my example 'MyDatabase'
  3. The name of the accompanying .sql file (in my example called restore.sql). This assumes the .bat and .sql files are in the same database

sqlcmd -S localhost\SQL2008r2 -i restore.sql -v database=MyDatabase -v root="%CD%"
PAUSE

The sql file (e.g. restore.sql)

There are a few things to note here:

  1. The name of the backup file, in my example MyDatabase.bak - the script assumes this is also in the same folder
  2. The names of the files associated with your database. These may differ on the machine where the database was backed up, to the machine where you are restoring the database to. You can get these names from the original database in Management Studio by right-clicking the database and viewing properties, click Files, and checking the Logical Name column. If you don't know these, just put any old value in and you will receive an error when running the script that should show the correct name. In my example the data file is MyDataFile, and the log file is MyLogFile_log
  3. The user you want to grant db_owner access to. This user must exist as a login on the database instance. In my example, it is called 'myuser'. You may want to change the roles assigned to the user, I am just assigning the db_owner role.

USE MASTER
GO

DROP DATABASE $(database)
GO

RESTORE DATABASE $(database) FROM DISK = '$(root)\MyDatabase.bak'
WITH MOVE 'MyDataFile' TO 'c:\temp\MyDatabase.mdf',
MOVE 'MyLogFile_log' TO 'c:\temp\MyDatabase.ldf'
GO

USE $(database)
GO

sp_grantdbaccess 'myuser'
GO

sp_addrolemember 'db_owner', 'myuser'
GO
06 February 2012 at 12:22
Exporting an SVN repository to an Hg repo is dead easy with the latest version of TortoiseHg:
  1. Open Workbench, and click File...Settings...Extensions
  2. Select the "convert" checkbox and restart work bench
    hg convert extension
  3. From the command prompt, run "hg convert http://<yoursubversionrepo/> <yourdestinationfolder>", and you're done!
03 January 2012 at 13:11
Some useful jQuery plugins:
  • DropKick - superb replacement for standard drop-downs, with in-built graceful degradation
  • FlexSlider - awesome slider plugin for image display
  • Apprise - a nice little replacement for alert/confirmation boxes, although I\'m not sure how well this works across platforms. It doesn\'t look great on IE7/8.
  • Reveal - very nice modal dialog implementation
03 December 2011 at 07:35
This is the start of my own technical blog. I'll be shifting my current tech blog (http://salmontech.blogspot.com) to this location over the next few months, as well as restoring posts from the blog I had before that.
18 August 2011 at 10:01

I have a powershell script that is executed as a Cruise Control.NET task.  The problem is, the script sat in a length path with spaces in some of the folder names.  The “powershell” task itself doesn’t support spaces, so the solution to this is to use old-school 8 character directory names.

For example, if you script lies in the path “C:Parent FolderNotParent Folder”, set your task up as follows:

<powershell>
	<script>myscript.ps1</script>
	<executable>C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe</executable>
	<scriptsDirectory>C:\Parent~1\NotPar~1\</scriptsDirectory>
	<description>Build something.</description>
</powershell>
08 July 2011 at 16:03

We noticed in our error logs that we were getting quite a few of these good old errors:

"Cannot redirect after HTTP headers have been sent."
This wasn’t affecting our system from a usability point of view, but it’s irritating nonetheless. Anyway, the cause of the error was the following code in the OnActionExecuting override of a custom ActionFilterAttribute:
HttpContext.Current.Response.Redirect(“our url”, true)

Looks fine, right?

The error here is that within action filter attributes, you don’t know what other code is being executed in the request pipeline – in our case we had a BaseController adding caching headers in the OnResultExecuting override. 

The correct way to do this is to set the result of the filterContext object – this gets dealt with immediately and the error goes away.  The correct code is:

filterContext.Result = new RedirectResult(“our url”);

Pretty trivial, I know, but I Googled this and found very little out there that actually describes this error and the solution.

06 May 2011 at 17:07

I had a freaking nightmare deploying an MVC3 app today.  Seems simple.  Database was up and running.  The code was deployed and had been tested on our local systems.  I deployed via our amazing SVN deployment process, and published on the live server.  Permissions applied automatically, all the files appeared – all was good in the world.  Until I loaded up the site in my browser and hit…

Infinite Redirects

Fan-freaking-tastic.  Double-check the Web.config.  Fine.  Double-check the IIS mappings.  Fine. This ruined my day.  To cut a long story short: this all boils down to conflicts between the versions of MVC3 binaries between RC1 and RC2.  If you search on the web you will find numerous references to issues with the get_viewBag() call, and to be honest, I didn’t have to time to fully understand WHY this is such a cockup.

Either way, the solution I finally came up with was to UNINSTALL MVC3 on the web server, manually add the .dll references to our project, and force "Copy Local".

http://www.tugberkugurlu.com/archive/deployment-of-asp-net-mvc-3-rc-2-application-on-a-shared-hosting-environment-without-begging-the-hosting-company

The DLLs that must be referenced:

  • Microsoft.Web.Infrastructure
  • System.Web.Razor
  • System.Web.WebPages
  • System.Web.WebPages.Razor
  • System.Web.Helpers
  • System.Web.WebPages.Deployment (If you are deploying MVC RC 2, this assembly is necessary to deploy)
  • System.Web.Mvc

Make sure these copy locally to your bin folder (Right click, Properties, Copy Local = true)

This sorted out that bug.  Enter the next issue: The type or namespace name 'WebMatrix' could not be found (are you missing a using directive or an assembly reference?).  WTF!?

Web Matrix Agony Part 1

MVC3 RC2 has a bug where some of the source files have "using WebMatrix" references in them. As such, even though you don't need them, your project will not run without a reference to WebMatrix, which is annoying.

The obvious solution is to add these as a reference, but I tried that and it fails because WebMatrix requires an initialisation call in the Global.asax. Bummer.  I redeployed, and my error message now changed to

You must call the "WebSecurity.InitializeDatabaseFile" or "WebSecurity.InitializeDatabaseConnection" method before you call any other method of the "WebSecurity" class.

EFF THIS.

Web Matrix Agony Part 2

To get around this, in your global.asax make sure you add the following code at the bottom of the file:

namespace WebMatrix.Data { internal class Ignore { } }
namespace WebMatrix.WebData { internal class Ignore { } }

Reference: http://stackoverflow.com/questions/4146545/razor-helper-in-mvc-3-rc

This works as the statements in the MVC3 source binaries are only using statements!  I added these lines to the end of my Global.asax with a big comment about how much I hate the world and how much those 2 lines of code REALLY NEED TO STAY; removed the WebMatrix references I had added earlier, published, and redeployed.

And everything started working.