Example #1
0
        /// <summary>
        /// Deletes a row from the database.
        /// </summary>
        /// <param name="table">The table in which to delete the row.</param>
        /// <param name="rowKeys">The row ID.</param>
        /// <returns></returns>
        internal static bool DeleteRow(Table table, string rowKeys)
        {
            if (Grid.GotHttpContext &&
                System.IO.File.Exists(String.Format("{0}\\noupdate.webgrid", HttpContext.Current.Server.MapPath("."))) &&
                table.m_Grid.Tag["allowupdate"] == null)
            {
                string content =
                    System.IO.File.ReadAllText(String.Format("{0}\\noupdate.webgrid", HttpContext.Current.Server.MapPath(".")));
                table.m_Grid.SystemMessage.Add(
                    String.IsNullOrEmpty(content)
                        ? "Inserting, updating, or deleting a database record functionality has been disabled."
                        : content);
                return false;
            }
            table.GetData(false);
            if (table.m_Grid.MasterTable.Rows.Count == 0)
                return true;
            if ( table.DataSource != null || table.m_Grid.MasterTable.Rows[0].DataRow != null)
            {
                string tmpId = table.m_Grid.InternalId;
                table.m_Grid.InternalId = rowKeys;
                table.m_Grid.MasterTable.GetData(true);

                switch (table.DataSourceType)
                {
                    case DataSourceControlType.SqlDataSource:
                    case DataSourceControlType.AccessDataSource:
                        DeleteDataSourceControl(table.m_Grid.MasterTable.Rows[0]);
                        break;
                    case DataSourceControlType.ObjectDataSource:
                        DeleteObjectDataSourceControl(table.m_Grid.MasterTable.Rows[0]);
                        break;
                }

                if (table.m_XmlDataDocument == null)
                {
                    table.m_Grid.MasterTable.Rows[0].DataRow.Delete();

                    if ( table.DataSource != null &&  table.DataSource is OleDbDataAdapter)
                    {
                        try
                        {
                            OleDbCommandBuilder updateCommand =
                                new OleDbCommandBuilder((OleDbDataAdapter) table.DataSource);

                            ((OleDbDataAdapter) table.DataSource).Update(
                                table.m_Grid.MasterTable.Rows[0].DataRow.Table);
                            updateCommand.Dispose();
                        }
                        catch (Exception ee)
                        {
                            throw new GridException("Error deleting record from data source.", ee);
                        }
                    }
                    table.m_Grid.MasterTable.Rows[0].DataRow.Table.AcceptChanges();
                }
                else if (table.m_XmlDataDocument != null)
                {

                    try
                    {
                        List<Column> datacolumns = table.Columns.Primarykeys;

                        if (datacolumns == null)
                        {
                            table.m_Grid.SystemMessage.Add("Primary key is required for the XML file to delete rows.",
                                                           true);
                            return false;
                        }

                        foreach (DataTable dt in table.m_XmlDataSet.Tables)
                        {
                            if (dt.TableName != rowKeys) continue;
                            int count = dt.Rows.Count;
                            for (int i = 0; i < count; i++)
                                table.m_XmlDataSet.Tables[dt.TableName].Rows.RemoveAt(0);
                            break;
                        }
                        table.m_XmlDataSet.AcceptChanges();
                        table.m_XmlDataSet.WriteXml(table.m_XmlDataDocument);
                    }
                    catch (Exception ee)
                    {
                        throw new GridDataSourceException("Error removing row in XML", ee);
                    }
                }

                table.m_Grid.InternalId = tmpId;

            }
            else
            {
                string datasourcetable = table.DataSourceId;
                if( datasourcetable == null)
                    return true;
                if (datasourcetable.Equals(Grid.DATASOURCEID_NULL))
                    datasourcetable = table.Columns.Primarykeys[0].DataSourceId ??
                                      table.Columns.Primarykeys[0].DefaultDataSourceId;
                InsertUpdate delete = new InsertUpdate(datasourcetable, QueryType.Delete, table.ConnectionString)
                                          {FilterExpression = BuildPKFilter(table, rowKeys, true)};

                if (table.m_Grid.Debug)
                    table.m_Grid.m_DebugString.AppendFormat("<b>{0}: SqlConnection/OleDB.DeleteRow({1}) </b>- {2}<br/>",
                                                            table.m_Grid.ID, table.DataSourceId, delete.GenerateSql());

                try
                {
                    // MUST FIX;
                    delete.Execute();
                }
                catch (Exception ee)
                {
                    throw new GridDataSourceException(String.Format("Error deleting datasource record (SQL generated:{0}) FILTER: " + BuildPKFilter(table, rowKeys, true), delete.GenerateSql()), ee);
                }
            }

            return true;
        }