Tuesday 27 February 2007

HQL Query - as, in errors

Tried to do a simple query:
from Page page

returned error ..expected in...

Tried
from Page as page

return error ..unexpected token:as..

After an hour of frustration realised that the object in C# was not called Page (I had renamed this to SalecorePage as it classed with the asp.net class:page).

The use of the wrong name stemmed from performing the query in SQL Server then porting to HQL.

Configuration - unknown class

Tripped me up a couple of times, the System.Configuration must be manually added as a reference to a project otherwise the Configuration class is unknown.
For some reason I always assume this reference is added by default then spend a while wondering why VS claims Configuration class is not part of System.Configuration!

Monday 26 February 2007

Asp.Net Adding a javascript confimation message box to button click

To add a javascript confimation (message) box to the click of a button add the following to the Page_Load():

btnDelete.Attributes["onclick"] = "return confirm('Are you sure you want to delete this record?');";

NHibernate - Record re-read not showing server updates

Has an issue where re-reading a record the record did not include updates that had happened on the server:
1. Read USER record (e.g. Name=JOHN).
2. Update USER record on server (e.g. set Name=MIKE).
3. Read USER record again. Name is still JOHN.

This implied the record is being read from the cache.
The problem came down to the fact that the Data-access-object was being created as a Singleton which meant the Hibernate Session was not being opened and closed.
The record will only be re-read from the server outside of Session (i.e. Hibernate will read the record from the cache if inside the same session).

Solution was to change the Spring object mapping to not create the DAO as a singleton, this meant the session was being open/closed during page life cycle.

Thursday 22 February 2007

C# Enum, enumerated types

C# Enumerated type info

Declaration:
enum MyType
{
One=1,
Two=2
};



Assignment:

int myVar = (int) MyType.One;
MyType myVar2 = MyType.Two;


Getting name of value:
string enumName = MyType.One.ToString();


Getting all values as string:
string[] enumNames = Enum.GetNames(typeof(MyType));


Getting an Enum value from a string:
MyType myVar = (MyType)Enum.Parse(typeof(MyType),"One",true)

Wednesday 21 February 2007

C# Lazy or full boolean evaluation?

C# allows for both full and lazy evaluation. For full evaluation use the single operators for And/Or (|/&), for lazy evaluation use the double operators for And/Or (&&/||).
e.g.
Full Evaluation
if ((sName==null) | (sName="~empty~"))
...do something

will cause an exception if sName is null as the second comparions (="~empty~") will be performed.

Lazy Evaluation
if ((sName==null) || (sName="~empty~"))
...do something

will not cause an exception if sName is null because as the rest of the expression is an OR the result will be true irrespective of the rest of the expression, and so the rest of the expression is ignored.

Monday 19 February 2007

Grid-View : Select a row without "Select link"

You can use javascript to select a row in a grid-view by setting the rows onMouse and onCLick events:


protected void gvSettings_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';";
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.gvSettings, "Select$" + e.Row.RowIndex);
}
}


When DataBind() is called on the grid the events will be set for each row.
Code taken from this Geekzilla article


.... further to this, if 'EnableEventValidation="true"' is on , which it is by default, the postback will cause an error: "Invalid postback or callback argument"


to overcome this you need to register each PostBack event in the Render fn of the page:


foreach (GridViewRow r in gvSettings.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
Page.ClientScript.RegisterForEventValidation(this.gvSettings.UniqueID,"Select$" + r.RowIndex);
}
}

Getting a value from a control in the Page Init

The value of a control (e.g. textbox) is not available in PreInit.
As the value may be required (e.g to generate dynamic controls, set theme) it can be retrieved by accessing Http Request:

string selectedValue ;
if (Request.Form["txtNoOfRooms"] != null)
selectedValue = Request.Form["txtNoOfRooms"].ToString();


Uses name of control to retrieve value. Not each control only has 1 value.

Example taken from this article in Code Project

Friday 16 February 2007

Setting a Page Theme

Setting a page theme programmatically can be troublesome as the theme can only be set in the Page PreInit. If the theme value is coming from a control on the page then the controls value is not available in the PreInit.
The following solution was taken from : (K.Scott Allen's) OdetoCode.Com

protected void Page_PreInit(object sender, EventArgs e)
{
if (IsPostBack)
{
string uniqueID = Request[_themeListIDKey];

if (uniqueID != null && Request[uniqueID] != null)
{
Theme = Request[uniqueID];
}
}
}

protected void Page_Load(object sender, EventArgs e)
{
ClientScript.RegisterHiddenField(
_themeListIDKey, _themeList.UniqueID
);
}

const string _themeListIDKey = "_themeListIDKey";

Thursday 15 February 2007

NHibernate - a couple of transaction related issues

A couple of issues with transactional updates

1. Timeout expired.

A timeout expired error was occurring when attempting to update the same record (and field) twice within a single transaction.
This only occurred when the database update was being performed.
Solution: When updating a record multiple times do not perform the Update until all field values have been updated.

2. Illegal attempt to associate a collection with two open sessions
This error was returned when attempting to update child records of a parent record as well as updating the parent record, where the child record was being read independent of the parent record.
e.g
Parent record links to Child1 and Child2
Parent record read (which implicity reads Child1 & Child2 and puts in list Parent.Children)
Child1 record is read independently.
Parent record Updated.
Child1 record Updated.
There is no need to explicity read and update the Child1 instance. The instance (from Parent) should be updated (in memory) instead.

C# Generics typecasting

Error being received:

cannot implicity convert type ilist to list

The error occurs when calling a function that returns an IList.
e.g.

IList myFunction();
....
List myVar = new List;
myVar=myFunction();


the error occurs on the assignment of myVar=myFunction.
The local var myVar should be declared as IList.
In the above instance the new List is not required because myFunction creates the instance.
Correct code:

IList myFunction();
....
IList myVar;
myVar=myFunction();

Wednesday 14 February 2007

NHibernate save error.

Error: "object references an unsaved transient instance...."

This occurred when saving a object that had a 1:M relationship that was represented by a list.



Parent --- child1

--- child2



To fix the error add cascade="all" to the that represents the 1:M relationship in the DB Mapping file.