public FrmUpdateShippingTerm(ShippingTerm term, AccessToken _myAccessToken)
 {
     InitializeComponent();
     var RoleAccess = new RoleAccess(_myAccessToken, this);
     _myShippingTermManager = new ShippingTermManager();
     _myShippingVendorManager = new ShippingVendorManager();
     vendors = _myShippingVendorManager.GetVendors();
     _originalTerm = term;
     PopulateVendorCombo();
     txtDesc.Text = term.Description;
     this.Text = "Shipping Term: " + term.Id;
 }
        public static Boolean DeactivateTerm(ShippingTerm term, SqlConnection connection)
        {
            //?? Null-coalescing operator.
            //If the connection is null a new connection will be returned.
            SqlConnection conn = connection ?? GetInventoryDbConnection();
            try
            {
                //Establishes the connection.
                conn.Open();
                //Creates the command object, passing the SP and connection object.
                SqlCommand sqlCmd = new SqlCommand("proc_DeactivateTerm", conn);
                sqlCmd.CommandType = CommandType.StoredProcedure;
                sqlCmd.Parameters.AddWithValue("@ShippingTermId", term.Id);

                //If the procedure returns 1 set to true, if 0 remain false.
                if (sqlCmd.ExecuteNonQuery() == 1)
                {
                    return true;
                }
            }
            catch (DataException ex)
            {
                Console.WriteLine(ex.Message);
                throw new ApplicationException(Messeges.GetMessage("DatabaseException"), ex);
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
                throw new ApplicationException(Messeges.GetMessage("SqlException"), ex);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw new ApplicationException(Messeges.GetMessage("Exception"), ex);
            }
            finally
            {
                conn.Close();
            }
            return false;
        }
        private void btnEdit_Click_1(object sender, EventArgs e)
        {
            ShippingTerm term;
            bool validShippingTerm = true;
            string errorMessage = "Please correct the following errors:\n";

            if(Validation.IsNullOrEmpty(txtDesc.Text) || Validation.IsBlank(txtDesc.Text))
            {
                validShippingTerm = false;
                errorMessage += "You must enter a description.";
                txtDesc.Focus();
            }
            if (txtDesc.TextLength > 250)
            {
                validShippingTerm = false;
                errorMessage += "The description must be 250 characters or less.";
                txtDesc.Focus();
            }
            if (validShippingTerm)
            {
                term = new ShippingTerm()
                {
                    Description = txtDesc.Text,
                    ShippingVendorId = ((KeyValuePair<int, string>)comboVendors.SelectedItem).Key,
                    Id = _originalTerm.Id
                };
                if (_myShippingTermManager.Update(term, _originalTerm))
                {
                    this.DialogResult = DialogResult.OK;
                    MessageBox.Show("Shipping term " + _originalTerm.Id + " was updated.");
                }
                else
                {
                    MessageBox.Show("The shipping term was not updated.");
                }
            }
            else
            {
                MessageBox.Show(errorMessage);
            }
        }
        public static bool AddShippingTerm(ShippingTerm shippingTerm, SqlConnection myConnection)
        {
            myConnection = myConnection ?? GetInventoryDbConnection();
            try
            {
                var mySqlCommand = new SqlCommand("proc_InsertIntoShippingTerm", myConnection)
                {
                    CommandType = CommandType.StoredProcedure
                };
                mySqlCommand.Parameters.AddWithValue("@shippingVendorID",shippingTerm.ShippingVendorId);
                mySqlCommand.Parameters.AddWithValue("@description", shippingTerm.Description);
                myConnection.Open();
                if (mySqlCommand.ExecuteNonQuery() == 1)
                {
                    return true;
                }

            }
            catch (DataException ex)
            {
                Console.WriteLine(ex.Message);
                throw new ApplicationException(Messeges.GetMessage("DatabaseException"), ex);
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
                throw new ApplicationException(Messeges.GetMessage("SqlException"), ex);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw new ApplicationException(Messeges.GetMessage("Exception"), ex);
            }
            finally
            {
                myConnection.Close();
            }
            return false;
        }
        public static ShippingTerm GetShippingTermsById(int id, SqlConnection myConnection)
        {
            var shippingTerm = new ShippingTerm();
            myConnection = myConnection ?? GetInventoryDbConnection();
            try
            {
                var mySqlCommand = new SqlCommand("proc_GetShippingTermsByID", myConnection)
                {
                    CommandType = CommandType.StoredProcedure
                };
                mySqlCommand.Parameters.AddWithValue("@shippingTermID", id);
                myConnection.Open();

                var mySqlReader = mySqlCommand.ExecuteReader();
                if (mySqlReader.HasRows)
                {

                    while (mySqlReader.Read())
                    {
                        shippingTerm = new ShippingTerm(mySqlReader.GetInt32(0))
                        {
                           ShippingVendorId = mySqlReader.GetInt32(1),
                           Description = mySqlReader.GetString(2),
                           ShippingVendorName = mySqlReader.GetString(3),
                           Active = mySqlReader.GetBoolean(10)
                        };
                    }

                }

                mySqlReader.Close();

            }
            catch (DataException ex)
            {
                Console.WriteLine(ex.Message);
                throw new ApplicationException(Messeges.GetMessage("DatabaseException"), ex);
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
                throw new ApplicationException(Messeges.GetMessage("SqlException"), ex);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw new ApplicationException(Messeges.GetMessage("Exception"), ex);
            }
            finally
            {
                myConnection.Close();
            }

            return shippingTerm;
        }
        public static List<ShippingTerm> GetAllShippingTerms(SqlConnection myConnection)
        {
            var shippingTermList = new List<ShippingTerm>();

            myConnection = myConnection ?? GetInventoryDbConnection();
            try
            {
                var mySqlCommand = new SqlCommand("proc_GetAllShippingTerms", myConnection)
                {
                    CommandType = CommandType.StoredProcedure
                };

                myConnection.Open();

                var mySqlReader = mySqlCommand.ExecuteReader();
                if (mySqlReader.HasRows)
                {
                    while (mySqlReader.Read())
                    {
                        var shippingTerm = new ShippingTerm(mySqlReader.GetInt32(0))
                        {
                            ShippingVendorId = mySqlReader.GetInt32(1),
                            Description = mySqlReader.GetString(2),
                            ShippingVendorName = mySqlReader.GetString(3),
                            Active = mySqlReader.GetBoolean(4)
                        };
                        //Add item to list
                        shippingTermList.Add(shippingTerm);
                    }
                }
                mySqlReader.Close();
            }
            catch (DataException ex)
            {
                Console.WriteLine(ex.Message);
                throw new ApplicationException(Messeges.GetMessage("DatabaseException"), ex);
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
                throw new ApplicationException(Messeges.GetMessage("SqlException"), ex);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw new ApplicationException(Messeges.GetMessage("Exception"), ex);
            }
            finally
            {
                myConnection.Close();
            }

            return shippingTermList;
        }
 public bool Insert(ShippingTerm term)
 {
     //Need to do error checking.
     return ShippingTermDAL.AddShippingTerm(term, _connection);
 }
 public Boolean DeleteTerm(ShippingTerm shippingTerm)
 {
     if (shippingTerm == null) throw new ArgumentException("The shipping term was null, therefore it cannot be deleted.");
     return ShippingTermDAL.DeleteTerm(shippingTerm, _connection);
 }
 public bool Update(ShippingTerm term, ShippingTerm originalTerm)
 {
     //Need to do error checking.
     return ShippingTermDAL.UpdateShippingTerms(term, originalTerm, _connection);
 }
 public Boolean ReactivateTerm(ShippingTerm shippingTerm)
 {
     if (shippingTerm == null) throw new ArgumentException("The shipping term was null, therefore it cannot be set to active.");
     return ShippingTermDAL.ReactivateTerm(shippingTerm, _connection);
 }