Recent site activity

Tutorials‎ > ‎C# Tutorials‎ > ‎

How to use CSLA NameList

posted Sep 27, 2011 6:37 AM by Tim Key   [ updated Sep 27, 2011 6:40 AM ]
using System;
using Csla;
using System.Data.SqlClient;
using System.Data;
using Csla.Data;
 
namespace CSLASample.NameList
{
    [Serializable]
    public class FindItemCollection : NameValueListBase<stringstring>
    {
        #region Factory Methods
 
        private static FindItemCollection _list;
 
        public static FindItemCollection GetFindItemCollection()
        {
            if (_list == null)
                _list = DataPortal.Fetch<FindItemCollection>();
            return _list;
        }
 
        public static void InvalidateCache()
        {
            _list = null;
        }
 
        private FindItemCollection()
        { /* require use of factory methods */ }
 
        #endregion
 
        #region Data Access
 
        private void DataPortal_Fetch()
        {
            RaiseListChangedEvents = false;
            IsReadOnly = false;
            //Load Values
            using (SqlConnection cn = new SqlConnection(Database.Connection))
            {
                cn.Open();
                using (SqlCommand cm = cn.CreateCommand())
                {
                    cm.CommandType = CommandType.StoredProcedure;
                    cm.CommandText = "getFindItemList";
                    using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
                    {
                        while (dr.Read())
                        {
                            Add(new NameValueListBase<stringstring>.NameValuePair(
                                dr.GetString("IMA_ItemID"), dr.GetString("IMA_ItemName")));
                        }
                    }
                }
            }
            IsReadOnly = true;
            RaiseListChangedEvents = true;
        }
 
        #endregion
    }
}