private void button1_Click(object sender, EventArgs e) { frmAddSpec ac = new frmAddSpec("Добавить вид"); ac.ShowDialog(); string newSpec = ac.value; if (newSpec != "") { int kol = Get_kol(newSpec); if (kol > 0) { MessageBox.Show("Такое значение в списке видов уже есть!"); //tbName.Focus(); 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_species", myConnection); sqlCmd.CommandType = CommandType.StoredProcedure; sqlCmd.Parameters.AddWithValue("@name", newSpec); 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); ; } fillComboBox(); cbSp.Text = newSpec; } }
private void butChange_Click(object sender, EventArgs e) { frmAddSpec changeSpec = new frmAddSpec("Изменить вид", dgvSpecies.SelectedCells[1].Value.ToString()); int id = Convert.ToInt32(dgvSpecies.SelectedCells[0].Value); changeSpec.ShowDialog(); string name = changeSpec.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 Species set species_name = '{0}' where species_id = '{1}'", name, id); cmd.ExecuteNonQuery(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } GetData(); } }