Hi, I have a DetailsView which inserts a news article into a database.
Various checks happen along the way but basically, if the data is added to the table, I want to upload the picture in the FileUpload to a folder in the site with the name
MAXID.jpg.
Aplogies. I have been banging my head against a brick wall all day with this and it turned out the code was fine. It was a permissions issue on the server! Doh!
And, yes, you are right. I needed to ask about Item_Inserted not Item_Updated: hence the Mark as Answer.
banksidepoet
Participant
774 Points
862 Posts
ItemUpdated not firing
Dec 18, 2012 03:45 PM|LINK
Hi, I have a DetailsView which inserts a news article into a database.
Various checks happen along the way but basically, if the data is added to the table, I want to upload the picture in the FileUpload to a folder in the site with the name MAXID.jpg.
This event is NOT firing at all, though.
Here's the code:
DataSources
DV
FileUpload
C#
(In full)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; using System.Web.Security; using System.Web.Configuration; using FreeTextBoxControls; public partial class admin_new_article : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void article_DetailsView_ItemInserting(object sender, DetailsViewInsertEventArgs e) { bool cancelInsert = false; //Reference the FileUpload control FileUpload imageUpload = (FileUpload)article_DetailsView.FindControl("headerGraphicInsert_FileUpload"); if (!imageUpload.HasFile) { // No file was uploaded cancelInsert = true; } else { // There was a file uploaded if (!imageUpload.FileName.ToUpper().EndsWith(".JPG")) { cancelInsert = true; //Invalid file } } if (cancelInsert) { // Cancel insert and show warning message e.Cancel = true; //Reference the Warning Label control Label warning = (Label)article_DetailsView.FindControl("cannotUploadImageMessage_Label"); warning.Visible = true; } DateTime tempDateNow = DateTime.Now; string dateFormat = "dd MMMM yyyy"; e.Values["date_added"] = (tempDateNow.ToString(dateFormat)); e.Values["last_modified"] = (tempDateNow.ToString(dateFormat)); } protected void article_DetailsView_ItemInserted(object sender, DetailsViewInsertedEventArgs e) { //If the record was successfully inserted, save the picture { if (e.AffectedRows > 0) { // Determine MAX picture ID DataView results = (DataView)maxArticleID_SqlDataSource.Select(DataSourceSelectArguments.Empty); int articleJustAdded = (int)results[0][0]; //Reference the FileUpload control FileUpload imageUpload = (FileUpload)article_DetailsView.FindControl("headerGraphicInsert_FileUpload"); if (imageUpload.HasFile) { string baseDirectory = Server.MapPath("/assets/images/news-images/"); imageUpload.SaveAs(baseDirectory + articleJustAdded + ".jpg"); } } } Response.Redirect(Request.RawUrl); } protected void article_DetailsView_ItemUpdating(object sender, DetailsViewUpdateEventArgs e) { bool cancelInsert = false; //Reference the FileUpload control FileUpload imageUpload = (FileUpload)article_DetailsView.FindControl("headerGraphicEdit_FileUpload"); if (imageUpload.HasFile) { if (!imageUpload.FileName.ToUpper().EndsWith(".JPG")) { cancelInsert = true; //Invalid file } } if (cancelInsert) { // Cancel insert and show warning message e.Cancel = true; //Reference the Warning Label control Label warning = (Label)article_DetailsView.FindControl("cannotUploadImageMessage_Label"); warning.Visible = true; } DateTime tempDateNow = DateTime.Now; string dateFormat = "dd MMMM yyyy"; e.NewValues["last_modified"] = (tempDateNow.ToString(dateFormat)); } protected void article_DetailsView_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e) { //If the record was successfully inserted, save the picture { if (e.AffectedRows > 0) { // Determine MAX picture ID DataView results = (DataView)maxArticleID_SqlDataSource.Select(DataSourceSelectArguments.Empty); int articleJustAdded = (int)results[0][0]; //Reference the FileUpload control FileUpload imageUpload = (FileUpload)article_DetailsView.FindControl("headerGraphicEdit_FileUpload"); if (imageUpload.HasFile) { string baseDirectory = Server.MapPath("/assets/images/news-images/"); imageUpload.SaveAs(baseDirectory + articleJustAdded + ".jpg"); } } } Response.Redirect(Request.RawUrl); } protected void article_DetailsView_ItemDeleted(object sender, DetailsViewDeletedEventArgs e) { Response.Redirect(Request.RawUrl); } protected void article_DetailsView_ModeChanging(object sender, DetailsViewModeEventArgs e) { if (e.NewMode == DetailsViewMode.Insert) { articleChoice_Panel.Visible = false; } else { articleChoice_Panel.Visible = true; } } protected void listArticles_SqlDataSource_Selected(object sender, SqlDataSourceStatusEventArgs e) { if (e.AffectedRows < 1) { articleChoice_Panel.Visible = false; article_DetailsView.ChangeMode(DetailsViewMode.Insert); } } protected void article_DetailsView_DataBound(object sender, EventArgs e) { if (article_DetailsView.CurrentMode == DetailsViewMode.Insert) { FreeTextBox ftbInsert = (FreeTextBox)article_DetailsView.FindControl("storyInsert_FreeTextBox"); ftbInsert.ShowTagPath = false; } else if (article_DetailsView.CurrentMode == DetailsViewMode.Edit) { FreeTextBox ftbEdit = (FreeTextBox)article_DetailsView.FindControl("storyEdit_FreeTextBox"); ftbEdit.ShowTagPath = false; } if (article_DetailsView.CurrentMode == DetailsViewMode.ReadOnly) { Image headImage = (Image)article_DetailsView.FindControl("headerGraphic_Image"); headImage.ImageUrl = "/assets/images/news-images/" + chooseArticle_DropDownList.SelectedValue + ".jpg"; } else if (article_DetailsView.CurrentMode == DetailsViewMode.Edit) { Image headImage = (Image)article_DetailsView.FindControl("headerGraphic_Image"); headImage.ImageUrl = "/assets/images/news-images/" + chooseArticle_DropDownList.SelectedValue + ".jpg"; } } }Why is the ItemUpdated event not firing at all?
mth13
Member
331 Points
96 Posts
Re: ItemUpdated not firing
Dec 18, 2012 05:04 PM|LINK
Well, are you updating a row at all? From the sounds of it, you are just Inserting. Insert =/= Update.
banksidepoet
Participant
774 Points
862 Posts
Re: ItemUpdated not firing
Dec 18, 2012 05:16 PM|LINK
Aplogies. I have been banging my head against a brick wall all day with this and it turned out the code was fine. It was a permissions issue on the server! Doh!
And, yes, you are right. I needed to ask about Item_Inserted not Item_Updated: hence the Mark as Answer.
What a day! Caffeine!