private void butAdd_Click(object sender, EventArgs e) { frmAddCity ac = new frmAddCity("Добавить город"); ac.ShowDialog(); string newCity = ac.value; if (newCity != "") { string query = "select count(city_id) from Cities where city_name = '{0}'"; int kol = Get_kol(newCity, query); if (kol > 0) { MessageBox.Show("Такое значение в списке городов уже есть!"); return; } else { string connectionString = @"Data Source=.;Initial Catalog=PetShopO;user id=sa; password=1;"; myConnection = new SqlConnection(connectionString); try { myConnection.Open(); } catch (SqlException ex) { MessageBox.Show(ex.Message); ; } var sqlCmd = new SqlCommand("insert_into_cities", myConnection); sqlCmd.CommandType = CommandType.StoredProcedure; sqlCmd.Parameters.AddWithValue("@name", newCity); sqlCmd.ExecuteNonQuery(); } string connectionString1 = @"Data Source=.;Initial Catalog=PetShopO;user id=sa; password=1;"; myConnection = new SqlConnection(connectionString1); try { myConnection.Open(); } catch (SqlException ex) { MessageBox.Show(ex.Message); ; } fillCityBox(); cbCity.Text = newCity; } }
private void butChange_Click(object sender, EventArgs e) { frmAddCity changeCity = new frmAddCity("Изменить город", dgvCities.SelectedCells[1].Value.ToString()); int id = Convert.ToInt32(dgvCities.SelectedCells[0].Value); changeCity.ShowDialog(); string name = changeCity.value; if(name != "") { if (Get_kol(name) != 0) { MessageBox.Show("Такое значение в списке городов уже есть!"); return; } else { try { string connectionString = @"Data Source=.;Initial Catalog=PetShopO;user id=sa; password=1;"; myConnection = new SqlConnection(connectionString); try { myConnection.Open(); } catch (SqlException ex) { MessageBox.Show(ex.Message); ; } using (var cmd = myConnection.CreateCommand()) { cmd.CommandType = CommandType.Text; cmd.CommandText = string.Format("update Cities set city_name = '{0}' where city_id = '{1}'", name, id); cmd.ExecuteNonQuery(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } GetData(); } }