protected void TorrentAdd_Click(object sender, EventArgs e) { Torrent torrent; if (_id == -1) { torrent = new Torrent(); torrent.CreateDateTime = DateTime.Now; torrent.UpdateDateTime = torrent.CreateDateTime; torrent.AuthorName = User.Identity.Name; myEntities.Torrents.Add(torrent); } else { torrent = myEntities.Torrents.Where(t => t.Id == _id).Single(); torrent.UpdateDateTime = DateTime.Now; } torrent.TypeId = TypeList.SelectedIndex; torrent.Title = TorrentTitle.Text; torrent.SubTitle = TorrentSubtitle.Text; torrent.Body = SummaryText.Text; FileUpload torrentFileUpload = (FileUpload)UploadTable.FindControl("TorrentFileUpload"); if (!torrentFileUpload.HasFile || !torrentFileUpload.FileName.ToLower().EndsWith(".torrent")) { CustomValidator cusValTorrent = (CustomValidator)UploadTable.FindControl("CusValTorrent"); cusValTorrent.IsValid = false; ModelState.AddModelError("Invalid", cusValTorrent.ErrorMessage); } if (ModelState.IsValid && Page.IsValid) { string virtualFolder = "~/TorrentFolder/"; string physicalFolder = Server.MapPath(virtualFolder); string fileName = torrentFileUpload.FileName; string extension = System.IO.Path.GetExtension(torrentFileUpload.FileName); torrentFileUpload.SaveAs(System.IO.Path.Combine(physicalFolder, fileName)); torrent.TorrentFileUrl = virtualFolder + fileName; myEntities.SaveChanges(); } myEntities.SaveChanges(); Response.Redirect(string.Format("~/TorrentView/TorrentDetails.aspx?Id={0}", torrent.Id)); }
protected void TorrentAdd_Click(object sender, EventArgs e) { Torrent torrent; if (_id == -1) { torrent = new Torrent(); torrent.CreateDateTime = DateTime.Now; torrent.UpdateDateTime = torrent.CreateDateTime; torrent.AuthorName = User.Identity.Name; myEntities.Torrents.Add(torrent); } else { torrent = myEntities.Torrents.Where(t => t.Id == _id).Single(); torrent.UpdateDateTime = DateTime.Now; } torrent.TypeId = TypeList.SelectedIndex; torrent.Title = TorrentTitle.Text; torrent.SubTitle = TorrentSubtitle.Text; torrent.Body = SummaryText.Text; FileUpload torrentFileUpload = (FileUpload)UploadTable.FindControl("TorrentFileUpload"); if (!torrentFileUpload.HasFile || !torrentFileUpload.FileName.ToLower().EndsWith(".torrent")) { CustomValidator cusValTorrent = (CustomValidator)UploadTable.FindControl("CusValTorrent"); cusValTorrent.IsValid = false; ModelState.AddModelError("Invalid", cusValTorrent.ErrorMessage); } if (ModelState.IsValid && Page.IsValid) { string virtualFolder = "~/TorrentFolder/"; string physicalFolder = Server.MapPath(virtualFolder); string fileName = torrentFileUpload.FileName; string extension = System.IO.Path.GetExtension(torrentFileUpload.FileName); torrentFileUpload.SaveAs(System.IO.Path.Combine(physicalFolder, fileName)); torrent.TorrentFileUrl = virtualFolder + fileName; myEntities.SaveChanges(); } try { myEntities.SaveChanges(); } catch (DbEntityValidationException ex) { StringBuilder errors = new StringBuilder(); IEnumerable <DbEntityValidationResult> validationResult = ex.EntityValidationErrors; foreach (DbEntityValidationResult result in validationResult) { ICollection <DbValidationError> validationError = result.ValidationErrors; foreach (DbValidationError err in validationError) { errors.Append(err.PropertyName + ":" + err.ErrorMessage + "\r\n"); } } ErrorSubmitTable.Visible = true; ErrorSubmit.Text = errors.ToString(); } finally { Response.Redirect(string.Format("~/TorrentView/TorrentDetails.aspx?Id={0}", torrent.Id)); } }