Ejemplo n.º 1
0
        public void Update(WorkType entity)
        {
            SqlCommand     command     = null;
            SqlTransaction transaction = null;
            SqlDataAdapter adapter     = null;

            try
            {
                // Define command.
                command             = new SqlCommand();
                command             = mDbConnection.CreateCommand();
                command.CommandText = "UpdateWorkTypes";
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@DataXml", SqlDbType.Xml).Value      = entity.GetXml();
                command.Parameters.Add("@hasError", SqlDbType.Bit).Direction = ParameterDirection.Output;
                //command.ExecuteNonQuery();

                // execute
                transaction         = mDbConnection.BeginTransaction();
                command.Transaction = transaction;

                // fill error datatable
                adapter = new SqlDataAdapter(command);
                DataTable errorDataTable = new DataTable();
                adapter.Fill(errorDataTable);

                // commit
                transaction.Commit();

                // Get output parameters.
                bool hasError = bool.Parse(command.Parameters["@hasError"].Value.ToString());

                if (hasError)
                {
                    // Create exception instance.
                    ValidationException exception = new ValidationException(string.Empty);

                    if (errorDataTable != null)
                    {
                        StringBuilder message = new StringBuilder();
                        foreach (DataRow row in errorDataTable.Rows)
                        {
                            message.Append(string.Format("{1}", row["Name"].ToString(), row["Value"].ToString()));
                        }
                        exception.Data.Add("IsExists", message);
                    }

                    throw exception;
                }
            }
            catch (ValidationException ve)
            {
                throw ve;
            }
            catch
            {
                if (transaction != null)
                {
                    if (transaction.Connection != null)
                    {
                        transaction.Rollback();
                    }
                }

                throw;
            }
            finally
            {
                // Dispose.
                if (transaction != null)
                {
                    transaction.Dispose();
                }
                if (command != null)
                {
                    command.Dispose();
                }
            }
        }