public static bool AddShippingVendor(ShippingVendor shippingVendor, SqlConnection myConnection)
        {
            myConnection = myConnection ?? GetInventoryDbConnection();
            try
            {
                var mySqlCommand = new SqlCommand("proc_InsertIntoShippingVendors", myConnection)
                {
                    CommandType = CommandType.StoredProcedure
                };
                mySqlCommand.Parameters.AddWithValue("@name", shippingVendor.Name);
                mySqlCommand.Parameters.AddWithValue("@address", shippingVendor.Address);
                mySqlCommand.Parameters.AddWithValue("@city", shippingVendor.City);
                mySqlCommand.Parameters.AddWithValue("@state", shippingVendor.State);
                mySqlCommand.Parameters.AddWithValue("@country", shippingVendor.Country);
                mySqlCommand.Parameters.AddWithValue("@zip", shippingVendor.Zip);
                mySqlCommand.Parameters.AddWithValue("@phone", shippingVendor.Phone);
                mySqlCommand.Parameters.AddWithValue("@contact", shippingVendor.Contact);
                mySqlCommand.Parameters.AddWithValue("@contactEmail", shippingVendor.ContactEmail);
                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 FrmUpdateShippingVendor(ShippingVendor vendor, AccessToken _myAccessToken)
 {
     InitializeComponent();
     var RoleAccess = new RoleAccess(_myAccessToken, this);
     _myShippingVendorManager = new ShippingVendorManager();
     _originalVendor = vendor;
     this.Text = "Shipping Vendor ID: " + vendor.Id;
     PopulateCountryCombo();
     PopulateStateCombo();
     txtAddress.Text = vendor.Address;
     txtCity.Text = vendor.City;
     txtContact.Text = vendor.Contact;
     comboCountry.SelectedItem = vendor.Country;
     txtEmail.Text = vendor.ContactEmail;
     txtName.Text = vendor.Name;
     txtPhone.Text = vendor.Phone;
     txtZip.Text = vendor.Zip;
     comboState.SelectedItem = vendor.State;
 }
        public static Boolean DeactivateVendor(ShippingVendor vendor, SqlConnection connection)
        {
            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_DeactivateShippingVendor", conn);
                sqlCmd.CommandType = CommandType.StoredProcedure;
                sqlCmd.Parameters.AddWithValue("@ShippingVendorId", vendor.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 btnAdd_Click(object sender, EventArgs e)
        {
            bool validShippingVendor = true;
            string errorMessage = "Please correct the following errors:\n";

            ShippingVendor newVendor;
            if (txtEmail.TextLength > 50)
            {
                validShippingVendor = false;
                errorMessage += "\nThe contact email must be less than 50 characters.";
                txtEmail.Focus();
            }
            if (Validation.IsNullOrEmpty(txtContact.Text) || Validation.IsBlank(txtContact.Text))
            {
                validShippingVendor = false;
                errorMessage += "\nYou must enter a primary contact.";
                txtContact.Focus();
            }
            if (Validation.IsNullOrEmpty(txtPhone.Text) || Validation.IsBlank(txtPhone.Text))
            {
                validShippingVendor = false;
                errorMessage += "\nYou must enter a phone.";
                txtPhone.Focus();
            }
            if (Validation.IsNullOrEmpty(txtZip.Text) || Validation.IsBlank(txtZip.Text))
            {
                validShippingVendor = false;
                errorMessage += "\nYou must enter a zip code.";
                txtZip.Focus();
            }
            else if (!Validation.ValidateZip(txtZip.Text, comboState.SelectedItem.ToString()))
            {
                validShippingVendor = false;
                errorMessage += "\nZip code " + txtZip.Text + " is not a valid zip code for the state of " + comboState.SelectedItem.ToString() + ".";
                txtZip.Focus();
            }
            if (Validation.IsNullOrEmpty(txtCity.Text) || Validation.IsBlank(txtCity.Text))
            {
                validShippingVendor = false;
                errorMessage += "\nYou must enter a city.";
                txtCity.Focus();
            }
            if (txtCity.TextLength > 25)
            {
                validShippingVendor = false;
                errorMessage += "\nThe city must be less than 25 characters.";
                txtCity.Focus();
            }
            if (Validation.IsNullOrEmpty(txtAddress.Text) || Validation.IsBlank(txtAddress.Text))
            {
                validShippingVendor = false;
                errorMessage += "\nYou must enter an address.";
                txtAddress.Focus();
            }
            if (txtAddress.TextLength > 50)
            {
                validShippingVendor = false;
                errorMessage += "\nThe address must be less than 50 characters.";
                txtAddress.Focus();
            }
            if (Validation.IsNullOrEmpty(txtName.Text) || Validation.IsBlank(txtName.Text))
            {
                validShippingVendor = false;
                errorMessage += "\nYou must enter a vendor name";
                txtName.Focus();
            }
            if (txtName.TextLength > 50)
            {
                validShippingVendor = false;
                errorMessage += "\nThe vendor name must be less than 50 characters.";
                txtName.Focus();
            }

            if(validShippingVendor)
            {
                newVendor = new ShippingVendor();
                newVendor.Name = txtName.Text;
                newVendor.Address = txtAddress.Text;
                newVendor.City = txtCity.Text;
                newVendor.Country = comboCountry.SelectedItem.ToString();
                newVendor.Contact = txtContact.Text;
                newVendor.ContactEmail = txtEmail.Text;
                newVendor.Phone = txtPhone.Text;
                newVendor.Zip = txtZip.Text;
                //newVendor.State = (String)comboState.SelectedItem;
                newVendor.State = comboState.SelectedItem.ToString();
                if (_myShippingVendorManager.Insert(newVendor))
                {
                    this.DialogResult = DialogResult.OK;
                    MessageBox.Show("The vendor was created and added.");
                    ClearFields();
                }
                else
                {
                    MessageBox.Show("The vendor was not created.");
                }
            }
            else
            {
                MessageBox.Show(errorMessage);
            }
        }
 public bool Insert(ShippingVendor vendor)
 {
     //Need to do error checking... Try/Catch.
     return ShippingVendorDAL.AddShippingVendor(vendor, _connection);
 }
 public Boolean DeleteVendor(ShippingVendor shippingVendor)
 {
     if (shippingVendor == null) throw new ArgumentException("The shipping vendor was null, therefore it could not be deleted.");
     return ShippingVendorDAL.DeleteVendor(shippingVendor, _connection);
 }
 public Boolean DeactivateVendor(ShippingVendor shippingVendor)
 {
     if (shippingVendor == null) throw new ArgumentException("The shipping vendor was null, therefore it cannot be set to inactive.");
     return ShippingVendorDAL.DeactivateVendor(shippingVendor, _connection);
 }
 public bool Update(ShippingVendor vendor, ShippingVendor originalVendor)
 {
     //Need to do error checking... Try/Catch.
     return ShippingVendorDAL.UpdateShippingVendor(vendor, originalVendor, _connection);
 }
        public static List<ShippingVendor> GetVendorsByActive(Boolean activeState, SqlConnection myConnection)
        {
            var shippingVendorList = new List<ShippingVendor>();
            myConnection = myConnection ?? GetInventoryDbConnection();
            try
            {
                var mySqlCommand = new SqlCommand("proc_GetShippingVendorsByActive", myConnection)
                {
                    CommandType = CommandType.StoredProcedure
                };
                mySqlCommand.Parameters.AddWithValue("@Active", activeState ? 1 : 0);
                myConnection.Open();
                var mySqlReader = mySqlCommand.ExecuteReader();
                if (mySqlReader.HasRows)
                {
                    while (mySqlReader.Read())
                    {
                        var shippingVendor = new ShippingVendor(mySqlReader.GetInt32(0))
                        {
                            Name = mySqlReader.GetString(1),
                            Address = mySqlReader.GetString(2),
                            City = mySqlReader.GetString(3),
                            State = mySqlReader.GetString(4),
                            Country = mySqlReader.GetString(5),
                            Zip = mySqlReader.GetString(6),
                            Phone = mySqlReader.GetString(7),
                            Contact = mySqlReader.GetString(8),
                            ContactEmail = mySqlReader.GetString(9),
                            Active = mySqlReader.GetBoolean(10)
                        };

                        //Add item to list
                        shippingVendorList.Add(shippingVendor);
                    }
                }
                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 shippingVendorList;
        }