Beispiel #1
0
        /// <summary>
        /// This method is fired by 'Data Interface/InsertUpdateRow' method.
        /// </summary>
        /// <param name="row"></param>
        /// <param name="columnArgs"></param>
        /// <param name="rowIdentity"></param>
        internal void UpdateInsertColumnDataSourceId(Row row, BeforeUpdateInsertEventArgs columnArgs,
            string rowIdentity)
        {
            bool isInsert = true;

            if (DataSourceId == null || row == null)
                return;
            if (string.IsNullOrEmpty(m_DataSourcePrimaryKeys))
                throw new GridException(string.Format("Set the 'DataSourcePrimaryKeys' for the property 'DataSourceId' in the column '{0}'", ColumnId));

            columnArgs.DataSourceId = DataSourceId;

            for (int i = 0; i < row.Columns.Count; i++)
                columnArgs.Row[row.Columns[i].ColumnId].Value = row[row.Columns[i].ColumnId].Value;

            m_Table.m_Grid.BeforeUpdateInsertEvent(columnArgs);

                for (int i = 0; i < row.Columns.Count; i++)
                if (row.Columns[i].ColumnType == ColumnType.SystemColumn == false)
                    row[row.Columns[i].ColumnId].Value = columnArgs.Row[row.Columns[i].ColumnId].Value;
            if (!columnArgs.AcceptChanges)
                return;

            string where = BuildColumnDataSourceIdFilter(row, m_DataSourcePrimaryKeys.Split(';'));
            if (where == null || where.Length < 2)
                throw new GridException(
                    string.Format(
                        "Unable to build a valid filter expression for the property 'DataSourceId' in the column '{0}'",
                        ColumnId));

            Query shouldUpdate =
                Query.ExecuteReader(string.Format("SELECT (1) FROM [{0}] WHERE {1}", DataSourceId, where),
                                    m_Grid.ActiveConnectionString, m_Grid.DatabaseConnectionType);

            if (shouldUpdate.Read())
                isInsert = false;
            shouldUpdate.Close();

            InsertUpdate insertUpdateDataTable = isInsert
                                                     ?
                                                         new InsertUpdate(DataSourceId, QueryType.Insert,
                                                                          Grid.ActiveConnectionString)
                                                     :
                                                         new InsertUpdate(DataSourceId, QueryType.Update,
                                                                          Grid.ActiveConnectionString);
            columnArgs.DataSourceId = DataSourceId;
             foreach (Column column in DataSourceTable.Columns)
            {
                string columnID = column.ColumnId;

                if (column.ColumnType == ColumnType.SystemColumn)
                    continue;
                object value = null;
                if (row.Columns.Contains(columnID)) // FIRST TRY TO RETRIEVE DATA FROM row.Row...
                {
                    CellUpdateInsertArgument ea = columnArgs.Row[row.Columns[columnID].ColumnId];

                    if (ea != null)
                    {
                        row.Columns[columnID].OnUpdateInsert(ea, row[column.ColumnId]);

                        if (ea.Value != null)
                        {
                            if (ea.m_Insert && row.Columns[columnID].Identity)
                                value = rowIdentity;
                            else
                                switch (row.Columns[columnID].ColumnType)
                                {
                                    case ColumnType.Foreignkey:
                                        value = ea.m_Insert ? ea.Value : row[ea.Column.ColumnId].Value;
                                        break;
                                    default:
                                        value = ea.Value;
                                        break;
                                }
                        }
                        else if (Grid.GotHttpContext && HttpContext.Current.Session[columnID] != null)
                            // THEN SESSION OBJECT..
                            value = HttpContext.Current.Session[columnID];
                    }
                }
                else if (Grid.GotHttpContext && HttpContext.Current.Session[columnID] != null) //SKIP
                    value = HttpContext.Current.Session[columnID];
                else
                    continue;

                if (columnID != null)
                    insertUpdateDataTable.Add(columnID, value);
            }
            try
            {
                if (isInsert == false)
                    insertUpdateDataTable.FilterExpression = where;
                insertUpdateDataTable.Execute();
                if (Grid.Debug)
                {
                    Grid.m_DebugString.AppendFormat("<b>Matrix-InsertUpdate({0})</b>:{1}<br/>", ColumnId,
                                                    insertUpdateDataTable.GetQueryString());
                    Grid.m_DebugString.AppendFormat("<b>Matrix-InsertUpdate where</b>:{0}<br/>", where);
                }
            }
            catch (Exception ee)
            {

                throw new GridException(insertUpdateDataTable.GetQueryString(),
                                        string.Format("Error updating/inserting datatable for column: {0}", ColumnId),
                                        ee);
            }
        }
Beispiel #2
0
 internal void BeforeUpdateInsertEvent(BeforeUpdateInsertEventArgs ea)
 {
     if (BeforeUpdateInsert != null)
         BeforeUpdateInsert(this, ea);
 }