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"