Wednesday, 28 March 2007

GridView : Getting updated cell values

When using a gridview the RowUpdating event arguments has a property (e.)NewValues.
There is a problem if the GridView datasource is not set to s DataSourceObject, these NewValues remain blank.
To populate use the following code:
GridView gv = (GridView)sender;
for (int i = 0; i < gridView.Columns.Count; i++)
{
DataControlFieldCell cell = gv.Rows[e.RowIndex].Cells[i] as DataControlFieldCell;
gv.Columns[i].ExtractValuesFromCell(e.NewValues, cell, DataControlRowState.Edit, true);
}

Monday, 26 March 2007

Nhibernate: Date time field null on insert

Receiving exception when trying to save a record the stack tarce identified theproblem as a null value for a DateTime field.
This is caused by either:
  1. Property not marked as nullable (? after Type in property declaration)
  2. Existing record being flushed that contains an invalid null value for datetime.

NHibernate: Object not set to instance on save

When saving I received 'Object not set to instance', from the stack trace the object in question was identified as the VerisionNo.
This was correctly defined for the table in question, after investigation it was identifeid as being a link table that was being flushed which had an existing record with a Null value for the VersionNo.
This record had been either manually entered or entered prior to the versionNo field being defined as NotNull.

Friday, 23 March 2007

RowDataBound checking if row is in edit mode

The RowState property of a grid row can hold more than one value using bitwise logic.
If you want to test if a row is in edit mode then use:

if ((e.Row.RowState & DataControlRowState.Edit) > 0)

The RowState will also have a value of DataControlRowState.Alternate for every other row.

Thursday, 22 March 2007

GridView : DIsplaying results of function using DataItem as parameter

Instead of just displaying the actual value of a field in a column (<%# Bind("Hours") %>) you can also call a function that uses the value to get another value.
e.g.
If we have ClientID but we want to display ClientName (another tabel in the db) we can assign:
Text='<%# GetClientName(DataBinder.Eval(Container.DataItem, "ClientID")) %>'

We then have a function in the page code:

protected string GetClientName(object iD)
{
return GetClientNameById(iD);
}

Note. the parameter must be defined as object because the actual type is unknown until binding.

Spring: Error instantiating context spring.root

Had the exception "Error instantiating context spring.root" when attempting to create an object.
This was caused by not including the assembly reference that the object belonged to in the web site.

Tuesday, 13 March 2007

Updatepanel : Making other panels visble/invisible

Had a problem making multiple panels visible / invisible inside an UpdatePanel. The problem was occurring when I wanted to display different panels based on wether a user was logged in or not.
As an example I created the following user control with two panels which should alternatively be displayed.
When the Top button is clicked the code was storing in a session object that the user is logged in.When the Top button is clicked the code was storing in a session object that the user is logged out. During the Render method of the contorl if the seesion object was set to Logged In then the bottom panel was set visible=true and the top panel set to visible=false (and vice versa when logged out)
This worked, until another UpdatePanel on the page performed a partial postback, then both panels were being displayed.

Assumption of what was happening: The problem seems to be that the setting of the visible=true|false was being made in the wrong place. Setting the values in the Render method mean that they are not then stored on the control state; when a different updatepanel performs a partial postback it appears the control state is used to identify what elements of the page need updating. In the above case both panels has visible=true set in the control state.
Fix: To fix the problem the setting of the panel visibilty was moved to the PreRender method (after PreRender, SaveState is called, the Render).