private void AddVoilator_Click(object sender, RoutedEventArgs e) { AddViolator addViolator = new AddViolator(); addViolator.Show(); this.Close(); }
private void SaveChanges(object sender, RoutedEventArgs e) { if (criterion.Text != string.Empty && searchCriterion.Text != string.Empty) { if (criterion.Text == "Марка автомобиля") { if (changingCriterion.Text != CheckCar.CheckCarModel(changingCriterion.Text)) { MessageBox.Show(CheckCar.CheckCarModel(changingCriterion.Text)); return; } string query = @"SELECT CarModelCode FROM CarModel WHERE CarModelName = '" + changingCriterion.Text + "'"; int carModelCode = 0; SqlConnection sqlConnection = new SqlConnection(connectionString); sqlConnection.Open(); SqlCommand sqlCommand = new SqlCommand(query, sqlConnection); SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); if (sqlDataReader.HasRows) { while (sqlDataReader.Read()) { carModelCode = Convert.ToInt32(sqlDataReader[0].ToString()); } } else { MessageBox.Show("Такой марки автомобиля нет в базе."); return; } sqlDataReader.Close(); sqlConnection.Close(); using (SqlConnection updateCarModel = new SqlConnection(connectionString)) using (SqlCommand lastCommnd = updateCarModel.CreateCommand()) { lastCommnd.CommandText = "UPDATE ViolatorCar SET CarModelCode = @carModel WHERE CarCode = @code"; lastCommnd.Parameters.AddWithValue("@carModel", carModelCode); lastCommnd.Parameters.AddWithValue("@code", Convert.ToInt32(searchCriterion.Text)); updateCarModel.Open(); lastCommnd.ExecuteNonQuery(); updateCarModel.Close(); } } else if (criterion.Text == "Цвет автомобиля") { if (changingCriterion.Text != CheckCar.CheckCarColor(changingCriterion.Text)) { MessageBox.Show(CheckCar.CheckCarColor(changingCriterion.Text)); return; } string query = @"SELECT ColorCode FROM Color WHERE ColorName = '" + changingCriterion.Text + "'"; int colorCode = 0; SqlConnection sqlConnection = new SqlConnection(connectionString); sqlConnection.Open(); SqlCommand sqlCommand = new SqlCommand(query, sqlConnection); SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); if (sqlDataReader.HasRows) { while (sqlDataReader.Read()) { colorCode = Convert.ToInt32(sqlDataReader[0].ToString()); } } else { MessageBox.Show("Такого цвета нет в базе."); return; } sqlDataReader.Close(); sqlConnection.Close(); using (SqlConnection updateColor = new SqlConnection(connectionString)) using (SqlCommand lastCommnd = updateColor.CreateCommand()) { lastCommnd.CommandText = "UPDATE ViolatorCar SET ColorCode = @color WHERE CarCode = @code"; lastCommnd.Parameters.AddWithValue("@color", colorCode); lastCommnd.Parameters.AddWithValue("@code", Convert.ToInt32(searchCriterion.Text)); updateColor.Open(); lastCommnd.ExecuteNonQuery(); updateColor.Close(); } } else if (criterion.Text == "Номер автомобиля") { if (changingCriterion.Text != CheckCar.CheckCarStateNumber(changingCriterion.Text)) { MessageBox.Show(CheckCar.CheckCarStateNumber(changingCriterion.Text)); return; } SqlConnection sqlConnection = new SqlConnection(connectionString); sqlConnection.Open(); string findReplays = "SELECT CarCode FROM ViolatorCar WHERE CarStatetNumber = '" + changingCriterion.Text + "'"; SqlCommand sqlCommand3 = new SqlCommand(findReplays, sqlConnection); SqlDataReader sqlDataReader = sqlCommand3.ExecuteReader(); if (sqlDataReader.HasRows) { MessageBox.Show("Вы ввели уже занятый номер автомобиля."); return; } else { using (SqlConnection updateStateNumber = new SqlConnection(connectionString)) using (SqlCommand lastCommnd = updateStateNumber.CreateCommand()) { lastCommnd.CommandText = "UPDATE ViolatorCar SET CarStatetNumber = @number WHERE CarCode = @code"; lastCommnd.Parameters.AddWithValue("@number", changingCriterion.Text); lastCommnd.Parameters.AddWithValue("@code", Convert.ToInt32(searchCriterion.Text)); updateStateNumber.Open(); lastCommnd.ExecuteNonQuery(); updateStateNumber.Close(); } } sqlConnection.Close(); } else if (criterion.Text == "Код владельца") { if (changingCriterion.Text == string.Empty) { MessageBox.Show("Вы не указали код нарушителя."); } else { char[] violatorCodeArray = changingCriterion.Text.ToCharArray(); for (int i = 0; i < violatorCodeArray.Length; i++) { if (!char.IsDigit(violatorCodeArray[i])) { MessageBox.Show("Вы указали в коде недопустимые символы."); return; } } } string query = @"SELECT ViolatorCode FROM Violator WHERE ViolatorCode = '" + changingCriterion.Text + "'"; int violatorCode = 0; SqlConnection sqlConnection = new SqlConnection(connectionString); sqlConnection.Open(); SqlCommand sqlCommand = new SqlCommand(query, sqlConnection); SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); if (sqlDataReader.HasRows) { while (sqlDataReader.Read()) { violatorCode = Convert.ToInt32(sqlDataReader[0].ToString()); } } else { MessageBoxResult meboxResult = MessageBox.Show("Такого нарушителя нет в списке. Можете добавить нового нарушителя.", "Предупреждение", MessageBoxButton.YesNo); if (meboxResult == MessageBoxResult.Yes) { AddViolator addViolator = new AddViolator(); addViolator.Show(); this.Close(); } return; } sqlDataReader.Close(); sqlConnection.Close(); using (SqlConnection updateViolatorCode = new SqlConnection(connectionString)) using (SqlCommand lastCommnd = updateViolatorCode.CreateCommand()) { lastCommnd.CommandText = "UPDATE ViolatorCar SET ViolatorCode = @violatorCode WHERE CarCode = @code"; lastCommnd.Parameters.AddWithValue("@code", Convert.ToInt32(searchCriterion.Text)); lastCommnd.Parameters.AddWithValue("@violatorCode", violatorCode); updateViolatorCode.Open(); lastCommnd.ExecuteNonQuery(); updateViolatorCode.Close(); } } } else { MessageBox.Show("Вы не выбрали данные для изменения."); return; } MessageBoxResult mboxResult = MessageBox.Show("Данные обновлены. Желаете изменить что-нибудь еще?", "Предупреждение", MessageBoxButton.YesNo); if (mboxResult == MessageBoxResult.No) { ChangeViolatorsCars changeViolarorsCars = new ChangeViolatorsCars(); changeViolarorsCars.Show(); this.Close(); } }