public void QnoteListView_UpdateItem(QnoteCollectionID qnoteCollectionID) { if (ModelState.IsValid) { try { if (qnoteCollectionID.NoteID <= 0) { // Checks if the note exists, else we wont be able to update it, right!? ModelState.AddModelError("", String.Format("Anteckningen med det angivna id:t {0} existerar inte!", qnoteCollectionID.NoteID)); return; } else { DropDownList ddlCollection = QnoteUpdateFormView.FindControl("DropDownListCollection") as DropDownList; qnoteCollectionID.CollectionNameID = Int32.Parse(ddlCollection.SelectedValue.ToString()); // Updates the note, and shows that all worked out! Service.CreateNoteAndCollection(qnoteCollectionID); Session["Success"] = "Anteckningen har uppdaterats!"; Response.RedirectToRoute("SingleNote", new { id = qnoteCollectionID.NoteID, header = qnoteCollectionID.Header }); Context.ApplicationInstance.CompleteRequest(); } } catch (Exception) { ModelState.AddModelError("", "Ett fel inträffade då anteckningen skulle uppdateras, försök igen om en stund!"); } } }
// Updates a existing note. public void UpdateNoteAndCollection(QnoteCollectionID qnote) { using (var conn = CreateConnection()) { try { var cmd = new SqlCommand("app.usp_UpdateNoteAndCollection", conn); cmd.CommandType = CommandType.StoredProcedure; // Here im creating the parameters that will be used. cmd.Parameters.Add("@Header", SqlDbType.VarChar, 60).Value = qnote.Header; cmd.Parameters.Add("@Note", SqlDbType.VarChar, 2000).Value = qnote.Note; cmd.Parameters.Add("@NoteID", SqlDbType.Int, 4).Value = qnote.NoteID; cmd.Parameters.Add("@CollectionNameID", SqlDbType.Int, 4).Value = qnote.CollectionNameID; conn.Open(); cmd.ExecuteNonQuery(); } catch { throw new ApplicationException("An error occured when trying to update a note."); } } }
// Creates a new note. public void CreateNoteAndCollection(QnoteCollectionID qnoteCollectionID) { using (var conn = CreateConnection()) { try { var cmd = new SqlCommand("app.usp_CreateNoteAndCollection", conn); cmd.CommandType = CommandType.StoredProcedure; // Here I'm creating the parameters that will be used. cmd.Parameters.Add("@Header", SqlDbType.VarChar, 60).Value = qnoteCollectionID.Header; cmd.Parameters.Add("@Note", SqlDbType.VarChar, 2000).Value = qnoteCollectionID.Note; cmd.Parameters.Add("@UserID", SqlDbType.Int, 4).Value = qnoteCollectionID.UserID; cmd.Parameters.Add("@CollectionNameID", SqlDbType.Int, 4).Value = qnoteCollectionID.CollectionNameID; conn.Open(); cmd.ExecuteNonQuery(); } catch { throw new ApplicationException("An error occured when trying to add a note connected to a collection to the database."); } } }