/* * Author: Aaron Mill * Date: July 6, 2015 */ public static bool AddPackage(Package package) { SqlConnection connection = TravelExpertsDB.GetConnection(); string insertStatement = "INSERT Packages " + "(PkgName, PkgStartDate, PkgEndDate, PkgDesc, PkgBasePrice, PkgAgencyCommission) " + "VALUES (@PkgName, @PkgStartDate, @PkgEndDate, @PkgDesc, @PkgBasePrice, @PkgAgencyCommission)"; SqlCommand insertCommand = new SqlCommand(insertStatement, connection); insertCommand.Parameters.AddWithValue( "@PkgName", package.PkgName); insertCommand.Parameters.AddWithValue( "@PkgStartDate", package.PkgStartDate); insertCommand.Parameters.AddWithValue( "@PkgEndDate", package.PkgEndDate); insertCommand.Parameters.AddWithValue( "@PkgDesc", package.PkgDesc); insertCommand.Parameters.AddWithValue( "@PkgBasePrice", package.PkgBasePrice); insertCommand.Parameters.AddWithValue( "@PkgAgencyCommission", package.PkgAgencyCommission); try { connection.Open(); int count = insertCommand.ExecuteNonQuery(); // Inform user and return execution status if (count > 0) { // MessageBox.Show("Inserted record: " + package.PackageId); return true; } else return false; } catch (SqlException ex) { throw ex; } finally { connection.Close(); } }
//function to delete the SQL data /*Chen Code Start, Aaron helped by deleting Packages_Products_Suppliers*/ public static bool DeletePackage(Package package) { SqlConnection connection = TravelExpertsDB.GetConnection(); //connect the SQL7 string deleteForeign = "DELETE FROM Packages_Products_Suppliers " + "WHERE PackageId = @PackageId "; SqlCommand deleteForeignCommand = new SqlCommand(deleteForeign, connection); //call the SQL deleteForeign command to delete the dependent table deleteForeignCommand.Parameters.AddWithValue( "@PackageId", package.PackageId); string deleteStatement = "DELETE FROM Packages " + "WHERE PackageId = @PackageId "; SqlCommand deleteCommand = new SqlCommand(deleteStatement, connection); //call the SQL delete command to delete the data deleteCommand.Parameters.AddWithValue( "@PackageId", package.PackageId); try { connection.Open(); int count2 = deleteForeignCommand.ExecuteNonQuery(); int count = deleteCommand.ExecuteNonQuery(); if (count > 0 && count2 > 0) return true; else return false; } catch (SqlException ex) { throw ex; } finally { connection.Close(); } }
// ------------------------------------------------------------------ // Pitsini Suwandechochai // Description: Event - when user click "Add" button // ------------------------------------------------------------------ private void btnAddPkg_Click(object sender, EventArgs e) { DialogResult result; // needs returning result from "AddPackageForm" (DialogResult.OK) frmAddPackage AddPackageForm = new frmAddPackage(); // create form object result = AddPackageForm.ShowDialog(); // show "AddPackageForm" form as a dialob and get DialogResult back if (result == DialogResult.OK) // Checks if user has a successful added package { try { // calls method "GetListOfPackage()" from PackageDB and keep the result in "ListOfPackages" ListOfPackages = PackageDB.GetListOfPackage(); chosenPkgId = AddPackageForm.newJustAddId; // keeps packageID that user just added in this variable aPackage = PackageDB.GetPackageByID(chosenPkgId); // get package data from database by using packageID DisplayListOfPackage(); // display list of the package in the combo box and list box DisplayPackageAndProduct(); // display package data in the textboxes and product in the products listbox // get first package as a default lstAllPackage.SelectedIndex = lstAllPackage.Items.Count - 1; // set the last package as a selectedIndex index = lstAllPackage.SelectedIndex; // keeps the index in variable } catch (DBConcurrencyException) // number of rows affected equals zero { MessageBox.Show("Concurrency error occurred. Some changes did not happen", "Concurrency error"); } catch (SqlException ex) // SQL Server returns a warning or error { MessageBox.Show("Database error # " + ex.Number + ": " + ex.Message, ex.GetType().ToString()); } catch (Exception ex) // any other error { MessageBox.Show("Other unanticipated error # " + ex.Message, ex.GetType().ToString()); } } }
// ------------------------------------------------------------------ // Pitsini Suwandechochai // Description: reset all data // ------------------------------------------------------------------ private void ResetData() { lstAllPackage.SelectedIndex = index; aPackage = PackageDB.GetPackageByID(chosenPkgId); DisplayPackageAndProduct(); }
// ------------------------------------------------------------------ // Pitsini Suwandechochai // Description: event -- everytime user choose a package on the listbox // ------------------------------------------------------------------ private void lstAllPackage_SelectedIndexChanged(object sender, EventArgs e) { try { // make sure it has package list if (lstAllPackage.Items.Count > 0) { // split the packageID out of the list that user have been chosen, keep it in "chosenPkgId" variable string[] separators = new string[] { " --- " }; chosenPkgId = Convert.ToInt32(lstAllPackage.SelectedItem.ToString().Split(separators, StringSplitOptions.None)[0]); // get package data from database and display it aPackage = PackageDB.GetPackageByID(chosenPkgId); DisplayPackageAndProduct(); // keep the index of listbox into variable index = lstAllPackage.SelectedIndex; } } catch (DBConcurrencyException) // number of rows affected equals zero { MessageBox.Show("Concurrency error occurred. Some changes did not happen", "Concurrency error"); } catch (SqlException ex) // SQL Server returns a warning or error { MessageBox.Show("Database error # " + ex.Number + ": " + ex.Message, ex.GetType().ToString()); } catch (Exception ex) // any other error { MessageBox.Show("Other unanticipated error # " + ex.Message, ex.GetType().ToString()); } }
// ------------------------------------------------------------------ // Pitsini Suwandechochai // Description: Event - When the form is loaded // ------------------------------------------------------------------ private void frmPackage_Load(object sender, EventArgs e) { try { // calls method "GetListOfPackage()" from PackageDB and keep the result in "ListOfPackages" ListOfPackages = PackageDB.GetListOfPackage(); DisplayListOfPackage(); // display list of package in combo box and listbox txtPkgId.Focus(); // set focus on PackageId textbox // get first package as a default lstAllPackage.SelectedIndex = index; aPackage = PackageDB.GetPackageByID(chosenPkgId); DisplayPackageAndProduct(); } catch (DBConcurrencyException) // number of rows affected equals zero { MessageBox.Show("Concurrency error occurred. Some changes did not happen", "Concurrency error"); } catch (SqlException ex) // SQL Server returns a warning or error { MessageBox.Show("Database error # " + ex.Number + ": " + ex.Message, ex.GetType().ToString()); } catch (Exception ex) // any other error { MessageBox.Show("Other unanticipated error # " + ex.Message, ex.GetType().ToString()); } }
// ------------------------------------------------------------------ // Pitsini Suwandechochai // Description: event -- everytime user choose a package on the combo box // ------------------------------------------------------------------ private void cboPkgName_SelectedIndexChanged(object sender, EventArgs e) { try { // get a package info from database and display it aPackage = PackageDB.GetPackageByName(cboPkgName.SelectedItem.ToString()); DisplayPackageAndProduct(); // keeps index and PackageID into variables index = cboPkgName.SelectedIndex; chosenPkgId = aPackage.PackageId; // make a hilight on the listbox lstAllPackage.SelectedIndex = index; } catch (DBConcurrencyException) // number of rows affected equals zero { MessageBox.Show("Concurrency error occurred. Some changes did not happen", "Concurrency error"); } catch (SqlException ex) // SQL Server returns a warning or error { MessageBox.Show("Database error # " + ex.Number + ": " + ex.Message, ex.GetType().ToString()); } catch (Exception ex) // any other error { MessageBox.Show("Other unanticipated error # " + ex.Message, ex.GetType().ToString()); } }
// ------------------------------------------------------------------ // Pitsini Suwandechochai // Description: event -- when user click "Search" button // ------------------------------------------------------------------ private void btnSearch_Click(object sender, EventArgs e) { // checks if input is valid if (IsValidData()) { try { // gets package by ID textbox aPackage = PackageDB.GetPackageByID(Convert.ToInt32(txtPkgId.Text)); // make sure has package in database if (aPackage != null) DisplayPackageAndProduct(); // don't have Package that user try to search in DB else { MessageBox.Show("Package ID: " + txtPkgId.Text + " is not found in Database."); // gets ready for user to type the new data txtPkgId.Focus(); txtPkgId.SelectAll(); } } catch (DBConcurrencyException) // number of rows affected equals zero { MessageBox.Show("Concurrency error occurred. Some changes did not happen", "Concurrency error"); } catch (SqlException ex) // SQL Server returns a warning or error { MessageBox.Show("Database error # " + ex.Number + ": " + ex.Message, ex.GetType().ToString()); } catch (Exception ex) // any other error { MessageBox.Show("Other unanticipated error # " + ex.Message, ex.GetType().ToString()); } } ResetData(); // reset to the previous selected package }
/*Chen Code End*/ /* Author: Pooja Jairath * Date: 6th July 2015 * Purpose: Function to return list of PackageIds to populate combo box with Package Ids, so a package can be selected to be edited */ public static List<int> GetPackageId() { List<int> packageids = new List<int>(); SqlConnection connection = TravelExpertsDB.GetConnection(); string query = "SELECT PackageId FROM Packages"; SqlCommand command = new SqlCommand(query, connection); try { // Open db connection and run query connection.Open(); SqlDataReader reader = command.ExecuteReader(); // Read dataset fill Packages list while (reader.Read()) { Package package = new Package(); package.PackageId = (int)reader["PackageId"]; packageids.Add(package.PackageId); } return packageids; } catch (SqlException ex) { throw ex; } finally { connection.Close(); } }
// Event - When the form is loaded private void frmModifyPackage_Load(object sender, EventArgs e) { if (selectedPkgId != 0) { try { // get package data from DB ChosenPackage = PackageDB.GetPackageByID(selectedPkgId); DisplayPackage(); // shows package data in textboxes } catch (DBConcurrencyException) // number of rows affected equals zero { MessageBox.Show("Concurrency error occurred. Some changes did not happen", "Concurrency error"); } catch (SqlException ex) // SQL Server returns a warning or error { MessageBox.Show("Database error # " + ex.Number + ": " + ex.Message, ex.GetType().ToString()); } catch (Exception ex) // any other error { MessageBox.Show("Other unanticipated error # " + ex.Message, ex.GetType().ToString()); } } }
// ------------------------------------------------------------------ // Pitsini Suwandechochai // Description: Updating method for package data // It will return "true" if updating is successful. Otherwise will return "false" // ------------------------------------------------------------------ public static bool UpdatePackage(int packageID, Package newPackage) { SqlConnection connection = TravelExpertsDB.GetConnection(); // update statement string updateStatement = "UPDATE Packages SET PkgName = @NewName, " + "PkgStartDate = @NewPkgStartdate, " + "PkgEndDate = @NewPkgEndDate, " + "PkgDesc = @NewPkgDesc, " + "PkgBasePrice = @NewPkgBasePrice, " + "PkgAgencyCommission = @NewPkgAgencyCommission " + "Where PackageId = @PackageId "; SqlCommand updateCommand = new SqlCommand(updateStatement, connection); // variables that user would like to update updateCommand.Parameters.AddWithValue("@NewName", newPackage.PkgName); updateCommand.Parameters.AddWithValue("@NewPkgStartdate", newPackage.PkgStartDate); updateCommand.Parameters.AddWithValue("@NewPkgEndDate", newPackage.PkgEndDate); updateCommand.Parameters.AddWithValue("@NewPkgDesc", newPackage.PkgDesc); updateCommand.Parameters.AddWithValue("@NewPkgBasePrice", newPackage.PkgBasePrice); updateCommand.Parameters.AddWithValue("@NewPkgAgencyCommission", newPackage.PkgAgencyCommission); updateCommand.Parameters.AddWithValue("@PackageId", packageID); try { connection.Open(); // run command int count = updateCommand.ExecuteNonQuery(); if (count > 0) return true; else return false; } catch (SqlException ex) // SQL Server returns a warning or error { throw ex; } finally { connection.Close(); } }
// ------------------------------------------------------------------ // Pitsini // Insert method for package data // It will return "true" if inserting is successful. Otherwise will return "false" // ------------------------------------------------------------------ public static int InsertPackage(Package newPackage) { SqlConnection connection = TravelExpertsDB.GetConnection(); // insert statement string insertStatement = "INSERT INTO Packages(PkgName, PkgStartDate, PkgEndDate, " + "PkgDesc, PkgBasePrice, PkgAgencyCommission) " + "VALUES (@newName, @newSDate,@newEDate, @newDesc, @newBasePrice, @newCommission)"; SqlCommand insertCommand = new SqlCommand(insertStatement, connection); // variables that user would like to insert insertCommand.Parameters.AddWithValue("@newName", newPackage.PkgName); insertCommand.Parameters.AddWithValue("@newSDate", newPackage.PkgStartDate); insertCommand.Parameters.AddWithValue("@newEDate", newPackage.PkgEndDate); insertCommand.Parameters.AddWithValue("@newDesc", newPackage.PkgDesc); insertCommand.Parameters.AddWithValue("@newBasePrice", newPackage.PkgBasePrice); insertCommand.Parameters.AddWithValue("@newCommission", newPackage.PkgAgencyCommission); try { connection.Open(); // run command int count = insertCommand.ExecuteNonQuery(); if (count > 0) { Package pkg = new Package(); pkg = GetPackageByName(newPackage.PkgName); return pkg.PackageId; } else return 0; } catch (SqlException ex) { throw ex; } finally { connection.Close(); } }
// ------------------------------------------------------------------ // Pitsini Suwandechochai // Description: use PackageName to get one package info from DB // Method to used: GetPackageByName(PackageName) // ------------------------------------------------------------------ public static Package GetPackageByName(string PackageName) { SqlConnection connectDB = TravelExpertsDB.GetConnection(); // select statement string selectStatement = "SELECT * " + "FROM Packages " + "WHERE PkgName = @PackageName "; SqlCommand selectCommand = new SqlCommand(selectStatement, connectDB); // @PackageName is a variable that we pass the value from textbox selectCommand.Parameters.AddWithValue("@PackageName", PackageName); // executes commmand try { connectDB.Open(); SqlDataReader pkgReader = selectCommand.ExecuteReader(CommandBehavior.SingleRow); Package package = new Package(); if (pkgReader.Read()) // if geting a row successful { // retrive data from data reader to the object package.PackageId = (int)pkgReader["PackageId"]; package.PkgName = pkgReader["PkgName"].ToString(); package.PkgStartDate = (DateTime)pkgReader["PkgStartDate"]; package.PkgEndDate = (DateTime)pkgReader["PkgEndDate"]; package.PkgDesc = pkgReader["PkgDesc"].ToString(); package.PkgBasePrice = (decimal)pkgReader["PkgBasePrice"]; package.PkgAgencyCommission = (decimal)pkgReader["PkgAgencyCommission"]; List<Product> ProductList = GetListOfProduct(package.PackageId); return package; } else // if coun't find data in DB { return null; } } catch (SqlException ex) // SQL Server returns a warning or error { throw ex; } finally { connectDB.Close(); } }
/* * Author: Aaron Mill * Date: July 6, 2015 */ public static bool UpdatePackage(Package package) { SqlConnection connection = TravelExpertsDB.GetConnection(); string updateStatement = "UPDATE Packages SET " + "PkgName = @PkgName, " + "PkgStartDate = @PkgStartDate, " + "PkgEndDate = @PkgEndDate, " + "PkgDesc = @PkgDesc, " + "PkgBasePrice = @PkgBasePrice, " + "PkgAgencyCommission = @PkgAgencyCommission " + "WHERE PackageId = @PackageId"; SqlCommand updateCommand = new SqlCommand(updateStatement, connection); updateCommand.Parameters.AddWithValue( "@PkgName", package.PkgName); updateCommand.Parameters.AddWithValue( "@PkgStartDate", package.PkgStartDate); updateCommand.Parameters.AddWithValue( "@PkgEndDate", package.PkgEndDate); updateCommand.Parameters.AddWithValue( "@PkgDesc", package.PkgDesc); updateCommand.Parameters.AddWithValue( "@PkgBasePrice", package.PkgBasePrice); updateCommand.Parameters.AddWithValue( "@PkgAgencyCommission", package.PkgAgencyCommission); updateCommand.Parameters.AddWithValue( "@PackageId", package.PackageId); try { connection.Open(); int count = updateCommand.ExecuteNonQuery(); if (count > 0) return true; else return false; } catch (SqlException ex) { throw ex; } finally { connection.Close(); } }
/*Chen Code Start*/ public static List<Package> GetPackages(string filter) { List<Package> packages = new List<Package>(); SqlConnection connection = TravelExpertsDB.GetConnection(); string query = "SELECT PackageId, PkgName, PkgStartDate, PkgEndDate, PkgDesc, " + "PkgBasePrice, PkgAgencyCommission " + " FROM Packages " + " WHERE PackageId > -1 " + filter; SqlCommand command = new SqlCommand(query, connection); try { // Open db connection and run query connection.Open(); SqlDataReader reader = command.ExecuteReader(); // Read dataset fill Packages list while (reader.Read()) { Package package = new Package(); package.PackageId = (int)reader["PackageId"]; package.PkgName = reader["PkgName"].ToString(); package.PkgStartDate = (DateTime)reader["PkgStartDate"]; package.PkgEndDate = (DateTime)reader["PkgEndDate"]; package.PkgDesc = reader["PkgDesc"].ToString(); package.PkgBasePrice = Convert.ToDouble(reader["PkgBasePrice"]); package.PkgAgencyCommission = Convert.ToDouble(reader["PkgAgencyCommission"]); packages.Add(package); } return packages; } catch (SqlException ex) { throw ex; } finally { connection.Close(); } }
// ------------------------------------------------------------------ // Paul Teixiera // ------------------------------------------------------------------ private void btnEditProduct_Click(object sender, EventArgs e) { //opens editproducts and sends back list DialogResult result; frmProductInPackage ProductInPackageForm = new frmProductInPackage(); if (txtPkgId.Text.Length>0) //is there any other checks for this? can we make pkgID nullable type? { ProductInPackageForm.PkgId = Convert.ToInt32(txtPkgId.Text); //this needs checking if I am doing validation on pkgform result = ProductInPackageForm.ShowDialog(); if (result == DialogResult.OK) { //clear and reload functions aPackage = PackageDB.GetPackageByID(chosenPkgId); DisplayPackageAndProduct(); } } else MessageBox.Show("Package not selected"); }
// ------------------------------------------------------------------ // Pitsini Suwandechochai // Description: Event - when user click "Save" button // ------------------------------------------------------------------ private void btnSave_Click(object sender, EventArgs e) { frmModifyPackage modForm = new frmModifyPackage(); // validate data if (IsValidData()) { try { // prepare package data into the new package object Package newPackage = new Package(); newPackage.PkgName = txtPkgName.Text; newPackage.PkgStartDate = Convert.ToDateTime(dtpStartDate.Text); newPackage.PkgEndDate = Convert.ToDateTime(dtpEndDate.Text); newPackage.PkgDesc = rtxtDesc.Text; newPackage.PkgBasePrice = Convert.ToDecimal(txtBasePrice.Text); newPackage.PkgAgencyCommission = Convert.ToDecimal(txtAgencyCommission.Text); // insert package object into database newJustAddId = PackageDB.InsertPackage(newPackage); // if inserting is fail if (newJustAddId == 0) { MessageBox.Show("Somthing went wrong with Database. " + "Please check with your Administrator.", "Database Error"); this.DialogResult = DialogResult.Retry; } // inserting is success else { MessageBox.Show("Add package is Successful!", "Alert"); this.DialogResult = DialogResult.OK; this.Close(); } } catch (DBConcurrencyException) // number of rows affected equals zero { MessageBox.Show("Concurrency error occurred. Some changes did not happen", "Concurrency error"); } catch (SqlException ex) // SQL Server returns a warning or error { MessageBox.Show("Database error # " + ex.Number + ": " + ex.Message, ex.GetType().ToString()); } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } }
// event -- when user click "Modify" button private void btnModPkg_Click(object sender, EventArgs e) { // check if user have selected a package that wants to modify if (chosenPkgId != 0) { // send packageId to modify form ModifyPackageForm.selectedPkgId = chosenPkgId; // show Modify form DialogResult result; result = ModifyPackageForm.ShowDialog(); // if successful update data if (result == DialogResult.OK) { chosenPkgId = ModifyPackageForm.selectedPkgId; // keep present packageID ModifyPackageForm.selectedPkgId = 0; // reset variable // get the list of packages data from DB and display ListOfPackages = PackageDB.GetListOfPackage(); DisplayListOfPackage(); txtPkgId.Focus(); // get package info and display lstAllPackage.SelectedIndex = index; aPackage = PackageDB.GetPackageByID(chosenPkgId); DisplayPackageAndProduct(); } } // show message that user have to choose package first else MessageBox.Show("Please select package before modify."); }
// Event - when user click "Save" button private void btnSave_Click(object sender, EventArgs e) { // validate data if (IsValidData()) { try { // prepare package data into the new package object Package newPackage = new Package(selectedPkgId, txtPkgName.Text, dtpStartDate.Value, dtpEndDate.Value, rtxtDesc.Text, decimal.Parse(txtBasePrice.Text, NumberStyles.Currency, CultureInfo.CurrentCulture.NumberFormat), decimal.Parse(txtAgencyCommission.Text, NumberStyles.Currency, CultureInfo.CurrentCulture.NumberFormat)); // if cannot update data. It will show an error if (!PackageDB.UpdatePackage(selectedPkgId, newPackage)) { MessageBox.Show("Another user has updated or " + "deleted that customer.", "Database Error"); this.DialogResult = DialogResult.Retry; } else { // if updating is successful MessageBox.Show("Package has been updated!!!"); this.DialogResult = DialogResult.OK; this.Close(); } } catch (DBConcurrencyException) // number of rows affected equals zero { MessageBox.Show("Concurrency error occurred. Some changes did not happen", "Concurrency error"); } catch (SqlException ex) // SQL Server returns a warning or error { MessageBox.Show("Database error # " + ex.Number + ": " + ex.Message, ex.GetType().ToString()); } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } }
/* Author: Pooja Jairath * Date: 6th July 2015 * Purpose: Function to retrieve package data based on PackageId from table Packages, to make it available to be edited */ public static Package GetPackage(int packageid) { SqlConnection connection = TravelExpertsDB.GetConnection(); string selectStatement = "SELECT * from Packages WHERE PackageId = @PackageId "; SqlCommand selectCommand = new SqlCommand(selectStatement, connection); selectCommand.Parameters.AddWithValue("@PackageId", packageid); try { connection.Open(); SqlDataReader packReader = selectCommand.ExecuteReader(CommandBehavior.SingleRow); if (packReader.Read()) { Package package = new Package(); package.PackageId = (int)packReader["PackageId"]; package.PkgName = packReader["PkgName"].ToString(); package.PkgStartDate = (DateTime)packReader["PkgStartDate"]; package.PkgEndDate = (DateTime)packReader["PkgEndDate"]; package.PkgDesc = packReader["PkgDesc"].ToString(); package.PkgBasePrice = Convert.ToDouble(packReader["PkgBasePrice"]); package.PkgAgencyCommission = Convert.ToDouble(packReader["PkgAgencyCommission"]); return package; } else { return null; } } catch (SqlException ex) { throw ex; } finally { connection.Close(); } }