예제 #1
0
        /// <summary>
        /// Returns the collection's items in a DataTable.
        /// </summary>
        /// <returns></returns>
        public DataTable ToDataTable()
        {
            DataTable tblOut = new DataTable();

            // create the columns
            ItemType schema = new ItemType();

            tblOut.TableName = schema.TableName;

            // get the schema from the object
            TableSchema.TableColumnSettingCollection settings = schema.GetColumnSettings();

            // add the columns
            foreach (TableSchema.TableColumnSetting setting in settings)
            {
                DataColumn col = new DataColumn(setting.ColumnName);
                TableSchema.TableColumn tableColumn = schema.GetSchema().GetColumn(setting.ColumnName);
                if (tableColumn != null)
                {
                    col.DataType    = tableColumn.GetPropertyType();
                    col.Caption     = tableColumn.DisplayName;
                    col.AllowDBNull = tableColumn.IsNullable;
                }

                tblOut.Columns.Add(col);
            }

            // set the values
            foreach (ItemType item in this)
            {
                item.CopyTo(tblOut);
            }

            return(tblOut);
        }
예제 #2
0
        /// <summary>
        /// Loops the underlying settings collection to validate type, nullability, and length
        /// </summary>
        public void ValidateColumnSettings()
        {
            // loop the current settings and make sure they are valid for their type
            foreach (TableSchema.TableColumnSetting setting in GetColumnSettings())
            {
                Utility.WriteTrace(String.Format("Validating {0}", setting.ColumnName));
                object settingValue         = setting.CurrentValue;
                bool   isNullValue          = (settingValue == null || settingValue == DBNull.Value);
                TableSchema.TableColumn col = table.GetColumn(setting.ColumnName);

                if (!col.IsReadOnly)
                {
                    string formattedName = Utility.ParseCamelToProper(col.ColumnName);
                    Type   t             = col.GetPropertyType();

                    //// Convert the existing value to the type for this column
                    //// if there's an error, report it.
                    //// OK to bypass if the column is nullable and this setting is null
                    //// just check for now if the value isn't null - it will be checked
                    //// later for nullability

                    if (!col.IsNullable && !isNullValue)
                    {
                        try
                        {
                            if (col.DataType != DbType.Guid)
                            {
                                Convert.ChangeType(settingValue, t);
                            }
                        }
                        catch
                        {
                            // there's a conversion problem here
                            // add it to the Exception List<>
                            if (col.IsNumeric)
                            {
                                errorList.Add(String.Format(InvalidTypeExceptionMessage, formattedName, "number"));
                            }
                            else if (col.IsDateTime)
                            {
                                errorList.Add(String.Format(InvalidTypeExceptionMessage, formattedName, "date"));
                            }
                            else
                            {
                                errorList.Add(String.Format(InvalidTypeExceptionMessage, formattedName, "value"));
                            }
                        }
                    }

                    bool isDbControlledAuditField = (Utility.IsAuditField(col.ColumnName) && !String.IsNullOrEmpty(col.DefaultSetting));

                    // now make sure that this column's null settings match with what's in the setting
                    Utility.WriteTrace(String.Format("Testing nullability of {0}", setting.ColumnName));
                    if (!col.IsNullable && isNullValue && !isDbControlledAuditField)
                    {
                        Utility.WriteTrace(String.Format("Null Error Caught {0}", setting.ColumnName));
                        errorList.Add(String.Format(NullExceptionMessage, formattedName));
                    }

                    // finally, check the length
                    Utility.WriteTrace(String.Format("Testing Max Length of {0}", setting.ColumnName));
                    if (!isNullValue && col.MaxLength > 0)
                    {
                        if (col.DataType != DbType.Boolean && settingValue.ToString().Length > col.MaxLength)
                        {
                            Utility.WriteTrace(String.Format("Max Length Exceeded {0} (can't exceed {1}); current value is set to {2}",
                                                             col.ColumnName,
                                                             col.MaxLength,
                                                             settingValue.ToString().Length));
                            errorList.Add(String.Format(LengthExceptionMessage, formattedName, col.MaxLength));
                        }
                    }
                }
            }
        }