Friday, February 26, 2010

Good Exception Rules - By Hanselman

Just wanted to stick this up as it's a good line of thought for handling exceptions.
There are, of course, many ideas on this, but 'from the horses mouth' often works well :)

Thursday, February 25, 2010

Sync Google Contacts To Your iPhone Contacts

Ok, this is fairly straightforward if you've already set up your Mail/etc on your iPhone to use Google Mail as an Exchange account.

If not, you've got some backing up to do, but it's ok - this blog post documents the whole process and makes it fairly painless:
One warning it makes: You will lose all your contacts and calendar entries if you simply sync with Google with importing your existing contacts into Google first!

Tuesday, February 23, 2010

Shared Heap Exhausted Or Damaged

I recently had an issue which caused a lot of error messages in the Control Panel -> Admin Tools -> Event Viewer -> Application log. While I've not fixed this issue on my machine specifically, I thought this was worth recording. The error I'm getting is:

The description for Event ID ( 1 ) in Source ( nview_info ) cannot be found. The local computer may not have the necessary registry information or message DLL files to display messages from a remote computer. You may be able to use the /AUXSOURCE= flag to retrieve this description; see Help and Support for details. The following information is part of the event: NVIEW : devenv: shared heap exhausted or damaged

Some may find the following link useful in this situation.

Sunday, February 21, 2010

The Project Is Not Supported By This Installation

Today I got this error dialog when trying to open a solution "The project file '....csproj' cannot be opened. The project type is not supported by this installation." Visual Studio went on to display all the projects in the solution it could, except the website project.

Googling this returned this helpful post:
Indeed, reading the 5th post on that thread lead me to deleting the ProjectTypeGuids element from within the first PropertyGroup element. Hitting Reload Project in Solution Explorer showed the project correctly.

Friday, February 19, 2010

Different Build Modes

It is often useful to have different Visual Studio build modes in order to make better/more appropriate compilations of your projects for different environments, such as Development (your local dev machine), Staging (internal/client testing) and Production (final, fully live environment.)

To this end setting up VS properly is important and actually fairly simple. The process I use is thus:
  • Create a \Configuration directory in the root of your web project
  • Create \[build mode] directories for each build mode inside the \Configuration directory
  • Open the web project properties panel, click "Build Events" and paste this into the first multi-line text box, titled "Pre-build event command line":
mkdir $(ProjectDir)Configuration\Current
copy $(ProjectDir)Configuration\$(ConfigurationName)\*.config $(ProjectDir)Configuration\Current
  • Create empty ConnectionStrings.config and AppSettings.config files within each of \Configuration\[build mode] directories. These will need the initial empty elements for their appropriate config blocks, such as and
  • Replace the above config blocks in your web.config file with element attributes pointing to the \Configuration\Current directory:
  • Open the Solution Configurations drop down and select "Configuration Manager..." This is on the Standard toolbar and will probably show "Debug" as the default item.
  • Create the appropriate build modes under "Active solution configuration". Select New... and just create a series of new configuration names, such as Development, Staging and Production. Copy the default settings from Debug, for ease of use.
  • Using the Active solution configuration drop down, select each of the configuration modes you've just created and for each one click New... under the "Configuration" column for each project in your solution. When the New dialog opens, just create the configuration with the same name as the active solution's configuration: Development under Development and so on. Again, copy the Debug project configuration, for ease of use.
  • After closing the configuration dialog, select a configuration mode from the drop down and hit Build -> Build Solution.
At this point you may discover the following exception when building:
The command "mkdir .........." exited with code n.

This happens because one of the files you're asking copy to work upon either doesn't exist, doesn't have permissions to access or the directory your command refers to simply isn't there.

Problem: The directory will be missing if your Visual Studio build mode is currently in, for example, Release and you've only got \Configuration directories for, eg, Development and Staging. The problem copy is hitting is that the \Release directory doesn't exist.

Solution: Change the current build mode to Staging or Development and all should be well.

Wednesday, February 10, 2010

Not Validating A Complete Model

I am currently trying to edit a single model object through a series of views, using the MVC 2 RC 2 framework, found here:
I discovered that while using the default server-side validation that my model could not be properly validated because some of the model properties were not fully populated, or had incorrect values when, for example, the first view's form was submitted. The incorrect/null/etc values were fine for me because the process had not reached the point they would be populated, but the ModelState.IsValid still returned false, of course. This would be fine, except I needed it to be true, fitting to my standards of what was 'true' about the state of 'my model'.

