public MainViewModel() { ExportToExcelCommand = new RelayCommand(ExportToExcel); DoneRowCommand = new RelayCommand(DoneRow); DeleteRowCommand = new RelayCommand(DeleteRow); GetRowInfoCommand = new RelayCommand(GetRowInfo); MySqlConnection conn = DBUtils.GetDBConnection(); try { conn.Open(); string sql = "SELECT * FROM appeal;"; MySqlCommand command = new MySqlCommand(sql, conn); MySqlDataReader reader = command.ExecuteReader(); int count = 0; while (reader.Read()) { if ((reader[4].ToString() == "Рассмотрено") || (reader[4].ToString() == "Рассмотренно") || (reader[4].ToString() == "рассмотрено") || (reader[4].ToString() == "рассмотрено")) { DoneRequests.Add(new RequestViewModel((int)reader[0], reader[1].ToString(), reader[2].ToString(), reader[3].ToString(), reader[4].ToString())); } else { Requests.Add(new RequestViewModel((int)reader[0], reader[1].ToString(), reader[2].ToString(), reader[3].ToString(), reader[4].ToString())); } count++; } if (count == 0) { MessageBox.Show("Обращений нет!"); } reader.Close(); conn.Close(); } catch (Exception ex) { if (ex.Message == "Unable to connect to any of the specified MySQL hosts.") { MessageBox.Show("Не удалось подключиться к серверу!"); } else { MessageBox.Show("Error: " + ex.Message); } } }
private void DoneRow() { if (SelectedPerson != null) { MySqlConnection conn = DBUtils.GetDBConnection(); try { conn.Open(); string sql = "UPDATE appeal SET comment = 'Рассмотрено' WHERE ID = " + SelectedPerson.ID.ToString() + ";"; MySqlCommand command = new MySqlCommand(sql, conn); MySqlDataReader reader = command.ExecuteReader(); reader.Close(); conn.Close(); SelectedPerson.Ответ = "Рассмотрено"; DoneRequests.Add(new RequestViewModel(SelectedPerson.ID, SelectedPerson.Обращение, SelectedPerson.Кабинет, SelectedPerson.Дата, SelectedPerson.Ответ)); Requests.Remove(SelectedPerson); } catch { } } }
private void DeleteRow() { if (SelectedPerson != null) { MessageBoxResult dialogResult = MessageBox.Show("Удалить запись?", "Подтверждение удаления", MessageBoxButton.YesNo); if (dialogResult == MessageBoxResult.Yes) // error is here { MySqlConnection conn = DBUtils.GetDBConnection(); try { conn.Open(); string sql = "DELETE FROM appeal WHERE ID = " + SelectedPerson.ID.ToString() + ";"; MySqlCommand command = new MySqlCommand(sql, conn); MySqlDataReader reader = command.ExecuteReader(); reader.Close(); conn.Close(); DoneRequests.Remove(SelectedPerson); Requests.Remove(SelectedPerson); } catch (Exception ex) { if (ex.Message == "Unable to connect to any of the specified MySQL hosts.") { MessageBox.Show("Не удалось подключиться к серверу!"); } else { MessageBox.Show("Error: " + ex.Message); } } } else { } } }