public static List <ComboBoxItem> GetEmployeeList()
        {
            List <ComboBoxItem> items  = new List <ComboBoxItem>();
            ComboBoxItem        cbItem = new ComboBoxItem();

            cbItem          = new ComboBoxItem();
            cbItem.strValue = "Show All";
            cbItem.key      = "Show All Employees";
            items.Add(cbItem);
            string sql = "SELECT EmployeeID,FullName FROM Employee WHERE EmployeeID IN (SELECT empID FROM vw_ChangeLog) ORDER BY FullName ";

            using (clsDataGetter dg = new clsDataGetter(CommonProcs.WCompanyConnStr))
            {
                System.Data.SqlClient.SqlDataReader dr = dg.GetDataReader(sql);
                while (dr.Read())
                {
                    cbItem          = new ComboBoxItem();
                    cbItem.strValue = dr[0].ToString();
                    cbItem.key      = dr[1].ToString();
                    items.Add(cbItem);
                }
                dg.KillReader(dr);
            }
            return(items);
        }
Beispiel #2
0
        public virtual T CreateModel(string sql, string connStr = "")
        {
            var results = new List <T>();

            if (connStr == string.Empty)
            {
                connStr = CommonProcs.WCompanyConnStr;
            }
            using (clsDataGetter dg = new clsDataGetter(connStr))
            {
                SqlDataReader reader    = dg.GetDataReader(sql);
                var           NotMapped = new List <String>();

                var props = typeof(T).GetProperties().Where(x => Attribute.IsDefined(x, typeof(NotMappedAttribute)));
                foreach (var prop in props)
                {
                    NotMapped.Add(prop.Name);
                }

                if (reader == null || reader.HasRows == false)
                {
                    return(default(T));
                }

                while (reader.Read())
                {
                    var item = Activator.CreateInstance <T>();
                    foreach (var property in typeof(T).GetProperties())
                    {
                        if (!NotMapped.Contains(property.Name))
                        {
                            try
                            {
                                if (!reader.IsDBNull(reader.GetOrdinal(property.Name)))
                                {
                                    Type convertTo = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
                                    property.SetValue(item, Convert.ChangeType(reader[property.Name], convertTo), null);
                                }
                                else
                                {
                                    if (property.PropertyType.Name == "String")
                                    {
                                        property.SetValue(item, "");
                                    }
                                }
                            }
                            catch
                            {
                            }
                        }
                    }
                    results.Add(item);
                }
                dg.KillReader(reader);
            }
            return(results[0]);
        }