The method I came up with follows, but I will say that it's not perfect and will almost certainly be superseded by something better in the future. Here it is:

///
/// Used simply to ensure that validation has occured for the model properties that were rendered on
/// the current view.
///
/// The list of form field names to check for errors. Eg: the FormCollection object
/// True if none of the ModelState values matching the whitelist keys have errors attached, else false.
private bool IsModelStateFieldsValidated(IEnumerable whiteList)
{
foreach (string key in whiteList)
if (ModelState[key].Errors.Count > 0)
return false;

return true;
}

If the action method takes a 'FormCollection collection' parameter then that can simply be passed into the IsModelStateFieldsValidated method directly.

The method above just very simply checks the field names which were on the page (which, of course, may not be every property in the model class) and checks that there were no exceptions found in the ModelState value collection - for those property names only. Any other exceptions will be ignored, because they are not pertinent to the current view.

Another Problem Solved By Someone Better Than Me

Ok, this is one of those woods-for-the-trees situations. Basically, I had been mucking about with custom model binders:
And had added a class attribute to my model class to handle the post back binding of the model. This worked fine. The following, however, I forgot about this and was trying to use the default server-side validation.

Problem: The server-side validation was not working.

Solution: Remove the custom model binder.

Reason: It was returning just an empty model object every time.

This solved a host of problems, in fact.

The EditorTemplates Directory

Having been trying to get a model property to render the appropriate editor in MVC, recently, I was having some trouble getting the editor - provided by a very talented colleague - to render correctly, or, in fact, at all.

Not having been introduced to the structure of the shared directory (it doesn't seem to be mentioned in the http://www.asp.net/learn/mvc/ pages) there was an issue with understanding how the Html.EditorFor method would provide my custom editor.

This was solved when I came across David Hayden's blog page:

Monday, February 08, 2010

MVC's HtmlAttributes Parameter

In MVC's model it is very easy to generate HTML tags, or XHTML elements, as I believe they prefer to be known these days.

What wasn't so obvious was the optional htmlAttributes parameter, available in all the Html.TextBoxFor(...) Html.LabelFor(...) etc etc.

My question was, "How do I use this, what's it for, why, when, where, who, whether, whence, etc?"

Yeah, I'm not so great at forming questions sometimes. But the answer came in the form of Rob Conery's blog entry:
And so I've learnt that you can simply have disabled="disabled" in a new {} and the ...For adds the values as attributes to your HTML element. Read Rob's article for a better description.

However, if you're still having trouble with this (eg: ActionLink doesn't seem to link certain htmlAttributes assignments) you can check out this post at stackoverflow:
Top notch.

Yes, I need to get the code embedding sorted on this 'ere blog thingy...

Friday, February 05, 2010

Learning More About Linq To Xml

Being armpit-deep in the slightly clearer .NET waters of MVC 2 RC 2 has lead me to getting further into the (shark infested?) LINQ deeps. Therefore, when I went looking for some good "how to sort XML using LINQ" on google, I came across this:

Finding Out More About MVC

[EDIT] Ok, irony of ironies... Literally AS I was writing this, the MVC team at MS were putting out their latest version: MVC 2 RC 2. You can get it here (and yes, it fixes the problem I've described below): http://weblogs.asp.net/scottgu/archive/2010/02/05/asp-net-mvc-2-release-candidate-2-now-available.aspx


So, after some working with MVC 2 RC (Full title: ASP.NET Model-View-Controller Version 2, release candidate, or so I'm told ;) I discovered some lovely things about MVC. One of which, in particular, is the TryUpdateModel, or UpdateModel, method available in the Controller class. This is supposed to take a FormCollection from your Edit (et al) Action method (ie: response from a postback) and populate your model instance for you. It should do this by using an optional whitelist and blacklist of class property names and the names of the fields on your View's form, then by using reflection to assign those properties values.

You'll notice, of course, that the above tends towards the opportunity rather than the specific. This is because the only output from the TryUpdateModel I've had is an error stating "Value cannot be null or empty. Parameter name 'name'." Woo hoo.

Anyway, fortunately there's lots of help on this subject, below... and hopefully my own solution, later...
And so, in the end, I've had my first look at downloading the source for an open MS framework and there are lots of things to be learnt in reading other organisation's code. It's an interesting project and doesn't have to be long and painful. Just grabbing a download and browsing should throw up an idea or two and hopefully show the right or wrong way of approaching things. Or, at the very least, a different way.

Also, I just wanted to point out that these are a great pair of posts: