Пример #1
0
        private void OnDataAdapterRowUpdated(object sender, SqlRowUpdatedEventArgs e)
        {
            SqlCommand cmd = null;

            if ((e.Status == UpdateStatus.Continue) && (e.StatementType == StatementType.Insert))
            {
                //Fetch identity value after updating the row

                if (_idCol != null)
                {
                    cmd = new SqlCommand("select @@IDENTITY", _conn);
                    try
                    {
                        e.Row[_idCol.Name] = cmd.ExecuteScalar();
                        e.Row.AcceptChanges();
                    }
                    finally
                    {
                        cmd.Dispose();
                        cmd = null;
                    }
                }

                if (_tblSchema.HasComputedColumn())
                {
                    try
                    {
                        cmd            = SqlCommandGenerator.GenerateSelectCommand(_tblSchema, e.Row, false);
                        cmd.Connection = _conn;

                        SqlDataReader reader = cmd.ExecuteReader();
                        try
                        {
                            bool isSecondRow = false;
                            while (reader.Read())
                            {
                                if (isSecondRow)
                                {
                                    e.Row.RejectChanges();
                                    MessageService.ShowWarning("More than one row returned while fetching computed column values."
                                                               + "\r\nPelase reload table data to view correct values of the computed columns.");
                                    break;
                                }

                                foreach (ColumnWrapper cCol in _tblSchema.ComputedCols.Values)
                                {
                                    e.Row[cCol.Name] = reader[cCol.Name];
                                }
                                e.Row.AcceptChanges();
                                isSecondRow = true;
                            }
                        }
                        finally
                        {
                            if (reader != null && !reader.IsClosed)
                            {
                                reader.Close();
                                reader.Dispose();
                            }
                            reader = null;
                        }
                    }
                    finally
                    {
                        if (cmd != null)
                        {
                            cmd.Dispose();
                        }
                        cmd = null;
                    }
                }

                _bs.ResetCurrentItem();
            }
        }
Пример #2
0
        private void UpdateRowToDatabase()
        {
            if (LastDataRow != null)
            {
                if (LastDataRow.RowState == DataRowState.Unchanged)
                {
                    return;
                }

                if (_generatedCommands)
                {
                    _adapter.Update(new DataRow[1] {
                        LastDataRow
                    });
                    _bs.ResetCurrentItem();
                    return;
                }

                SqlTransaction tr = null;
                switch (LastDataRow.RowState)
                {
                case DataRowState.Added:
                    SqlCommand insertCmd = SqlCommandGenerator.GenerateInsertCommand(_tblSchema, LastDataRow, _tableName);
                    if (insertCmd == null)
                    {
                        throw new Exception("Can not apply deleted record to database!");
                    }
                    insertCmd.Connection   = _conn;
                    _adapter.InsertCommand = insertCmd;
                    _adapter.Update(new DataRow[1] {
                        LastDataRow
                    });
                    break;

                case DataRowState.Modified:
                    SqlCommand updateCmd = SqlCommandGenerator.GenerateUpdateCommand(_tblSchema, LastDataRow, _tableName);
                    if (updateCmd == null)
                    {
                        throw new Exception("Can not apply updated record to database!");
                    }

                    updateCmd.Connection   = _conn;
                    _adapter.UpdateCommand = updateCmd;


                    try
                    {
                        tr = _conn.BeginTransaction();
                        updateCmd.Transaction = tr;
                        int rowCnt = updateCmd.ExecuteNonQuery();
                        if (rowCnt > 1)
                        {
                            tr.Rollback();
                            LastDataRow.RejectChanges();
                            MessageService.ShowError("This operation causes multiple rows (" + rowCnt.ToString() + ") to be altered."
                                                     + "\n"
                                                     + "Correct the errors and attempt to alter the row again.");
                        }
                        else if (rowCnt == 0)
                        {
                            tr.Rollback();
                            LastDataRow.RejectChanges();
                            MessageService.ShowError("Record can not be located."
                                                     + "\nPossibly row was already updated by another user/session.");
                        }
                        else
                        {
                            LastDataRow.AcceptChanges();
                            tr.Commit();
                        }
                    }
                    catch (Exception ex)
                    {
                        LastDataRow.RejectChanges();
                        tr.Rollback();
                        throw ex;
                    }
                    break;

                case DataRowState.Deleted:
                    SqlCommand deleteCmd = SqlCommandGenerator.GenerateDeleteCommand(_dataTable.Columns, LastDataRow, _tableName);
                    if (deleteCmd == null)
                    {
                        throw new Exception("Can not apply deleted record to database!");
                    }

                    deleteCmd.Connection   = _conn;
                    _adapter.DeleteCommand = deleteCmd;
                    try
                    {
                        tr = _conn.BeginTransaction();
                        deleteCmd.Transaction = tr;
                        int rowCnt = deleteCmd.ExecuteNonQuery();
                        if (rowCnt > 1)
                        //&& !MessageService.AskQuestion("This operation will delete multiple rows!\r\nAre you sure you want to perform this operation?"))
                        {
                            tr.Rollback();
                            LastDataRow.RejectChanges();
                            MessageService.ShowError("This operation causes multiple rows (" + rowCnt.ToString() + ") to be deleted."
                                                     + "\n"
                                                     + "Correct the errors and attempt to delete the row again.");
                        }
                        else if (rowCnt == 0)
                        {
                            tr.Rollback();
                            LastDataRow.RejectChanges();
                            MessageService.ShowError("Record can not be located."
                                                     + "\nPossibly row was already deleted by another user/session.");
                        }
                        else
                        {
                            LastDataRow.AcceptChanges();
                            tr.Commit();
                        }
                    }
                    catch (Exception ex)
                    {
                        LastDataRow.RejectChanges();
                        tr.Rollback();
                        throw ex;
                    }

                    break;

                default:
                    break;
                }
            }
        }