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);
}
}