I'm using a persisted upload control to upload multiple files on my web server. But whenever I submit my changes I get an object reference error. I don't know what I'm missing. Here are my codes
- using System;
- using System.IO;
- using CuteWebUI;
- using System.Data;
- using System.Web.UI.WebControls;
-
- public partial class Controls_Uploader_FileUploader : System.Web.UI.UserControl
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- ItemPictureAttachments.ShowCheckBoxes = false;
- ItemPictureAttachments.TempDirectory = "~/shop/uploads/temp/";
- }
-
- protected override void OnInit(EventArgs e)
- {
- SampleUtil.SetPageCache();
- base.OnInit(e);
-
- if (!Page.IsPostBack)
- {
- if (Request.QueryString["si"] != "" && Request.QueryString["si"] != null)
- {
- PopulateUploader(int.Parse(Request.QueryString["si"].ToString()));
- }
- }
-
- ItemPictureAttachments.AttachmentRemoveClicked += new AttachmentItemEventHandler(ItemPictureAttachments_DeleteAttachment);
- ItemPictureUploader.FileUploaded += new UploaderEventHandler(ItemPicture_FileUploaded);
-
- ItemPictureAttachments.InsertButton.Style["display"] = "none";
- }
-
- private void PopulateUploader(int id)
- {
-
-
- ShopItemDL sidl = new ShopItemDL();
- DataSet ds = new DataSet();
- ShopItem si = new ShopItem();
- DataTable dtImageFilename = new DataTable();
- DataTable dtImageFolder = new DataTable();
- string sImageFolder;
-
- si.ShopItemId = id;
- ds = sidl.SelectShopItemPictures(si);
- dtImageFilename = ds.Tables[0];
- dtImageFolder = ds.Tables[1];
-
- sImageFolder = dtImageFolder.Rows[0]["ShopItemPicFolder"].ToString();
-
- int i = 0;
- foreach (DataRow dr in dtImageFilename.Rows)
- {
-
- string str = Server.MapPath(sImageFolder + dr["ShopItemPicFilename"].ToString());
- using (System.IO.FileStream streamImg = new System.IO.FileStream(str, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read))
- {
- ItemPictureAttachments.Items.Add(int.Parse(dr["ShopItemPicSize"].ToString()), dr["ShopItemPicFilename"].ToString(), streamImg);
-
- Image imgPicture = ItemPictureAttachments.Items[i].FindControl("imgPicture") as Image;
- imgPicture.ImageUrl = Page.ResolveUrl(sImageFolder + dr["ShopItemPicFilename"].ToString());
-
- i++;
- }
- }
- }
-
- private void ItemPictureAttachments_DeleteAttachment(object sender, AttachmentItemEventArgs args)
- {
- if (Request.QueryString["si"] != "" && Request.QueryString["si"] != null)
- {
-
- ShopItemDL sidl = new ShopItemDL();
- int itemId = int.Parse(Request.QueryString["si"]);
- string filename = args.Item.FileName;
- string picfolder = sidl.SelectShopItemPicFolder(itemId);
-
-
- if(File.Exists(Server.MapPath(picfolder + filename)))
- {
- ShopItemPic sip = new ShopItemPic();
-
- sip.ShopItemPicFilename = filename;
- sip.ShopItemId = itemId;
- sidl.DeleteShopItemPicture(sip);
-
- File.Delete(Server.MapPath(picfolder + filename));
- }
- }
- }
-
- private void ItemPicture_FileUploaded(object sender, UploaderEventArgs args)
- {
- if (GetVisibleItemCount() >= 5)
- return;
-
-
- if (Request.QueryString["si"] != "" && Request.QueryString["si"] != null)
- {
- foreach (AttachmentItem item in ItemPictureAttachments.Items)
- {
- if (item.FileName == "x_" + Request.QueryString["si"] + "_" + args.FileName || item.FileName == args.FileName)
- {
- lblConfirm.Visible = true;
- lblConfirm.Text += "<br />" + args.FileName + " already exists.";
- }
- }
- }
-
- using (System.IO.Stream stream = args.OpenStream())
- {
- ItemPictureAttachments.Upload(args.FileSize, args.FileName, stream);
- }
- }
-
- protected override void OnPreRender(EventArgs e)
- {
- base.OnPreRender(e);
- if (GetVisibleItemCount() >= 5)
- {
- ItemPictureUploader.InsertButton.Enabled = false;
- }
- else
- {
- ItemPictureUploader.InsertButton.Enabled = true;
- }
- }
-
- int GetVisibleItemCount()
- {
- int count = 0;
- foreach (AttachmentItem item in ItemPictureAttachments.Items)
- {
- if (item.Visible)
- count++;
- }
- return count;
- }
- }
And this is where I get Object reference not set to and instance error..
- UploadAttachments ItemPictureAttachments = (UploadAttachments)arcFileUploader.FindControl("ItemPictureAttachments");
-
-
-
- if (itemId > 0)
- {
- if (ItemPictureAttachments.Items.Count > 0)
- {
- if (!Directory.Exists(HttpContext.Current.Server.MapPath(si.ShopItemPicFolder.ToString())))
- Directory.CreateDirectory(Server.MapPath(si.ShopItemPicFolder.ToString()));
-
- foreach (AttachmentItem item in ItemPictureAttachments.Items)
- {
- if (!File.Exists(Server.MapPath(ViewState["picfolder"] + item.FileName)))
- {
- ShopItemPic sipic = new ShopItemPic();
- sipic.ShopItemId = itemId;
-
- sipic.ShopItemPicFormat = item.FileName.Substring(item.FileName.LastIndexOf(".") + 1, 3);
- sipic.ShopItemPicFilename = "x_" + itemId + "_" + item.FileName;
- sipic.ShopItemPicSize = item.FileSize;
- int picresult = sidl.InsertShopItemPicture(sipic);
-
- if (picresult != -1)
- {
- item.CopyTo(Server.MapPath(si.ShopItemPicFolder + sipic.ShopItemPicFilename));
- }
-
-
-
- }
- }
- }
- }
Any ideas?
Thanks,
- Ryan