Thursday, 30 August 2007

Databind: Object does not match target type

This error sometimes occurs when trying to bind a result from a NHibernate query. It is caused by the first item in the list being a proxy value which causes the DataBind to use reflection of the proxy rather than the real target.

Details and solution taken from Alkampfer's Place.

Possible solution, requires object implements ICloneable:


public IList<T> SafeList<T>(IList<T> input) where T : class, ICloneable {
//Symply check the type of the first element.
if (input.Count > 0) input[0] = (T) input[0].Clone();
return input;
}



If object does not implement IClonable then need to turn off Lazy loading in mapping file.

Final Solution
As it was not pratical to make all objects ICloneable or to turn off lazy loading we finally settled on clearing the cache (Session.Clear) prior to executing queries or criterias.

Wednesday, 29 August 2007

LoginStatus - Login page URL

When using LoginStatus control the Login link goes to the page identified in the web.config:


system.web
authentication mode="Forms"
forms loginurl="~/Default.aspx"

Friday, 15 June 2007

Problems with Properties showing in Property Editor with custom web server controls

I was creating a custom web sever control which was inherited from CompositeControl, I had removed a couple of editor properties, I then noticed that some properties were not being shown in the property editor after you switched between source and design view of a web page. To fix this I had to add in a new property, compile and use it, then take the new property back out again.
I also had to take the reference of the object out of the web page and stick it back in again.
All in all a pain in the bottom, and a waste of time!

Dear MS why did you make the development of web controls so annoying, like having to put a new control on the screen everytime you make a change, and debugging the component in design mode what were you thinking!

Thursday, 7 June 2007

NHibernate:object references an unsaved transient instance

"object references an unsaved transient instance" recieved when performing a save (and transactional commit).
Caused by a related (parent) record not being saved and the entity not set up for cascading save.
e.g. User record has link to Person record but when user is saved the linked person is not saved.
Solution is either to turn on cascading saves for related entity or to explicity save the related record by creating a business object method.

Tuesday, 5 June 2007

Ajax Extender Properties Gotchas

When you want to add a property to an Ajax Extender you need to do the following.

Make sure you have installed the Ajax Extender Templates, run AjaxControlExtender.vsi from the AjaxControlExtender directory of your toolkit install.

Create a new item using the ASP.NET Ajax Extender Control Template from my templates.

Note : If you set the namespace to have a . in it e.g. Company.AjaxControls then the Javascript will not be able to find the script as a webresource. If you want to do this then you have to manually change the ClientScriptResource attribute of the extender.


1) Create the C# Property in the controlExtender.cs file

[ExtenderControlProperty]
[RequiredProperty]
public string ExampleControl
{
get { return GetPropertyValue("ExampleControl", ""); }
set { SetPropertyValue("ExampleControl", value); }
}

2) Create the JavaScript Property in the controlBehaviour.js file
  • In the function(element) section, at the top of the page by the todo 1 to declare the property on the JavaScript side

    this._ExampleControl = null;

  • Add the property getter and setters at the bottom of the file

    get_ExampleControl : function() {
    return this._ExampleControl;
    },

    set_ExampleControl : function(value) {
    this._ExampleControl = value;
    },
Note : The getter and setters are named get_(propertyname) and set_(propertyname). the asp.net Ajax controls expect the names in this form.

Monday, 21 May 2007

NHibernate, GetType on Proxy

When lazy loading is true the returned type from a db call will be a proxy, so if you call GetType() you will get the proxy type not the underlying class.
To overcome this use NHibernateUtil.GetClass(object-instance).

Friday, 11 May 2007

C# Date encoding / parse

DateTime.Parse requires a Culture to be set to use a specific format.
If the format is known then a better alternative is to use new DateTime(year,month,day).