Thursday 22 November 2012

Create a text area for a spreadsheet using NPOI

I've been doing a lot of work lately for a client that requires the exporting of large amounts of data into Excel spreadsheet format. After a fair bit of research into the more flexible .NET-based packages available, I decided on NPOI.

Most of the questions on NPOI are answerable from research, but here's one I found tricky to find. Creating a proper text box on a spreadsheet that you can add formatting to, and include massive amounts of text in.

To create a textbox, do the following.

Create a patriarch object

This is your base to create any drawing object. I created mine as a member at the top of the class so that I can re-use it class-wide as I please.

Create an anchor point

This is the area that your textbox will sit on. You need to change startingRowNum and endRowNum to be the start and end rows (height) of your textbox. The width is determined by startingColNum and endColNum.

Create the textbox, and put something in it.

You have to create a rich text string to put in there, but on the plus side, that gives you the opportunity to play with formatting and so forth.

I found this particular bit of functionality to be quite well hidden in the documentation, so if you take this any further or do anything interesting, please feel free to comment and let me know!

Thursday 8 November 2012

"Validation failed for one or more entities" == Vague!

Sometimes when you're debugging exceptions around Entity Framework, the exceptions can be rather unhelpful. I've encountered this error on many an occasion:

"Validation failed for one or more entities. See 'EntityValidationErrors' property for more details."

Thanks to this post on Stackoverflow, I now know how to get at that EntityValidationErrors property that was unusable from the quick watch window.

In my case, I wrapped the update command throwing the exception in a try / catch block, and casted it as follows:

This then let me view exactly which field was causing the problem, and I then adjusted my validation to suit!