Posts

Using the Razor Engine to create PDFs

I've been using MVC 3 and the Razor syntax for a while now, and a while ago whilst reading how people were using Razor for their email templates I had the idea to use Razor to help create PDF templates. Now sadly it's been a while since I played around with this but I have only just started getting back on top of things so here's how I've done it. Components Used My solution is to write HTML templates using the Razor syntax and then to make use of the Razor View Engine for filling in the dynamic data. Then for the PDF creation to make use of iTextSharp library to generating the PDF documents from the generated HTML. iTextSharp is a free library that allows you to create PDF's using C#, unfortunatley its API is a bit of a pain to use natively so I have made use of Hugo Bonacci's HtmlToPdfBuilder.cs class to simply my interaction with the library. The first thing to do is to generate the HTML markup for out PDF. So fire up a new RazorViewEngine and load the v...

Using DeferredLoadListBox in a Pivot Control

Recently I've been using the DeferredLoadListBox It's a fantastic way of improving the performance of your list views. I started using the DeferredLoadListBox within a Pivot control but occasionally found that the app would randomly crash throwing the following exception: All containers must have a Height set (ex: via ItemContainerStyle), though the heights need not all need to be the same. This usually means you haven't set the properly. However I had ensured I had set the style height. I then got the source and started poking around. What I found was although in the UnmaskItemContent method the container had a height if you inspected the ActualHeight property this was 0. I did a bit of research and found the following on MSDN: ActualWidth and ActualHeight are calculated based on the Width/Height property values and the layout system. There is no guarantee as to when these values will be "calculated" So the problem appeared to be with timing. After contactin...

Windows Phone 7: Security Exception on deactivate

This morning I have been finishing the tombstoning part of one of my Windows Phone applications I have been developing. Whilst testing I found that when the application deactivated or terminated that a SecurityException was thrown. Looking at the stack trace I noticed that it was occurring whilst trying to serialise some data to the Applications State store. Now I knew I was putting some data into state so this wasn't to hard to find however, when I looked at what I was putting into state it was nothing more complicated than a custom type that exposed a collection of POCO classes. Why would this cause a security exception. I decided to do a simple Google search for the exception "windows phone 7 security exception" and the second result looked similar: "c# - SecurityException was unhandled when using isolated storage" [ http://stackoverflow.com/questions/4209280/securityexception-was-unhandled-when-using-isolated-storage ]. So I had a look, and guessed that if y...

Windows Phone Development and Designer Exceptions

I've been building a few Windows Phone applications recently and have been learning lot's along the way and am hopefully releasing a couple of these in the near future. However on my current project I have been constantly hounded by the designer view of my xaml pages throwing exceptions. Tonight I decided to finally look into why these occur, I have managed to ignore them previously due to the weird nature of them. My exception scenario consists of a page that contains a pivot control and inside one of the pivot items there is a user control. The user control contains a simple list view which has some data bound to it Now I have always got past the exceptions as if you use the designer view for the user control it works fine, however as soon as I put it into a page or a pivot the designer starts kicking off. The actual exception is as follows: Could not load type 'System.Net.HttpUtility' from assembly 'System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToke...

MVC 3 RC2 Install Error (0x80070643)

This evening I came to installing MVC3 RC2 on both my desktop and laptop. The laptop installed first time, however I found that on my desktop it refused to install. Citing the following error in its install log: Installation failed with error code: (0x80070643) After poking around in the log file I found that the VS10-KB2465236-x86.exe patch was where it was falling over. To try and get more information I found the temp directory with this file in and ran the installer manually. This also as expected errored, however this time when I looked in the log file I found a slightly different error: Install failed on product (Microsoft Visual Studio 2010 Express for Windows Phone - ENU) The next obvious place for me to go next was to uninstall the Windows Phone Developer Tools, I figured I can always reinstall them. Upon removing this I then tried to install MVC3 RC2 again. This time it still failed, with the same error I had previously but this time for Visual Studio 2010. As a final shot I w...

MVC DateTime Suffix HTMLHelper

Recently I have been working on an MVC Project, and tonight I got to the point where I wanted to output a date in a specific format, for example Tuesday 13th April 2010. Sadly DateTime formatting still doesn't allow you to specify output a suffix, you can do Tuesday 13 April 2010 but not what I wanted. I decided that I could achieve what I wanted by writing a quick HTMLHelper, this would then allow me to specify a datetime format string with a magic / special character which I could then replace for the appropriate suffix. Writing the helper was very quick and easy, if you want to learn about HTMLHelpers and how to write your own I recommend looking at Stephen Walther's Post on HTMLHelpers . The code for my datetimehelper is below: using System; using System.Web.Mvc; namespace mjjames.MVCHelpers { public static class DateTimeExtensions { public static string DateTimeFormat(this HtmlHelper helper, string dateTimeFormat, DateTime dateTime){ var dateTimeOutput = ...

Using DOTRas - An Overview and some things I've learnt

Yesterday I decided to starting knocking together a quick application to help me backup my server to some local storage. The idea being that at any point I have a local copy of my server setup a maximum of a day old. The point of this application and how I've gone about writing it, what libraries I'm using etc will be part of a future blog post. I decided early on that I however I wanted to transfer files I wanted to do this over a VPN to the server. I had several reasons for this, being able to expose my files over a network share, more secure etc. My Application will be running on an old laptop, so I first thought about just always having it connected to a VPN using windows, and run the application as normal. However I then thought what if the VPN disconnects and I don't notice, how long would it take until I noticed etc. So I decided to make the application create a VPN Connection at start up and then disconnect from it upon completion. I figured that there would be a go...