private void btnSave_Click(object sender, EventArgs e) { if (cmbSelect.SelectedValue == null) { MessageBox.Show("Please select an owner to add."); } else if (AllValidInputs() && IsValidYear(txtYear.Text)) { //add new vehicle Vehicle newVehicle = new Vehicle(); newVehicle.make = txtMake.Text; newVehicle.model = txtModel.Text; newVehicle.VIN = txtVIN.Text; newVehicle.year = txtYear.Text; int vehicleID = vehControl.AddVehicle(newVehicle); int ownerID = int.Parse(cmbSelect.SelectedValue.ToString()); OwnerVehicle newOwnerVehicle = new OwnerVehicle(); newOwnerVehicle.ownerID = ownerID; newOwnerVehicle.vehicleID = vehicleID; int addStatus = ownVehControl.AddOwnerVehicle(newOwnerVehicle); if (ownerID != 0 && vehicleID != 0 && addStatus == 0) { MessageBox.Show("You have successfully created a new vehicle!"); this.Close(); } else { MessageBox.Show("There was an error while saving, please try again."); } } }
private void btnSave_Click(object sender, EventArgs e) { if (cmbSelect.SelectedValue == null || cmbCustomers.SelectedValue == null) { MessageBox.Show("Please make sure to select both a vehicle and owner."); } else { int vehicleID = int.Parse(cmbSelect.SelectedValue.ToString()); int ownerID = int.Parse(cmbCustomers.SelectedValue.ToString()); //link vehicle to owner (add entry to OwnerVehicle table) OwnerVehicle newOwnerVehicle = new OwnerVehicle(); newOwnerVehicle.ownerID = ownerID; newOwnerVehicle.vehicleID = vehicleID; int status = ownVehControl.AddOwnerVehicle(newOwnerVehicle); if (status == 0) { MessageBox.Show("You have successfully added a vehicle to an owner!"); this.Close(); } else { MessageBox.Show("There was an error while saving, please try again."); } } }
/// <summary> /// Associates an Owner with a Vehicle /// </summary> /// <param name="newOwnerVehicle"></param> /// <returns></returns> public static int CreateOwnerVehicle(OwnerVehicle newOwnerVehicle) { int exitStatus = 0; SqlConnection connection = DBConnection.GetConnection(); string insertStatement = "INSERT ownerVehicle " + "(ownerID, vehicleID)" + "VALUES" + "(@ownerID, @vehicleID)"; SqlCommand insertCommand = new SqlCommand(insertStatement, connection); insertCommand.Parameters.AddWithValue("@ownerID", newOwnerVehicle.ownerID); insertCommand.Parameters.AddWithValue("@vehicleID", newOwnerVehicle.vehicleID); try { connection.Open(); insertCommand.ExecuteNonQuery(); exitStatus = 0; } catch (SqlException ex) { exitStatus = 1; StringBuilder errorDetails = new StringBuilder(); for (int i = 0; i < ex.Errors.Count; i++) { errorDetails.Append("Index #" + i + "\n" + "Message: " + ex.Errors[i].Message + "\n" + "Error Number: " + ex.Errors[i].Number + "\n" + "Procedure: " + ex.Errors[i].Procedure + "\n"); } MessageBox.Show(errorDetails.ToString(), "SQL Exception"); } finally { connection.Close(); } return(exitStatus); }
/// <summary> /// Disassociates an Owner with a Vehicle /// </summary> /// <param name="ownerVehicle"></param> /// <returns></returns> public static int RemoveOwnerVehicle(OwnerVehicle ownerVehicle) { int exitStatus = 0; SqlConnection connection = DBConnection.GetConnection(); string removeStatement = "DELETE FROM ownerVehicle " + "WHERE (ownerID = @ownerID AND vehicleID = @vehicleID)"; SqlCommand removeCommand = new SqlCommand(removeStatement, connection); removeCommand.Parameters.AddWithValue("@ownerID", ownerVehicle.ownerID); removeCommand.Parameters.AddWithValue("@vehicleID", ownerVehicle.vehicleID); try { connection.Open(); removeCommand.ExecuteNonQuery(); exitStatus = 0; } catch (SqlException ex) { exitStatus = 1; StringBuilder errorDetails = new StringBuilder(); for (int i = 0; i < ex.Errors.Count; i++) { errorDetails.Append("Index #" + i + "\n" + "Message: " + ex.Errors[i].Message + "\n" + "Error Number: " + ex.Errors[i].Number + "\n" + "Procedure: " + ex.Errors[i].Procedure + "\n"); } MessageBox.Show(errorDetails.ToString(), "SQL Exception"); } finally { connection.Close(); } return(exitStatus); }
public int AddOwnerVehicle(OwnerVehicle newOwnerVehicle) { return(OwnerVehicleDAL.CreateOwnerVehicle(newOwnerVehicle)); }
public int RemoveOwnerVehicle(OwnerVehicle oldOwnerVehicle) { return(OwnerVehicleDAL.RemoveOwnerVehicle(oldOwnerVehicle)); }
private void btnSave_Click(object sender, EventArgs e) { if (AllValidInputs() && IsValidEmail(txtEmail.Text) && ((panelNew.Visible == true && IsValidYear(txtYear.Text) && AllNewCarValidInputs()) || (panelExist.Visible == true && cmbSelect.SelectedItem != null))) { int vehicleID = 0; int ownerID = 0; int status = 1; //add new owner Owner newOwner = new Owner(); string formattedPhoneNumber = txtPhoneAreaCode.Text + "-" + txtPhoneFirstThreeDigits.Text + "-" + txtPhoneLastFourDigits.Text; newOwner.firstName = txtFirstName.Text; newOwner.lastName = txtLastName.Text; newOwner.streetAddress = txtStreet.Text; newOwner.city = txtCity.Text; newOwner.state = cmbState.Text; newOwner.zip = txtZip.Text; newOwner.phoneNumber = formattedPhoneNumber; newOwner.emailAddress = txtEmail.Text; if (panelNew.Visible == true) { //add new vehicle Vehicle newVehicle = new Vehicle(); newVehicle.make = txtMake.Text; newVehicle.model = txtModel.Text; newVehicle.VIN = txtVIN.Text; newVehicle.year = txtYear.Text; vehicleID = vehControl.AddVehicle(newVehicle); } else { vehicleID = int.Parse(cmbSelect.SelectedValue.ToString()); } ownerID = ownControl.AddOwner(newOwner); //link vehicle to owner (add entry to OwnerVehicle table) OwnerVehicle newOwnerVehicle = new OwnerVehicle(); newOwnerVehicle.ownerID = ownerID; newOwnerVehicle.vehicleID = vehicleID; status = ownVehControl.AddOwnerVehicle(newOwnerVehicle); if (ownerID != 0 && vehicleID != 0 && status == 0) { MessageBox.Show("You have successfully created a new customer!"); this.Close(); } else { MessageBox.Show("There was an error while saving, please try again."); } } }