public static Cancellation NewCancellation(CancellationTemplate cancTemplate) { Cancellation result = null; try { string query = "INSERT INTO Cancellation (UserID, ArticleID, ArticleCount) VALUES (" + cancTemplate.User.UserID + "," + cancTemplate.Article.ArticleID + "," + cancTemplate.Count + "); " + "SELECT * FROM Cancellation WHERE CancellationID = SCOPE_IDENTITY();"; SqlConnection connection = new SqlConnection(ConnectionString); SqlCommand cmd = new SqlCommand(query, connection); connection.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { result = new Cancellation( Convert.ToInt32(reader["CancellationID"]), GetUser(Convert.ToInt32(reader["UserID"])), GetArticle(Convert.ToInt32(reader["ArticleID"])), Convert.ToInt32(reader["ArticleCount"]), Convert.ToDateTime(reader["Created"]) ); } connection.Close(); } catch (Exception ex) { LogWriter.Write(ex, LOGFILE_NAME); string errmsg = "Fehler beim Erstellen des Stornos.\n\n"; errmsg += "DatabaseHandler.NewCancellation(cancTemplate): " + ex.ToString(); throw new Exception(errmsg); } if (result == null) throw new Exception("Fehler beim Erstellen des Stornos. Das Speichern konnte nicht verifiziert werden.\n\nDatabaseHandler.NewCancellation(cancTemplate)"); return result; }
private void btnNewCancellation_Click(object sender, EventArgs e) { if (MessageBox.Show("Storno bestätigen?", "Bestätigen", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == System.Windows.Forms.DialogResult.Cancel) return; try { if (cmbArticles.SelectedItem == null) throw new Exception("Kein Artikel ausgewählt"); CancellationTemplate templ = new CancellationTemplate((User)cmbUsers.SelectedItem, (Article)cmbArticles.SelectedItem, Convert.ToInt32(cmbCount.Text)); templ.Create(); cmbCount.Text = "1"; cmbArticles.SelectedIndex = -1; } catch (Exception ex) { LogWriter.Write(ex, LOGFILE_NAME); MessageBox.Show(ex.Message, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error); } }