private void OnOpenAttachment() { PersonAttachment att = SelectedPersonAttachment; if (att == null) { return; } try { SaveFileDialog dlg = new SaveFileDialog(); var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); var fullPath = System.IO.Path.Combine(path, att.FileName) + "." + att.FileExtension; File.WriteAllBytes(fullPath, att.FileBytes); Process.Start(fullPath); } catch (IOException ex) { MessageBox.Show(ex.Message, "Error opening file", MessageBoxButton.OK, MessageBoxImage.Error); } catch (Exception ex) { MessageBox.Show(ex.Message, "Other exception type", MessageBoxButton.OK, MessageBoxImage.Error); } }
private static void SetCommonParameters(PersonAttachment item, SqlCommand cmd) { cmd.Parameters.AddWithValue("@PersonId", item.PersonId); cmd.Parameters.AddWithValue("@Caption", item.Caption); cmd.Parameters.AddWithValue("@FileName", item.FileName); cmd.Parameters.AddWithValue("@FileExtension", item.FileExtension); cmd.Parameters.AddWithValue("@FileBytes", item.FileBytes); }
public async Task <ActionResult> DeleteConfirmed(int id) { PersonAttachment personAttachment = await db.PersonAttachments.FindAsync(id); db.PersonAttachments.Remove(personAttachment); await db.SaveChangesAsync(); return(RedirectToAction("Details", "Person", new { id = personAttachment.PersonID })); }
internal static void DeleteEntity(PersonAttachment item, SqlConnection conn) { using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandType = System.Data.CommandType.Text; cmd.CommandText = "delete PersonAttachment where Id = @Id"; cmd.Parameters.AddWithValue("@PersonAttachmentId", item.Id); cmd.ExecuteNonQuery(); } }
public async Task <ActionResult> Edit([Bind(Include = "ID,PersonID,AttachmentID,Action")] PersonAttachment personAttachment) { if (ModelState.IsValid) { db.Entry(personAttachment).State = EntityState.Modified; await db.SaveChangesAsync(); return(RedirectToAction("Index")); } ViewBag.AttachmentID = new SelectList(db.Attachments, "ID", "Name", personAttachment.AttachmentID); ViewBag.PersonID = new SelectList(db.People, "ID", "FirstName", personAttachment.PersonID); return(View(personAttachment)); }
public async Task <ActionResult> Create([Bind(Include = "PersonID,AttachmentID,Action")] PersonAttachment personAttachment) { if (ModelState.IsValid) { db.PersonAttachments.Add(personAttachment); await db.SaveChangesAsync(); return(RedirectToAction("Details", "Person", new { id = personAttachment.PersonID })); } ViewBag.AttachmentID = new SelectList(db.Attachments, "ID", "Name", personAttachment.AttachmentID); ViewBag.PersonID = new SelectList(db.People, "ID", "FirstName", personAttachment.PersonID); return(View(personAttachment)); }
// GET: PersonAttachment/Details/5 public async Task <ActionResult> Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } PersonAttachment personAttachment = await db.PersonAttachments.FindAsync(id); if (personAttachment == null) { return(HttpNotFound()); } return(View(personAttachment)); }
private static PersonAttachment LoadPersonAttachmentFromDataReader(SqlDataReader dr) { var c = new PersonAttachment(); c.Id = dr.MapTo <int>("Id"); c.PersonId = dr.MapTo <int>("PersonId"); c.Caption = dr.MapTo <string>("Caption"); c.FileName = dr.MapTo <string>("FileName"); c.FileExtension = dr.MapTo <string>("FileExtension"); c.FileBytes = dr.MapTo <Byte[]>("FileBytes"); c.IsDirty = false; return(c); }
internal static void InsertEntity(PersonAttachment item, SqlConnection conn) { using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandType = System.Data.CommandType.Text; var sql = new StringBuilder(); sql.Append("insert PersonAttachment (PersonId, Caption, FileBytes, FileName, FileExtension)"); sql.Append("values (@PersonId, @Caption, @FileBytes, @FileName, @FileExtension);"); sql.Append("select cast ( scope_identity() as int);"); cmd.CommandText = sql.ToString(); SetCommonParameters(item, cmd); item.Id = (int)cmd.ExecuteScalar(); } }
// GET: PersonAttachment/Edit/5 public async Task <ActionResult> Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } PersonAttachment personAttachment = await db.PersonAttachments.FindAsync(id); if (personAttachment == null) { return(HttpNotFound()); } ViewBag.AttachmentID = new SelectList(db.Attachments, "ID", "Name", personAttachment.AttachmentID); ViewBag.PersonID = new SelectList(db.People, "ID", "FirstName", personAttachment.PersonID); return(View(personAttachment)); }
public void LoadAttachmentFromFile(string fileName) { if (SelectedItem == null) { throw new ApplicationException("Must select a Person First"); } var attach = new PersonAttachment { FileName = System.IO.Path.GetFileNameWithoutExtension(fileName), FileExtension = System.IO.Path.GetExtension(fileName).Replace(".", ""), FileBytes = File.ReadAllBytes(fileName) }; SelectedItem.PersonAttachments.Add(attach); RaiseAllCanExecuteChanged(); }
internal static void UpdateEntity(PersonAttachment item, SqlConnection conn) { using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandType = System.Data.CommandType.Text; var sql = new StringBuilder(); sql.Append("update PersonAttachment set "); sql.Append(" PersonId = @PersonId, "); sql.Append(" Caption = @Caption, "); sql.Append(" FileName = @FileName "); sql.Append(" FileBytes = @FileBytes "); sql.Append("where Id = @Id "); cmd.CommandText = sql.ToString(); SetCommonParameters(item, cmd); cmd.Parameters.AddWithValue("@Id", item.Id); cmd.ExecuteNonQuery(); } }
private void OnSaveAttachmentToDisk() { PersonAttachment att = SelectedPersonAttachment; if (att == null) { return; } SaveFileDialog dlg = new SaveFileDialog(); var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); dlg.DefaultExt = att.FileExtension; dlg.FileName = System.IO.Path.Combine(path, att.FileName) + "." + att.FileExtension; if (dlg.ShowDialog() == true) { File.WriteAllBytes(dlg.FileName, att.FileBytes); } }
public static PersonAttachment Persist(PersonAttachment PersonAttachment, SqlConnection conn) { if (PersonAttachment.Id == 0 && PersonAttachment.IsMarkedForDeletion) { PersonAttachment = null; } else if (PersonAttachment.IsMarkedForDeletion) { DeleteEntity(PersonAttachment, conn); PersonAttachment = null; } else if (PersonAttachment.Id == 0) { InsertEntity(PersonAttachment, conn); PersonAttachment.IsDirty = false; } else if (PersonAttachment.IsDirty) { UpdateEntity(PersonAttachment, conn); PersonAttachment.IsDirty = false; } return(PersonAttachment); }