Category: C#

Getting access to and using Visual Studio code snippets with Resharper installed

When you first install Resharper, your Visual Studio code snippets are imported into Resharpers ‘Live Templates’. This is all very well until you install, update or create new Visual Studio code snippets.

If you have ever wondered how to use the Visual Studio code snippets when you have Resharper installed then here is the quick solution.

Press CTRL+J and then start typing the code snippet shortcut you are after. You will now see the list of available Visual Studio code snippets.

You can find this mentioned here

Entity Framework – Setting integer keys client side gets ‘Cannot insert the value NULL’

I have just been banging my head against a wall trying to get a simple database record added to a simple table using Entity Framework! My table has a primary key field of type INT, but is NOT set as an identity field (i.e. Not set to increment SQL server side). I wanted the ability to set the Id in code.

An example would be a model like:

public Contact
    public int Id { get; set; }
    public string Name { get; set; }

then you would normally expect the standard EF code like this to work

var myNewContact = new Contact
    Id = 123,
    Name = "John Smith"

However it always came back with the a DbUpdateException stating

“Cannot insert the value NULL into column ‘Id’, table ‘MyDatabase.dbo.Contacts’; column does not allow nulls. INSERT fails.”

It turns out that Entity Framework assumes that an integer [Key] field will always be generated server side. Maybe I should have known this, but actually found it very hard to locate this bit of information.

Once you know this the fix is very straight forward. You just need to decorate your model object with

[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]

so the model above becomes:

public Contact
    [Key, DatabaseGenerated( DatabaseGeneratedOption.None )]
    public int Id { get; set; }
    public string Name { get; set; }

I hope this saves someone wasting as much time and loosing as much hair as I did on this simple issue!

One reason it took so long, is that I can’t get Entity Framework Profiler working with the Azure Emulator. If any has had any success with this, please let me know.

HTML Placeholder not working in IE9 and earlier!!

Recently found this very useful jQuery plugin for fixing placeholder issues in old IE browsers. It seems to cover most scenarios and is very easy to  implement 🙂

Just add the script after the jQuery and call

$('input, textarea').placeholder();

You will also need to add the placeholder style in your css.

/* Placeholder fix for IE9 for use with jQuery.placeholder plugin */
 .placeholder { color: #aaa; }

If you are a c# razor person, something like this on your impacted pages will do the trick (assuming you something like

@RenderSection("MyJavaScript", false)

in your layout).

@section MyJavaScript
 <script src="@Url.Content("~/Scripts/jquery.placeholder/jquery.placeholder.js")" type="text/javascript"></script>
 $('input, textarea').placeholder();

How to make a short URL friendly GUID in C#

Been trying my own hack to make a short GUID for a web form submission, but hit issues with incompatible characters. Just before making more changes I came across this excellent blog and code to easily convert GUIDs to Short GUIDs and back.

I have found this especially useful when sending unique vendor transaction refs to the SagePay forms service as they have a 40 character limit.

.Net MVC 3 Logon does not redirect out the box–quick fix!

It seems that the templates for MVC 3 razor are not quite complete. If you attempt to browse to a page where authentication is required, then you will be redirect to the Logon page with a ReturnUrl query string.

Something like:

That’s great except, out the box, the Logon page does not make use of this and the destination page is not redirected to after a successful login. The fix is easy.

Add the returnUrl route value into the Html.BeginForm method as shown below.

Html.BeginForm("LogOn", "Account", new { returnUrl = Request.QueryString["ReturnUrl"] })

You should already have some code in the controller that does some sanity checking on the returnUrl before doing the redirect. Somthing like:

if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
       && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
    return Redirect(returnUrl);

Make sure this is after the SetAuthCookie line!

How to get ‘Create Unit Test’ shortcut back in Visual Studio 2013

Why the hell Microsoft thought it was a good idea to remove this I have no idea. If they want good reliable code on their platforms you would have thought this would be good to leave in to encourage developers to create unit tests!

Anyway, rant over, this blog explains all the options. The last one being the first one to try!