Skip to main content

NHibernate sample web Application

step 0-> 
Create a database(Example:Employee),  And a Table(Example:tbl_contact )

 
 step 1->

 add references as follows





step 2-> 


then open visual studio webapplication(name for example: WebApplication1)
step 2.1->

Add a Class for storing business objects as follows
  public class Contactss
    {
        private int _contactId;
        private string _firstName;
        private string _lastName;
        private string _email;
        private string _telephone;

        public virtual int ContactId
        {
            get { return _contactId; }
            set { _contactId = value; }
        }

        public virtual string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; }
        }

        public virtual string LastName
        {
            get { return _lastName; }
            set { _lastName = value; }
        }

        public virtual string Email
        {
            get { return _email; }
            set { _email = value; }
        }

        public virtual string Telephone
        {
            get { return _telephone; }
            set { _telephone = value; }
        }

     
    } 

step 2.2->
Add a Web form with following design

 <form id="form2" runat="server">
    <div>
        <asp:Label ID="lblFirstName" runat="server" Text="First Name:"></asp:Label>
        <asp:TextBox ID="FirstName" runat="server"></asp:TextBox><br />
        <asp:Label ID="lblLastName" runat="server" Text="Last Name:"></asp:Label>
        <asp:TextBox ID="LastName" runat="server"></asp:TextBox><br />
        <asp:Label ID="lblEmail" runat="server" Text="Email:"></asp:Label>
        <asp:TextBox ID="Email" runat="server"></asp:TextBox><br />
        <asp:Label ID="lblTelephone" runat="server" Text="Telephone:"></asp:Label>
        <asp:TextBox ID="Telephone" runat="server"></asp:TextBox><br />
       
        <br />
        <br />

        <asp:Button ID="btnSaveContact" runat="server" Text="Save Contact" OnClick="btnSaveContact_Click" />
    </div>
    </form>


then create corresponding btnSaveContact_Clicke() Function

               protected void btnSaveContact_Click(object sender, EventArgs e)
        {
            NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();
            Assembly myAssenbly = typeof(Contactss).Assembly;
            config.AddAssembly(myAssenbly);

            Contactss contact = new Contactss();
            contact.FirstName = FirstName.Text;
            contact.LastName = LastName.Text;
            contact.Email = Email.Text;
            contact.Telephone = Telephone.Text;
            NHibernate.ISessionFactory factory = config.BuildSessionFactory();
            NHibernate.ISession session = factory.OpenSession();
            using (NHibernate.ITransaction transaction = session.BeginTransaction())
            {
                session.Save(contact);
                transaction.Commit();
                session.Close();
            }
        }
    }



step -3>

add Mapping File (Name for example:Contact.hbm.xml).and change its  Build Action Property to-Embedded Resources

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   namespace="WebApplication1"
                   assembly="WebApplication1">

  <class name="Contactss" table="tbl_contact">
    <id name="ContactId" column="contact_id" type="int">
      <generator class="identity"></generator>
    </id>

    <property name="FirstName"  column="first_name" type="String"/>
    <property name="LastName"   column="last_name"  type="String"/>
    <property name="Email"      column="email"      type="String"/>
    <property name="Telephone"  column="telephone"  type="String"/>
  </class>
</hibernate-mapping>



step 4->

Add  write statement in your web.xml file

<?xml version="1.0"?>
<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="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
      <property name="connection.connection_string">Data Source=PC-TVM01-D002;Initial Catalog=Employee;User ID=sa;Password=password1</property>
      <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
      <property name="show_sql">false</property>
      <!--<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>-->
      <mapping assembly="WebApplication1"/>
    </session-factory>
  </hibernate-configuration>
  <system.web>
    <compilation debug="true"/>
  </system.web>
</configuration>





Comments

Popular posts from this blog

NHibernate QueryOver Class And Projection....

Introduction The ICriteria API is NHibernate's implementation of Query Object . NHibernate 3.0 introduces the QueryOver api, which combines the use of Extension Methods and Lambda Expressions (both new in .Net 3.5) to provide a statically typesafe wrapper round the ICriteria API. QueryOver uses Lambda Expressions to provide some extra syntax to remove the 'magic strings' from your ICriteria queries. So, for example: .Add(Expression.Eq("Name", "Smith")) becomes: .Where<Person>(p => p.Name == "Smith") With this kind of syntax there are no 'magic strings', and refactoring tools like 'Find All References', and 'Refactor->Rename' work perfectly. Note: QueryOver is intended to remove the references to 'magic strings' from the ICriteria API while maintaining it's opaqueness. It is not a LINQ provider; NHibernate 3.0 has a built-in

Passing Data from View to Controller Using Ajax Example Jquery

Jquery       $ ( '#btnSaveComments' ). click ( function () { var comments = $ ( '#txtComments' ). val (); var selectedId = $ ( '#hdnSelectedId' ). val (); $ . ajax ({ url : '<%: Url.Action("SaveComments")%>' , data : { 'id' : selectedId , 'comments' : comments }, type : "post" , cache : false , success : function ( savingStatus ) { $ ( "#hdnOrigComments" ). val ( $ ( '#txtComments' ). val ()); $ ( '#lblCommentsNotification' ). text ( savingStatus ); }, error : function ( xhr , ajaxOptions , thrownError ) { $ ( '#lblCommentsNotification' ). text ( "Error encountered while saving the comments." ); } }); });     Controller    [ HttpPost ] public ActionResult SaveComments ( int id , string com

The Core Concepts of Angular -- Jithin CJ

I started to learn angular from 2016, I was very curious about the celibacy of this super hero. From my initial understanding is like, the power of angular is only limited on " html decoration "  But this JavaScript framework has the potential to re-define conventional html-css patterns . Modern browsers support for things like modules, classes, lambdas, generators, etc. These features fundamentally transform the JavaScript programming experience. But big changes aren't constrained merely to JavaScript. Web Components are on the horizon. The term Web Components usually refers to a collection of four related W3C specifications: Custom Elements - Enables the extension of HTML through custom tags.  HTML Imports - Enables packaging of various resources (HTML, CSS, JS, etc.).  Template Element - Enables the inclusion of inert HTML in a document.  Shadow DOM - Enables encapsulation of DOM and CSS.  Developers can create fully encapsulated (Shadow DOM) declar