Wednesday, 13 February 2013

Get a list of countries for a dropdown

This is a surprisingly difficult question to answer, as it's not all as simple as just adding the System.Globalization namespace and getting stuck in.

It appears that there are in fact a large amount of incorrect / incomplete answers on the subject.

There is a web service that you could add and call which may prove to be more up-to-date than the .NET framework itself, but the easiest, fastest and WORKING method of adding countries to a drop-down is to use the method mentioned in this StackOverflow question:

It's a life-saver!

Thursday, 3 January 2013

You get "Maximum request length exceeded" when you upload big files

I came across this issue in an MVC 3 project I'm working on at the moment, where when the user tried to upload files greater than 4mb in size, they were getting this exception.
If you're using IIS 7, a lot of answers online seem to point to increasing the maxAllowedContentLength attribute for the requestLimits element in the web.config.
Others also say if you're using IIS 6, you need to increase the maxRequestLength attribute for the httpRuntime element, also in the web.config.
I had to do both.
Within <system.web>, add:

And within <system.webServer>, add:
IMPORTANT:

Both of these values must match. In this case, my max upload is 1024 megabytes.

maxRequestLength has 1048576 KILOBYTES, and maxAllowedContentLength has 1073741824 BYTES.

It's quite easy to overlook this difference in requirements for each attribute, and mismatching these values can cause other errors.
I also answered this question on StackOverflow.

Monday, 10 December 2012

Mercurial vs Git

Most posts about distributed version control are written by people who are biased to one particular solution. This is usually because that person has a wealth of experience in one system, and not so much experience in the other.
This post will be no different.

Prerequisite

I'm biased to windows. I know this. Mac and Linux users live in a different world to your average .NET developer, and that's always going to influence how I view these products.

Git's GUI Offering

Git offers Git for Windows. It's sub-standard, you can't see a branch map, it crashes and falls out of sync with the command window, and you can't do a fraction of the things in the GUI that you can do on the command line. This includes merging and removing branches.
I understand that the makers of Git are trying to make their product more palatable to Windows users, but in my humble opinion, if they are going to try and offer a GUI to do this, bin Git for Windows, and make better use of third-party components.

Third-party components

Mercurial actively offers TortoiseHG as a compliment, and its benefits are huge. You're able to create, merge and close branches, push, pull and contort Mercurial to your whim with a few clicks of a mouse. For Git, TortoiseGit is on its way, but it needs to be pushed harder as a package, because public ignorance of it as a product choice is high. Proof of this is the fact that there are still people trying to find ways to allow TortoiseHG to work with Git repositories!

Conclusion

I know that lovers of the command line will always prefer a black screen with coloured writing to provide them with text-based representations of their source control. I'm also very well aware of how powerful both products are in their purest CLI-based form. However, in three weeks of using a combination of the Git for Windows GUI and the command line interface (not to mention a LOT of Googling and documentation-reading!), I still couldn't do a fraction of what I learnt to do using TortoiseHG and Mercurial in the space of a week.
Productivity must not be made to suffer to be one of the cool kids.

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!

Thursday, 25 October 2012

Problems with sending email over SMTP in .NET?

I occasionally have problems sending email to my own local SMTP server, and get pretty paranoid that your recipient might actually get a test email that they're not actually supposed to get.

You know you haven't set SMTP up properly on a .NET website when you get an error like this: "Failure sending mail" or "No connection could be made because the target machine actively refused it 127.0.0.1:25"

My advice? Don't bother setting SMTP up on your local box. What's the point when you can intercept all outgoing email and view what you're going to send (and to whom) with a little application that sits in your system tray!

Enter SMTP4Dev. I'm going to sound like a JML advert, or possibly sound like I work for them (I don't), but I can't recommend this little application enough. It requires no config whatsoever provided that you're going to use port 25 for SMTP, and it's free!

That URL again: SMTP4Dev.

Friday, 19 October 2012

Elmah won't work in .NET MVC 3 won't work with CustomErrors

I've been doing a lot of work on an MVC 3 application recently, and it's already got Elmah installed ready to report all sorts of errors to help you debug your application.

Except it doesn't.

When the <customerrors /> element of the web.config is set to "On", a friendly error message appears when a user experiences an issue, but Elmah doesn't capture the error. This is because ASP.NET swallows the exception, and this method in the global.asax.cs is responsible:

This line ensures that the "handle error" attribute is attached to all controllers, and the exception's lost.

I found a really nice neat solution on a StackOverflow question, but it's a long way down the page, so I'm recording it here for easier reference.

Create a class that looks like this:

And reference it in the global.asax.cs file ahead of the other attribute like this:

The various answers and a full discussion of the problem are available at this StackOverflow question, but it's quite a long way down the page. It's really worth a read though.