Setting MaxValue for Migrations

12 03 2013

I’ve just been using the MigratorDotNet framework for the first time. I was creating a migration that would create a new table with various new fields. One of the fields I wanted to create was to store files so therefore I wanted a varbinary(max) field. If you leave the size undeclared, it will create a varbinary(8000). The way I got around this was to create my column as follows:

Column file = new Column("BinaryFile", System.Data.DbType.Binary, int.MaxValue, ColumnProperty.NotNull);

Using the int.MaxValue gives the same result as varbinary(max). However, this doesn’t create a varbinary(max) field (at least not in SQL Server 2008 R2 which is what I am using). It creates an image field instead. This isn’t necessarily a problem, it still gives me the space I require. Importantly though, Microsoft are now advising you do not use this type in new development as it will be removed in a future version of SQL Server (see MSDN article here). We’ve weighed this up and have decided that this is not a problem for us at this time because we are developing improvements to a current system with no plans for any SQL upgrade in the future. There are also uses of the image type in other parts of the database. We have instead logged it in our technical backlog to go through and change all uses of this type at a later date. Personally, in a new project, I would not use it, just in case!

I did some searching in the hope I would find a more elegant solution (something akin to the EF Code First data annotations would have been nice) but I didn’t find anything. If anybody comes across this post, and you’ve found a nicer way to do this, please comment on here to let me know!

Advertisements




Sorting UK Postcodes with C#

19 11 2012

Ok, before you say anything, yes I know. It has been a long time since I posted anything remotely useful here. Well in my last job I ended up doing less development and more team management so the useful bits I came across were few and far between. In my new job I am back to development, so I should have more to post here in the future.

One of the requirements I had recently was to sort UK postcodes in ascending order. The postcodes were being stored in a list of type IEnumerable and were all strings. As you are probably aware if you are searching for this, UK postcodes are alphanumeric (NP10, NP11 etc). This means that if you try and sort the following list of postcodes: NP1, NP23, NP35, NP3, NP2, NP45, NP11

You would get the outcome: NP1, NP11, NP2, NP23, NP3, NP35, NP45

However the outcome required is: NP1, NP2, NP3, NP11, NP23, NP35, NP45

I found this brilliant blog post which provides some code under the MIT license which does the job perfectly – Natural Sorting in C#

I included his EnumerableComparer class as a Utility class in my Framework and then used the code in a similar way to this:

IEnumerable<PostcodeAllocation> allocations = GetAllAllocations();
// Declare convert string as per IanG's blog post
Func<string, object> convert = str =>
{
try
{
return int.Parse(str);
}
catch
{
return str;
}
};
allocations.OrderBy(str => Regex.Split(str.Postcode.Replace(" ", ""), "([0-9]+)").Select(convert), new EnumerableComparer<object>());

This then sorts my data as follows: NP1, NP2, NP3, NP11, NP23, NP35, NP45.

Perfect! I hope this helps someone else and a huge thanks to IanG on his blog for this great snippet of code.





How To Enable Integrated Windows Authentication in IIS 7

16 06 2010

I’m currently working on a website that was created in .NET 1.1 and Visual Studio 2003 and I had nothing but nightmares getting it to work on my Vista machine which is running IIS7! My final hurdle was getting it to run in Debug from Visual Studio 2003. To do this I needed to enable integrated Windows authentication for the website. So here is how I did it:

1 – Open IIS Manager

2 – Highlight your website that you want to enable integrated Windows authentication for

iis7

3 – Double-click the Authentication tab

4 – Where it says “Windows Authentication” in the Name column, the status column should read “Enabled”. If it reads disabled, right click and select “Enable”.

iis72

This did it for me and I was then able to go back into my Visual Studio 2003 environment and successfully run the website in debug mode. REMEMBER!!! Visual Studio 2003 might need to be run as an administrator to allow debugging (Right Click — Run as administrator) so if you are still having problems give that a go!





Creating PDF files using C#

12 02 2010

Being able to create PDF files from C# code is something I find incredibly useful. It’s taken me a while to find a good (free!) tool, but I’ve definitely found it. It’s a tool called MigraDoc which also comes with the renderer PDFSharp. They’re both fantastic open source .NET libraries.

The commands and way it works take a little getting used to but it’s all pretty straight forward. They have a Wiki that has loads of code examples which provides great knowledge to learn how to use the tool, but is also great if you get a bit stuck on something. If you can’t find an answer on the Wiki there’s also a forum. There’s lots of answered questions on there which has provided most of my answers to problems and the one time I couldn’t find an answer and posted, I had a reply from one of the developers within the hour.

So enough of the babbling, here’s a link to the site. Go forth and create PDF’s!

http://www.pdfsharp.net/MainPage.ashx

I may even post some code snippets using MigraDoc sometime in the near future.





String Formatting in C#

5 02 2010

I don’t have much to say in this post really. It’s all in the title and I’m going to point you to this brilliant post on SteveX Compiled. Whenever I need to check up on a string format, this is the place I go! 🙂

http://blog.stevex.net/string-formatting-in-csharp/





Permissions issues with DLL’s stored on network shared drives

29 06 2009

One of the main things I am currently working on is a windows service that watches for files, and when one is found uses the information in that file, to call a DLL that is stored on a different machine on the same domain on a network shared drive, to do the magic with the information in the file (we will say in this case the DLL was held on MachineA and the service was running on MachineB).

I was having a continuous problem with the following exception:

“Request for the permission of type ‘System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’ failed.”

It took a lot of surfing the internet and forums, and a lot of book reading to finally find a solution that worked for us.

Due to the DLL being held on a different machine (MachineA), MachineB would not allow it to do anything outside of it’s own scope. For example, it has no access to the file system. Using the caspol utility (see here for information) I added a new url with full trust to the internet group on MachineB using the following command text:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\caspol.exe -addgroup 1.3 -url file:\\ipAddress/folder/* FullTrust

After doing this I restarted my service, and it worked! The nasty security permissions exception went away and it worked as expected. Yey!





Regular Expression for matching dd/mm/yyyy format

10 06 2009

(0[1-9]|[12][0-9]|3[01])[/](0[1-9]|1[012])[/](19|20)[0-9]{2}

I’m not going to try and explain this regular expression as it is very clever and has been only very slightly modified from the one found here http://www.regular-expressions.info/regexbuddy/dateddmmyyyy.html

I have limited mine slightly more to only being allowed to use slashes and changed the end very slightly (although it means the same thing) and that site shows an excellent explanation. I simply want to post this here as I know I will want to use it in the future and I’m less likely to lose a blog post than a post it note 🙂