private void btnSave_Click(object sender, EventArgs e) { try { Notification n = new Notification(); n.Content = notification_content.Text; n.Date = notification_rem_date.Value.Date; n.Employee_idemployee = Employee.employee_id; n.Employee_name = Employee.employee_name; n.Employee_number = Employee.emp_no; n.Title = notification_title.Text; n.User_iduser = int.Parse(LoginInfo.UserID); //Add a new notification bool state = NotificationHandler.addNotification(n); if (state) { MessageBox.Show("Notification added succesfully...!"); } else { MessageBox.Show("Adding Notification failed. Please check again."); } } catch (Exception ex) { MessageBox.Show("Adding Notification failed. Please check again."); } }
public static bool addNotification(Notification n) { DBConnector dbcon = new DBConnector(); try { if (dbcon.openConnection()) { MySqlCommand cmd = new MySqlCommand(); cmd.CommandText = "INSERT INTO notification (title, content, date, employee_name, employee_number, employee_idemployee, user_iduser) VALUES (N'" + n.Title + "', N'" + n.Content + "', '" + n.Date.ToString("yyyy-MM-dd") + "', N'" + n.Employee_name + "', N'" + n.Employee_number + "', " + n.Employee_idemployee + ", " + n.User_iduser + ")"; cmd.Connection = dbcon.connection; cmd.Prepare(); cmd.ExecuteNonQuery(); dbcon.closeConnection(); return true; } else { dbcon.closeConnection(); return false; } } catch (MySqlException e) { int errorcode = e.Number; Console.Write(e.Message + "\n"); dbcon.closeConnection(); return false; } }