Пример #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>
        /// Sets a value for a particular column in the record
        /// </summary>
        /// <param name="columnName">Name of the column, as defined in the database</param>
        /// <param name="oValue">The value to set the type to</param>
        public void SetColumnValue(string columnName, object oValue)
        {
            columnSettings = columnSettings ?? new TableSchema.TableColumnSettingCollection();

            // add the column to the DirtyColumns
            // if this instance has already been loaded
            // and this is a change to existing values
            if (IsLoaded && !IsNew)
            {
                TableSchema.Table schema      = GetSchema();
                object            oldValue    = null;
                string            oldValueMsg = "NULL";
                string            newValueMsg = "NULL";
                bool areEqualOrBothNull       = false;

                try
                {
                    oldValue = columnSettings.GetValue(columnName);
                }
                catch {}

                if (oldValue == null && oValue == null)
                {
                    areEqualOrBothNull = true;
                }
                else
                {
                    if (oldValue != null)
                    {
                        oldValueMsg        = oldValue.ToString();
                        areEqualOrBothNull = oldValue.Equals(oValue);
                    }

                    if (oValue != null)
                    {
                        newValueMsg = oValue.ToString();
                    }
                }

                TableSchema.TableColumn dirtyCol = schema.GetColumn(columnName);

                if (dirtyCol != null && !areEqualOrBothNull)
                {
                    string auditMessage = String.Format("Value changed from {0} to {1}{2}", oldValueMsg, newValueMsg, Environment.NewLine);
                    TableSchema.TableColumn dirtyEntry = DirtyColumns.GetColumn(columnName);
                    if (dirtyEntry != null)
                    {
                        DirtyColumns.Remove(dirtyEntry);
                        auditMessage = String.Concat(dirtyCol.AuditMessage, auditMessage);
                    }

                    dirtyCol.AuditMessage = auditMessage;
                    DirtyColumns.Add(dirtyCol);
                }
            }

            columnSettings.SetValue(columnName, oValue);
        }
Пример #3
0
        /// <summary>
        /// Loads your object from an ASP.NET form postback
        /// </summary>
        /// <param name="validatePost">Set this to false to skip validation</param>
        public void LoadFromPost(bool validatePost)
        {
            if (HttpContext.Current != null)
            {
                // use Request.form, since the ControlCollection can return weird results based on
                // the container structure.
                NameValueCollection formPost = HttpContext.Current.Request.Form;
                TableSchema.TableColumnSettingCollection settings = GetColumnSettings();

                if (formPost != null && settings != null)
                {
                    foreach (string s in formPost.AllKeys)
                    {
                        Utility.WriteTrace(String.Format("Looking at form field {0}", s));

                        foreach (TableSchema.TableColumnSetting setting in settings)
                        {
                            if (s.EndsWith(String.Concat("_", setting.ColumnName), StringComparison.InvariantCultureIgnoreCase) ||
                                s.EndsWith(String.Concat("$", setting.ColumnName), StringComparison.InvariantCultureIgnoreCase) ||
                                Utility.IsMatch(s, setting.ColumnName))
                            {
                                SetColumnValue(setting.ColumnName, formPost[s]);
                                Utility.WriteTrace(String.Format("Matched {0} to {1}", s, setting.ColumnName));
                            }
                        }
                    }
                }

                // validate the settings, since we're setting the object values here, not
                // using the accessors as we should be.
                if (validatePost)
                {
                    ValidateColumnSettings();

                    if (errorList.Count > 0)
                    {
                        // format this for the web
                        if (HttpContext.Current != null)
                        {
                            // decorate the output
                            StringBuilder errorReport = new StringBuilder("<b>Validation Error:</b><ul>");
                            foreach (string s in errorList)
                            {
                                errorReport.AppendFormat("<li><em>{0}</em></li>", s);
                            }

                            errorReport.Append("</ul>");
                            throw new Exception(errorReport.ToString());
                        }

                        throw new Exception(
                                  "Validation error - catch this and check the ExceptionList property to review the exceptions. You can change the output message as needed by accessing the ExceptionMessage properties of this object");
                    }
                }
            }
        }