If if the object reference is type int, sure. That's what I've done, well not using a direct cast. I wrapped the ExecuteScalarQuery in a method like so:
Code:
public object ExecuteScalarQuery(string query)
{
return NHibernateSession.CreateQuery(query).UniqueResult()
}
I called it from VB.Net using CType(dao.ExecuteScalarQuery(query), INTEGER
Code:
...
return CType(dao.ExecuteScalarQuery(query), Integer)
I also wasn't using SetString(), but I'd imagine it would just return the modified IQuery instance. Not sure, I'd probably break it down into a few lines to ensure there were no issues. I'd also ensure that all input params are valid (like uname), check the SQL generated by NHibernate (in log file), run the query independently (which should make a difference here), then I'd ensure that I'm not getting a null back from the query (which I'm not sure why that would happen off hand).