/// <summary> /// Action when the user clicks the 'Add' button. Attempts to Check if it is a car or boat object /// disables the interaction, inserts into the database. If it fails put the error on the respected field /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnAdd_Click(object sender, RoutedEventArgs e) { try { //Check if txtMileage is visable, if it is the object should be a car. if (this.txtMileage.Visibility == Visibility.Visible) { //Create a car object var carObject = new Car(this.txtManufacturer.Text, this.txtModel.Text, this.txtProductYear.Text, this.txtMileage.Text); //Disable the interaction of the fields disableInteraction(); //Attempt to insert the car into the database DBL.InsertCar(carObject); //Update the status text lblStatusText.Content = "Inserted a car"; } else { //Create the boat object var boatObject = new Boat(this.txtManufacturer.Text, this.txtModel.Text, this.txtProductYear.Text); //Disable the user interaction disableInteraction(); //Attemp to insert the boat object DBL.InsertBoat(boatObject); //Update the status text lblStatusText.Content = "Inserted a boat"; } // Set focus to the Clear button to facilitate data entry. btnClear.IsEnabled = true; btnClear.Focus(); } //Catch any errors catch (ArgumentException ex) { //Each if: // Check the parameter name // If the parameter name matches, set the content to the error message, // Highlight the textbox if (ex.ParamName == "Manufacturer") { lblManufacturerError.Content = "Manufacturer entry error! " + ex.Message; HighlightTextbox(txtManufacturer); } else if (ex.ParamName == "ProductYear") { lblProductYearError.Content = "Product Year entry error! " + ex.Message; HighlightTextbox(txtProductYear); } else if (ex.ParamName == "Model") { lblModelError.Content = "Model entry error! " + ex.Message; HighlightTextbox(txtModel); } else if (ex.ParamName == "Mileage") { lblMileageError.Content = "Mileage entry error! " + ex.Message; HighlightTextbox(txtMileage); } } catch (Exception ex) { MessageBox.Show("Critical Error " + ex.Message); } }
/// <summary> /// Calls the DBL file (Database helper methods) to get the list of vehicles in the database /// sets the dataview to the result of the fetch /// </summary> public void ReloadData() { datagridVehicles.ItemsSource = DBL.GetVehicleList().DefaultView; }