Hi I've started working with NHibernate for the first time and doing with some reference given in Code Project. I have created a console application which does simple work employee details maintenance. Table Employee has ID and Name as columns, I'm deleting, inserting and updatinh through Nhibernate. But I'm facing an issue or exception Object Reference not set to an instance of an object
HEre is wat I've done so far
Employee.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NHibernateSimpleDemo
{
class Employee
{
public virtual int ID { get; set; }
public virtual string Name { get; set; }
}
}
Employee.hbm.xml
="1.0"="utf-8"
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateSimpleDemo" assembly="NHibernateSimpleDemo">
<class name="Employee" table="Employee" lazy="false">
<id name="ID" column="ID">
<column name ="ID"/>
<generator class="native"/>
</id>
<property name="Name" column="Name" />
</class>
</hibernate-mapping>
App config file
="1.0"="utf-8"
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Data Source=(local);Initial Catalog=Testing;User ID=sa;Password=Keane123"</property>
<property name="show_sql">true</property>
<mapping assembly="NHibernateSimpleDemo" />
</session-factory>
</hibernate-configuration>
</configuration>
Program.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using NHibernate;
using NHibernate.Cfg;
namespace NHibernateSimpleDemo
{
class Program
{
private static Configuration myConfiguration;
private static ISessionFactory mySessionFactory = null;
private static ISession mySession;
static void Main(string[] args)
{
try
{
if (mySession != null && mySession.IsOpen)
{
mySession.Close();
}
if (mySessionFactory != null && !mySessionFactory.IsClosed)
{
mySessionFactory.Close();
}
Employee emp = new Employee();
myConfiguration = new Configuration();
myConfiguration.Configure();
myConfiguration.AddAssembly("NHibernateSimpleDemo");
mySessionFactory = myConfiguration.BuildSessionFactory();
mySession = mySessionFactory.OpenSession();
Console.WriteLine("Insert an employee");
Console.WriteLine("Enter employee ID");
emp.ID = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("enter name of employee");
emp.Name = Console.ReadLine();
using (mySession.BeginTransaction())
{
mySession.Save(emp.ID, emp.Name);
mySession.Transaction.Commit();
}
Console.WriteLine("Update an employee");
using (mySession.BeginTransaction())
{
var empl = mySession.Load<Employee>(4);
empl.Name = "User";
mySession.Update(empl);
mySession.Transaction.Commit();
}
using (mySession.BeginTransaction())
{
Console.WriteLine("Delete an employee");
Console.WriteLine("Enter employee ID");
emp.ID = Convert.ToInt32(Console.ReadLine());
Object result = mySession.CreateSQLQuery("DELETE from Employee where ID = :itm")
.AddScalar("count", NHibernate.NHibernateUtil.Int32)
.SetParameter("itm",emp.ID)
.UniqueResult();
mySession.Transaction.Commit();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message + e.InnerException.Message);
}
}
}
}
I'm getting this exception when the break point reaches
mySessionFactory = myConfiguration.BuildSessionFactory();
Someone help me I'm really breaking my heads for solution
Thanks in advance