Feed subscriptions April 2013 10 April 2013 at 08:53

With the shutting down of Google Reader, I've had to think about how I want to receive my favourite blog posts and podcasts. I've looked at all sorts of alternatives - Feedly, ifttt, but none of them has really done it for me. I've finally resorted to the technology I used 5 years ago - Microsoft Outlook. It actually works really well, but it's not easily portable and available everywhere I go so I thought I'd at least document my current list so I can easily set them up when I find a provider that works for me.

Blog Posts

Podcasts

Customising Console2 05 April 2013 at 08:08

Every time I get a new machine, I always have to set up Console2 again, and because I have the memory of a goldfish, I always forget how. So this time around, I thought I would blog the minor changes I make.

Add Visual Studio Command Prompt

The first thing I like to do is make sure I can open tabs to the relevant Visual Studio command prompts. Go to Edit...Settings...Tabs, and then set up a new tab as per the screenshot below (you can copy and paste the actual location of your command prompt by viewing the properties of your visual studio command prompt shortcut):

I also like to set up a custom icon for the prompt, but that is obviously optional.

Add shortcuts to your context menu

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOTDirectoryshellConsole2]
@="Open Console2"
[HKEY_CLASSES_ROOTDirectoryshellConsole2command]
@="C:\\Matt\Dropbox\\Utilities\\Console2\\Console.exe -d %1" 
[HKEY_CLASSES_ROOTDirectoryBackgroundshellConsole2]
@="Open Console2"
[HKEY_CLASSES_ROOTDirectoryBackgroundshellConsole2command]
@="C:\\Matt\\Dropbox\\Utilities\\Console2\\Console.exe -d  \"%V\"" 

Create a console2.reg file with the above as contents, making sure you change the paths to correctly point to your console2.exe file. This will give you two context menu additions:

  • When you right-click a folder, you will have an option to open a console at that folder location
  • When you right-click inside a directory, you will have an option to open a console at that directory location

Using SQL Server's BCP Import Utility 06 February 2013 at 17:10

I had an issue today trying to get data from a client where we had access to their database via their VPN, but couldn't do a straight SQL Server transfer. I opted to use the bcp utility to export the table to a 0.75GB file, which I was then able to zip up, download, and then import again to our local database, once again using bcp. I had never used to the tool before, but it was really easy to use, and I thought I would document it here for my future reference.

Export

I needed a filtered subset of all the data in a table, so I opted to use a query for the import.

bcp "SELECT  FROM [dbo].[DataTable] WHERE [Description] like '%example%'" queryout "C:\Temp\MyData.txt" -c -S 10.1.0.18 -d ClientDb -b 1000 -U MyUser -P MyPassword
This statement executes a query against the "ClientDb" database, using the user name "MyUser" and password "MyPassword" on the server 10.1.0.18, executed in batches of 1000. This is then all exported into a file called "MyData.txt".

Import

Import was straight-forward, I just wanted to dump all the data into a local database.

bcp Temp_Table in "C:\Temp\MyData.txt" -c -T -E -S MyServer -d LocalDb -b 100000
This statement imports all the data in the text file to the table "Temp_Table" in the "LocalDb" database, on the server "MyServer" using a trusted connection (-T),

I found this a really easy way of doing an export/import. In my case the source table contained a large amount of binary data, so transferring the data to my local machine took just under an high, at a rate of 0.14 rows per second, resulting in a 375MB table. However, the import was very fast, and even with all that data it only took 35 seconds, averaging around 14 rows per second.
Padding strings in SQL Server 2008 29 January 2013 at 13:12

There are often cases when you need to format a string or number into a string with a specified length. SQL Server 2008 doesn't have an inbuilt "PAD" function, but you can get around that with some String functions.

For example, you want to format an integer value into a 6-digit string, where the integer is of variable length, and any missing digits must be padded with a '0'. In SQL Management Studio, run the following script to generate some test data:

CREATE TABLE #temp (Id int null)

INSERT INTO #temp (id) VALUES (1)
INSERT INTO #temp (id) VALUES (2)
INSERT INTO #temp (id) VALUES (NULL)
INSERT INTO #temp (id) VALUES (5)
INSERT INTO #temp (id) VALUES (47)
INSERT INTO #temp (id) VALUES (123)
INSERT INTO #temp (id) VALUES (54789)
INSERT INTO #temp (id) VALUES (154789)

You can now use a combination of LEFT / RIGHT, LTRIM and REPLICATE to pad your string with leading or trailing characters:

SELECT 
	Id AS OriginalValue,
	(SELECT RIGHT(REPLICATE('0', 6) + LTRIM(Id), 6)) AS PaddedLeft,
	(SELECT LEFT(LTRIM(Id) + REPLICATE('0', 6), 6)) AS PaddedRight
	FROM #temp

