Example #1
0
        public static List <T> FillCollectionFromDataSet(DataSet ds)
        {
            var lstT = Null <T> .GetListCollectionNull();

            if (ds != null && ds.Tables.Count > 0)
            {
                // get properties for type
                Hashtable objProperties = GetPropertyInfo(typeof(T));

                // get ordinal positions in datareader
                Hashtable arrOrdinals = GetOrdinalsFromDataSet(objProperties, ds);

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    // fill business object
                    var objFillObject = (T)CreateObjectFromDataSet(typeof(T), dr, objProperties, arrOrdinals);

                    // add to collection
                    lstT.Add(objFillObject);
                }
            }

            return(lstT);
        }
Example #2
0
        private static object CreateObjectFromDataSet(Type objType, DataRow dr, Hashtable objProperties, Hashtable arrOrdinals)
        {
            try
            {
                object objObject = Activator.CreateInstance(objType);

                // fill object with values from datareader
                string _fieldname = "";
                int    _possition = -1;
                foreach (DictionaryEntry de in arrOrdinals)
                {
                    _fieldname = de.Key.ToString();
                    _possition = (int)de.Value;

                    if (((PropertyInfo)objProperties[_fieldname]).CanWrite)
                    {
                        if (dr[_fieldname] == System.DBNull.Value)
                        {
                            // translate Null value
                            ((PropertyInfo)objProperties[_fieldname]).SetValue(objObject, Null.SetNull((PropertyInfo)objProperties[_fieldname]), null);
                        }
                        else
                        {
                            try
                            {
                                Type pType = ((PropertyInfo)objProperties[_fieldname]).PropertyType;

                                #region
                                switch (pType.FullName)
                                {
                                case "System.Enum":
                                    ((PropertyInfo)objProperties[_fieldname]).SetValue(objObject, System.Enum.ToObject(pType, dr[_possition]), null);
                                    break;

                                case "System.String":
                                    ((PropertyInfo)objProperties[_fieldname]).SetValue(objObject, (string)dr[_possition], null);
                                    break;

                                case "System.Boolean":
                                    ((PropertyInfo)objProperties[_fieldname]).SetValue(objObject, (Boolean)dr[_possition], null);
                                    break;

                                case "System.Decimal":
                                    ((PropertyInfo)objProperties[_fieldname]).SetValue(objObject, Convert.ToDecimal(dr[_possition].ToString()), null);
                                    break;

                                case "System.Int16":
                                    ((PropertyInfo)objProperties[_fieldname]).SetValue(objObject, Convert.ToInt16(dr[_possition].ToString()), null);
                                    break;

                                case "System.Int32":
                                    ((PropertyInfo)objProperties[_fieldname]).SetValue(objObject, Convert.ToInt32(dr[_possition].ToString()), null);
                                    break;

                                case "System.Int64":
                                    ((PropertyInfo)objProperties[_fieldname]).SetValue(objObject, Convert.ToInt64(dr[_possition].ToString()), null);
                                    break;

                                case "System.DateTime":
                                    ((PropertyInfo)objProperties[_fieldname]).SetValue(objObject, (DateTime)dr[_possition], null);
                                    break;

                                case "System.Double":
                                    ((PropertyInfo)objProperties[_fieldname]).SetValue(objObject, (Double)dr[_possition], null);
                                    break;

                                default:
                                    // try explicit conversion
                                    ((PropertyInfo)objProperties[_fieldname]).SetValue(objObject, Convert.ChangeType(dr[_possition], pType), null);
                                    break;
                                }
                                #endregion
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }
                        }
                    }
                }

                return(objObject);
            }
            catch (Exception)
            {
                throw;
            }
        }