private void add_phonehelp_button_Click(object sender, EventArgs e) { var form = new PhoneHelpForm(); { var request = "SELECT Id, surname + ' ' + name + ' ' + patronymic AS fio FROM Mikolaichuk_Abonent"; var adapter = new SqlDataAdapter(request, connectionString); var abonent_table = new DataTable(); adapter.Fill(abonent_table); var dict = new Dictionary <int, string>(); foreach (DataRow row in abonent_table.Rows) { dict.Add((int)row["Id"], row["fio"].ToString()); } form.AbonentData = dict; } { var request = "SELECT Id, phone + ' (' + type + ') ' AS phonetype FROM Mikolaichuk_Contact"; var adapter = new SqlDataAdapter(request, connectionString); var phone_table = new DataTable(); adapter.Fill(phone_table); var dict = new Dictionary <int, string>(); foreach (DataRow row in phone_table.Rows) { dict.Add((int)row["Id"], row["phonetype"].ToString()); } form.PhoneData = dict; } var res = form.ShowDialog(); if (res == DialogResult.OK) { var abonentid = form.AbonentIdSelect; var contactid = form.ContactIdSelect; var connection = new SqlConnection(connectionString); connection.Open(); var request = @"INSERT INTO Mikolaichuk_Abonent_has_Contact (contact_id, abonent_id) VALUES ('" + contactid.ToString() + "','" + abonentid.ToString() + "')"; var command = new SqlCommand(request, connection); command.ExecuteNonQuery(); connection.Close(); updatePhoneHelpDGV(); } }
private void edit_phonehelp_button_Click(object sender, EventArgs e) { var row = phoneHelp_grid.SelectedRows.Count > 0 ? phoneHelp_grid.SelectedRows[0] : null; if (row == null) { MessageBox.Show("Сначала укажите строку!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } var form = new PhoneHelpForm(); { var request = "SELECT id, surname + ' ' + name + ' ' + patronymic AS fio FROM Mikolaichuk_Abonent"; var adapter = new SqlDataAdapter(request, connectionString); var abonent_table = new DataTable(); adapter.Fill(abonent_table); var dict = new Dictionary <int, string>(); foreach (DataRow dbRow in abonent_table.Rows) { dict.Add((int)dbRow["id"], dbRow["fio"].ToString()); } form.AbonentData = dict; } { var request = "SELECT id, phone + ' (' + type + ') ' AS phonetype FROM Mikolaichuk_Contact"; var adapter = new SqlDataAdapter(request, connectionString); var phone_table = new DataTable(); adapter.Fill(phone_table); var dict = new Dictionary <int, string>(); foreach (DataRow dbRow in phone_table.Rows) { dict.Add((int)dbRow["id"], dbRow["phonetype"].ToString()); } form.PhoneData = dict; } form.AbonentIdSelect = (int)row.Cells["abonent_id"].Value; form.ContactIdSelect = (int)row.Cells["contact_id"].Value; var res = form.ShowDialog(); if (res == DialogResult.OK) { var abonentid = form.AbonentIdSelect; var contactid = form.ContactIdSelect; var abonentid_sel = row.Cells["abonent_id"].Value.ToString(); var contactid_sel = row.Cells["contact_id"].Value.ToString(); var connection = new SqlConnection(connectionString); connection.Open(); var request = @"UPDATE Mikolaichuk_Abonent_has_Contact SET contact_id='" + contactid.ToString() + "', abonent_id='" + abonentid.ToString() + "' " + "WHERE contact_id=" + contactid_sel.ToString() + " AND abonent_id=" + abonentid_sel.ToString() + ""; var command = new SqlCommand(request, connection); command.ExecuteNonQuery(); connection.Close(); updatePhoneHelpDGV(); } }