Example #1
0
        private void Handle_GetVehiclesButton_Click(object sender, RoutedEventArgs e)
        {
            //((MainWindow)Application.Current.MainWindow).VehicleListBox.ItemsSource = SqliteDataAccess.GetVehiclesById(VehicleId);
            try
            {
                //create new vehicle
                Vehicle vehicle = new Vehicle
                {
                    Make  = MakeBox.Text.ToUpper(),
                    Model = ModelBox.Text.ToUpper()
                };

                //We cannot parse an empty string to an int, so we must check these cases
                //set Vehicle Id and year to default 0 if empty
                if (string.IsNullOrEmpty(IdBox.Text) && string.IsNullOrEmpty(YearBox.Text))
                {
                    vehicle.Id   = 0;
                    vehicle.Year = 0;
                }

                //check if year is empty
                if (!string.IsNullOrEmpty(IdBox.Text) && string.IsNullOrEmpty(YearBox.Text))
                {
                    vehicle.Id   = Convert.ToInt32(IdBox.Text);
                    vehicle.Year = 0;
                }
                //check if Id is empty
                if (string.IsNullOrEmpty(IdBox.Text) && !string.IsNullOrEmpty(YearBox.Text))
                {
                    vehicle.Id   = 0;
                    vehicle.Year = Convert.ToInt32(YearBox.Text);
                }

                //check uf year and id are empty
                if (!string.IsNullOrEmpty(IdBox.Text) && !string.IsNullOrEmpty(YearBox.Text))
                {
                    vehicle.Id   = Convert.ToInt32(IdBox.Text);
                    vehicle.Year = Convert.ToInt32(YearBox.Text);
                }

                //Pass instance of Main Window to update ListBox
                //Pass to SQLite Vehicle database access
                ((MainWindow)Application.Current.MainWindow).VehicleListBox.ItemsSource = SqliteDataAccess.FilterVehicles(vehicle);

                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error Occured");
            }
        }