private void Add_Click(object sender, EventArgs e) { String newfirstname ; String newname; String newlastname; AddAuthor addAuth = new AddAuthor(); DialogResult result = addAuth.ShowDialog(this); if (result == DialogResult.Cancel) Hide(); if (result == DialogResult.OK) { using (connection = new SqlConnection(connectionString)) { newfirstname = addAuth.textBox1.Text; newname = addAuth.textBox2.Text; newlastname = addAuth.textBox3.Text; String queryString2 = String.Format("INSERT INTO Author(firstname, name, lastname) VALUES('{0}','{1}','{2}')", newfirstname, newname, newlastname); command = new SqlCommand(queryString2, connection); try { connection.Open(); int number = command.ExecuteNonQuery(); Console.WriteLine("Добавлено объектов: {0}", number); MessageBox.Show("Добавлен новый автор!"); dataGridView1.Update(); UpdateForm(); } catch (Exception ex) { Console.Write(ex); } } } }
private void update_Click(object sender, EventArgs e) { if (dataGridView1.SelectedRows.Count > 0) { int index = dataGridView1.SelectedRows[0].Index; int id = 0; bool converted = Int32.TryParse(dataGridView1[0, index].Value.ToString(), out id); if (converted == false) return; AddAuthor addAuthor = new AddAuthor(); addAuthor.textBox1.Text = dataGridView1[1, index].Value.ToString(); addAuthor.textBox2.Text = dataGridView1[2, index].Value.ToString(); addAuthor.textBox3.Text = dataGridView1[3, index].Value.ToString(); DialogResult result = addAuthor.ShowDialog(this); if (result == DialogResult.Cancel) Hide(); if (result == DialogResult.OK) { using (connection = new SqlConnection(connectionString)) { String queryString4 = String.Format("UPDATE Author SET firstname='{0}', name='{1}', lastname='{2}' WHERE id_author='{3}'", addAuthor.textBox1.Text, addAuthor.textBox2.Text, addAuthor.textBox3.Text, id); command = new SqlCommand(queryString4, connection); connection.Open(); command.ExecuteNonQuery(); } dataGridView1.Refresh(); UpdateForm(); MessageBox.Show("Объект обновлен"); } } }