private void upd_Click(object sender, EventArgs e) { string id = sId.Text; Regex regexId = new Regex(@"^[1-9]\d*$"); if (regexId.IsMatch(id)) { string name = sName.Text; string country = sCou.Text; string searchId = "Id = " + id; string searchName = "Name = '" + name + "'"; DataRow[] rowId = virusesDBDataSet2.Scientist.Select(searchId); DataRow[] rowName = virusesDBDataSet2.Scientist.Select(searchName); Regex regex = new Regex("^([a-zA-Z]{2,}\\s[a-zA-z]{1,}'?-?[a-zA-Z]{2,}\\s?([a-zA-Z]{1,})?)"); if (rowId.Length == 1 && rowName.Length == 0 && regex.IsMatch(name) && CountryValidator(country)) { SqlDataAdapter adapter = new SqlDataAdapter(); String sql = "UPDATE Scientist SET Name = '" + name + "', Country ='" + country + "' Where Id = " + id; adapter.UpdateCommand = new SqlCommand(sql, cnn); adapter.UpdateCommand.ExecuteNonQuery(); this.scientistTableAdapter.Fill(this.virusesDBDataSet2.Scientist); SGrid.Refresh(); SGrid.Update(); } else { MessageBox.Show("Name is not correct or element with this ID or Name is not in table!"); } } else { MessageBox.Show("Id is not correct!"); } }
private void del_Click(object sender, EventArgs e) { string id = sId.Text; Regex regexId = new Regex(@"^[1-9]\d*$"); if (regexId.IsMatch(id)) { string searchId = "Id = " + id; DataRow[] rowId = virusesDBDataSet2.Scientist.Select(searchId); SqlDataAdapter adapter = new SqlDataAdapter(); String vCheck = "Select Id from VirusScientist where ScientistId = '" + id + "'"; adapter.SelectCommand = new SqlCommand(vCheck, cnn); Object check = adapter.SelectCommand.ExecuteScalar(); if (rowId.Length == 1 && check == null) { String sql = "DELETE from Scientist Where Id = " + id; adapter.DeleteCommand = new SqlCommand(sql, cnn); adapter.DeleteCommand.ExecuteNonQuery(); this.scientistTableAdapter.Fill(this.virusesDBDataSet2.Scientist); SGrid.Refresh(); SGrid.Update(); } else { MessageBox.Show("Element with this ID is not in table!"); } } else { MessageBox.Show("Id is not correct!"); } }