Hi,
I'm a newbie to NHibernate. I've been trying to figure this out for about a day now..
I have a built the objects and classes as a separate assembly and i'm importing it into my .NET project. When I try to run i get this error
The type initializer for 'CB.AppCode.NHibernateHelper' threw an exception.
I did try to configure in the NHibernateHelper class but i'm not sure if I have to set the configuration object there...plz help..my session is always null...how do I go about it?
Pasting my NHibernateHelper.cs below:
public sealed class NHibernateHelper
{
private const string sessionKey = "nhibernate.current_session";
private static readonly ISessionFactory sessionFactory;
static NHibernateHelper()
{
log4net.Config.XmlConfigurator.Configure();
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration().Configure();
// Get the mapping file path
String mappingPath =
HttpContext.Current.Server.MapPath(
"/cms-checkedout/CodeBase/Mappings/");
// Set up the configuration
NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration()
.AddFile(mappingPath + "User.hbm.xml")
.AddFile(mappingPath + "Site.hbm.xml");
// Build the session factory
sessionFactory = config.BuildSessionFactory();
}
public static ISession GetCurrentSession()
{
HttpContext context = HttpContext.Current;
ISession currentSession = (ISession)context.Items[sessionKey];
if (currentSession == null)
{
currentSession = sessionFactory.OpenSession();
context.Items[sessionKey] = currentSession;
}
return currentSession;
}
public static void CloseSession()
{
HttpContext context = HttpContext.Current;
ISession currentSession = (ISession)context.Items[sessionKey];
if (currentSession == null)
{
// No current session
return;
}
currentSession.Close();
context.Items.Remove(sessionKey);
}
public static void CloseSessionFactory()
{
if (sessionFactory != null)
{
sessionFactory.Close();
}
}
}
And this is my aspx.cs code:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
ISession session = NHibernateHelper.GetCurrentSession();
string username = TextBox1.Text;
string password = TextBox2.Text;
User tmpUser = new User();
UserDAO userdao = new UserDAO();
tmpUser = UserDAO.validateUser(username, password);
string validUser = "";
validUser = tmpUser.userName;
if (string.IsNullOrEmpty(validUser))
{
Label1.Visible = true;
Label1.Text = "Invalid username/password";
}
else
{
Response.Redirect("index.aspx");
}
Somebody please help!
|