Tuesday 8 July 2008

Mapping errors with nHibernate in Visual Studio

I've started to get into nHibernate recently and while it's pretty cool to use I'm shocked at the lack of info if you get into problems.

A good example - I was working on a project that already had nHibernate setup and running with exisiting integration tests. I added a new object to the domain, created a new mapping file, project built, all good but when I ran my new integration test I kept getting this error:

Class type <YOURNAMESPACE.YOURCLASS> is not defined within your current mapping files

Finally got fed up searching through Google and asked a collegue who pointed out the reason why.
The error generally means the mapping file isn't being found by nHibernate.

Essentially I was being a bit thick and had named the mapping file newclass.xml instead of newclass.hbm.xml ok ok my bads on that one BUT he also told me the xml file needed to be setup as an embedded resource in Visual Studio (Right click the file - properties and set 'Build Action' to 'Embedded Resource').

Lesson learned and just in case anyone else is stumped out there;
check you have named the mapping file correctly (it should be named the same as the domain object), with the right extension (hbm.xml) and you have specified the file is an embedded resource!!!

Wednesday 2 July 2008

ASP.NET ScriptManager causing JavaScript errors

First post for a while and the reason I'm getting this out there is due to the lack of Google results on the subject.

Recently our QA dept raised a bug with 2 javascript errors appearing on certain pages. The errors itself is 'Sys is not defined' which in itself is a really useless error. Just to give this some background, I was using a .NET ScriptManager on a masterpage. Some pages using this masterpage had the JS errors and some didn't.

sys not defined error

Further investigation lead me to believe the errors were caused by the webresource.axd and scriptresource.axd not being rendered in the HTML. I was expecting something like this:
<script src="/WEBSITE/WebResource.axd?d=-LVZ..."
type="text/javascript"></script>

<script src="/WEBSITE/ScriptResource.axd?d=IrhmF..."
type="text/javascript"></script>
After spending a few hours googling the issue I found a few responses suggesting IIS (deselect 'check file exists', yes done that), or web.config entries (again all correct).

Finally I stumbled upon a thread suggesting .NET pages using ScriptManagers can cause issues when not calling the base from any page events - I've since lost the link hence this post!

This is how my code looked with the error:

protected override void OnPreRenderComplete(EventArgs e)
{
base.OnPreRender(e);

[REST OF CODE...]
}

The fix is obvious now - use base.OnPreRenderComplete(e) but for future reference if you experience this issue check all your events are calling base correctly.