private void GetPrinterFromDb() { PrinterList.Clear(); int i = 0; var items = PrinterList.Items; try { using (var con = new MySqlConnection(cs)) { con.Open(); var query = "SELECT PrinterName FROM printers"; using (var cmd = new MySqlCommand(query, con)) { using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { items.Add(reader.GetString("PrinterName")); PrinterList.Items[i].ImageIndex = 0; i++; } } } } } catch (MySqlException) { } }
private void RemovePrinter_Click(object sender, EventArgs e) { try { MySqlConnection con = new MySqlConnection(cs); MySqlCommand cmd = con.CreateCommand(); cmd.CommandText = "DELETE FROM printers where PrinterName='" + PrinterList.SelectedItems[0].Text + "' "; con.Open(); cmd.ExecuteNonQuery(); con.Close(); PrinterList.Clear(); GetPrinterFromDb(); } catch (MySqlException) { } catch (Exception) { } }