Skip to main content

Sample code for Populating DropDownList Using QueryOver In NH3

  
Code For Populating DropDownList 
 
 NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();
            Assembly myAssenbly = typeof(Contactss).Assembly;

 config.AddAssembly(myAssenbly);
                ISessionFactory factory = config.BuildSessionFactory();
                ISession session = factory.OpenSession();
                IList FName =(IList)session.QueryOver<Contactss>().Select(c => c.FirstName, c => c.ContactId ).List<object[]>().Select(x => new {
                fn=(string)x[0],
                Id=(Int32)x[1]
                }).ToList();
                DropDownList1.DataSource = FName;
                DropDownList1.DataTextField = "fn";
                DropDownList1.DataValueField = "Id";

                DropDownList1.DataBind();
                session.Close();



contactss is a Business Model as follows

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication1
{
    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; }
        }

     
    }
}

Comments