Running this will result in the following output:
Results grid

Editing Video Tags with taglib-sharp 24 January 2013 at 20:12

I've been using XBMC at home, and I've been trying to find software to edit Genres on my movie files so I can sort them a little better. To my surprise, I couldn't find anything that seems to be able to edit tags on all the file types that I used. My next step was looking for libraries, and I was pleasantly surprised with taglib-sharp - available at https://github.com/mono/taglib-sharp.

I installed using the Nuget Package Manager in Visual Studio, and it literally took me 2 minutes to figure out a basic example and it worked perfectly on a number of files I tried!

using (TagLib.File tagFile = TagLib.File.Create("D:\\Temp\\101 Dalmatians.avi"))
{
    tagFile.Tag.Genres = new string[] { "Animated", "Family" };
    tagFile.Save();
}

I love software that just works.

Reconciling client-side and server-side lookup values with T4 Templates 11 January 2013 at 09:59

One of the challenges when creating web applications is the separation of logic between the client and the server. Most solutions will have the concept of "lookups" e.g. categories or some type of data that is static but necessary for the business logic. In terms of the server-side code, you will usually reference these using enums or constants, so they are not scattered throughout your solution in code.

However, you often will need to execute logic on the client using these same lookup values. This means you need to declare them again, which can result in the client and the server-side code going out of sync if the lookups change. One nice way to tackle this if you're using Visual Studio is with the use of T4 templates.

Example

You have an "Animal" class declared in your C# project and you want some of the const values exposed on the client.

T4 Template

<#@ template language="C#" #>
<#@ output extension=".js" #>
<#@ assembly name="System.Data" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="C:\Development\MyProject\bin\MyAssembly.dll" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="MyNamespace.Lookups" #>

var Lookups = (function() {
    var pub = {};
 
    pub.Animals = (function() {    
		
	var pub = {};
	pub.Cat = '<#=MyNamespace.Lookups.Animal.Cat#>';
	pub.Dog = '<#=MyNamespace.Lookups.Animal.Dog#>';
	return pub;

    }());
 
    return pub;
}());

Using the Lookups

Once you've saved and run the T4 template, you will then have a .js file to include in your project. You can then write JavaScript as follows to use the lookup values:
alert('Test: ' + Lookups.Animals.Dog);
Visual Studio 2012 : Navigate to Current File in Solution Explorer 07 December 2012 at 11:49

We use Visual Studio 2012 with an older version of Resharper at work. I've been wanting to use Visual Studio 2012, but our version of Resharper isn't compatible, and although I can get by without most of the features, the one I REALLY REALLY REALLY miss is the ability to navigate to the current file in the solution explorer. With the help of an existing extension I've finally managed to resolve that.

First off, download and install the extension mentioned above - you can also get it from within Visual Studio 2012 as per the below screen show:

Extension image

Once you've done that, you will be able to right-click on the current tab in Visual Studio to navigate directly to your file:

Context menu image

The last thing we want to do, is also hook this up to a keyboard command. I like to hook it up to "Alt-Shift-L"; to do this follow these steps:

  • Click the Tools menu
  • Click Customise...
  • Click the Keyboard... button
  • In the "Show commands containing" textbox type "FindInSolutionExplorer" (see screenshot below)
  • Select the "Press shortcut keys" text box, and hit "Shift-Alt-L" (or whatever keyboard combination you want)
  • Click "Assign", and then OK all the dialogs

Assign key image
Android : Sharing development packages 18 July 2012 at 18:15

I was wondering how you can share Android packages that you're developing with other interested parties. Well, it turns out it's fairly easy. Assuming you're working on the emulator, your first task is to extract the package off the emulator.

Make sure the emulator is running i.e. you have executed your application via Eclipse. At the command prompt, you will need to navigate to wherever your android SDKs are installed on your machine, and go into the platform-tools. Here, type in

adb shell
This will allow you to determine the name of your package. Navigate (cd) into /data and then /app and "ls" - this will list the installed packages. Once you have that you can "exit".

The next step is to pull the package off the phone. Assuming your package name is "com.test.apk", issue the following command:

adb pull /data/app/com.test.apk
This will pull the .apk file onto your hard drive.

Next step: make the .apk available onto the internet. I dropped the file into a public Dropbox share. Now all you have to do is share the public link, and anyone with an android device can navigate to the .apk public link using the Android browser. It will download the file - from there all you need to do click it and it will install (or if a version is already installed, the existing version will be overwritten).

Incidentally, you can test this on the emulator itself, although you will need to enable internet access. To do this, in Eclipse:

  1. Window -> Preferences -> Android -> Launch
  2. Default emulator options: -dns-server 8.8.8.8,8.8.4.4

WPF : Checking for Design Mode 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.
BluetoothHeadsetProxy.exe and killing multiple processes in Windows 7 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.