Monday, 24 September 2007

NHibernate Criteria aggregate (Sum,Max etc)

It is possible to get NHibernate criteria to perform aggregations using the Projections class:

 


public double SumHours(out double chargeable, out double nonchargeable)
        {
            IList results;
            ICriteria crit = _session.CreateCriteria(typeof(Activity));
            ProjectionList projList = Projections.ProjectionList();
            projList.Add(Projections.Sum("ChargeableHours"));
            projList.Add(Projections.Sum("NonChargeableHours"));

            crit.SetProjection(projList);
            results=crit.List();
            if (results.Count == 0)
            {
                chargeable = 0;
                nonchargeable = 0;
            }
            else
            {
                chargeable = (double)(results[0] as object[])[0];
                nonchargeable = (double)(results[0] as object[])[1];
            }

            return chargeable + nonchargeable;
        }


Thursday, 13 September 2007

FileUpload not working ~ no state information on postback

Had a problem where the 'selected file' information was not coming through for a dynamically created FileUpload control. The problem was due to the FileUpload control being inluded on a Ajax UpdatePanel.

FileUpload control will not work with an async postback (Ajax UpdatePanel) because the details of the file to upload are included in the Web Request of a full postback.

See Eilons Liptons Blog for more details.

Solution was to have a dedicated upload page.

Thursday, 6 September 2007

Instantiating arrays

Always seem to forget the syntax for instantiating arrays in code:

int [] numbers = new int[] {3,1,4};
string [] names = new string[] {"Tom", "Dick", "Harry"};

Monday, 3 September 2007

Validation of viewstate MAC failed... using AJAX UpdatePanel

Was getting the following error after a partial update of a component on an AJAX UpdatePanel:

Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster

It seems the problem occurs because after the partial update the component include a GridView that used DataKeyNames. The use of DataKeyNames requires that view state is encrypted.
I think the error occurred because this encryption was only taking place after the partial postback.

The solution seems to be to always have view state encrypted ~ a page directive.


viewStateEncryptionMode ="Always"

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!