Monday, 26 November 2007

Databinding NHibernate

Had a few problems data-binding lists returned from NHibernate (queries or collections) where the first object in the list was a proxy. We previously 'fixed' this by clearing the session prior to running the query. This then caused further problems as objects were detached and the lazy-lists threw exceptions.

The solution we came to is based on : This posting by Ayende

A TypeDescriptor was created for EntityBase (all our objects returned from NHibernate inherit from EntityBase) which was registered with the web-site in the Global.asax Application_StartUp.

Thursday, 22 November 2007

Configuration Error: FairmortRoleProvider

When creating a new site we kept getting spurious errors which seemed to relate to "add name=FairmortRoleProvider...." line of Web.Config.

This was a red herring and the actual error related to Spring Config files and references.  The Salecore assemblies that are to be referenced must be added as a reference AND the relevant config files also added. Either missing reference or config file can cause this spurious error.

Thursday, 25 October 2007

VS2005 not rebuilding project

Had an issue with a specific project not being rebuilt when changes made to it and web site being run. The solution contained multiple projects and none of the other projects exhibited this behavior.

 

Appears due to Project being marked as not rebuild for Solution, although this setting was not turned on manually.

Setting access via: Solution (right-click) Properties -> Configuration.

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"