Can't find items in Attachments

  •  08-16-2009, 5:34 AM

    Can't find items in Attachments

    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
     
    1. using System;  
    2. using System.IO;  
    3. using CuteWebUI;  
    4. using System.Data;  
    5. using System.Web.UI.WebControls;  
    6.   
    7. public partial class Controls_Uploader_FileUploader : System.Web.UI.UserControl  
    8. {  
    9.     protected void Page_Load(object sender, EventArgs e)  
    10.     {  
    11.         ItemPictureAttachments.ShowCheckBoxes = false;  
    12.         ItemPictureAttachments.TempDirectory = "~/shop/uploads/temp/";  
    13.     }  
    14.   
    15.     protected override void OnInit(EventArgs e)  
    16.     {  
    17.         SampleUtil.SetPageCache();  
    18.         base.OnInit(e);  
    19.   
    20.         if (!Page.IsPostBack)  
    21.         {  
    22.             if (Request.QueryString["si"] != "" && Request.QueryString["si"] != null)  
    23.             {  
    24.                 PopulateUploader(int.Parse(Request.QueryString["si"].ToString()));  
    25.             }  
    26.         }  
    27.   
    28.         ItemPictureAttachments.AttachmentRemoveClicked += new AttachmentItemEventHandler(ItemPictureAttachments_DeleteAttachment);  
    29.         ItemPictureUploader.FileUploaded += new UploaderEventHandler(ItemPicture_FileUploaded);  
    30.   
    31.         ItemPictureAttachments.InsertButton.Style["display"] = "none";  
    32.     }  
    33.   
    34.     private void PopulateUploader(int id)  
    35.     {  
    36.   
    37.         //GET PICTURE DATATABLE FROM DATABASE  
    38.         ShopItemDL sidl = new ShopItemDL();  
    39.         DataSet ds = new DataSet();  
    40.         ShopItem si = new ShopItem();  
    41.         DataTable dtImageFilename = new DataTable();  
    42.         DataTable dtImageFolder = new DataTable();  
    43.         string sImageFolder;  
    44.   
    45.         si.ShopItemId = id;  
    46.         ds = sidl.SelectShopItemPictures(si);  
    47.         dtImageFilename = ds.Tables[0];  
    48.         dtImageFolder = ds.Tables[1];  
    49.   
    50.         sImageFolder = dtImageFolder.Rows[0]["ShopItemPicFolder"].ToString();  
    51.   
    52.         int i = 0;  
    53.         foreach (DataRow dr in dtImageFilename.Rows)  
    54.         {  
    55.             // ADD THE IMAGES THAT WERE UPLOADED BEFORE FOR ITEM EDITING  
    56.             string str = Server.MapPath(sImageFolder + dr["ShopItemPicFilename"].ToString());  
    57.             using (System.IO.FileStream streamImg = new System.IO.FileStream(str, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read))  
    58.             {  
    59.                 ItemPictureAttachments.Items.Add(int.Parse(dr["ShopItemPicSize"].ToString()), dr["ShopItemPicFilename"].ToString(), streamImg);  
    60.                   
    61.                 Image imgPicture = ItemPictureAttachments.Items[i].FindControl("imgPicture"as Image;  
    62.                 imgPicture.ImageUrl = Page.ResolveUrl(sImageFolder + dr["ShopItemPicFilename"].ToString());  
    63.   
    64.                 i++;  
    65.             }  
    66.         }  
    67.     }  
    68.   
    69.     private void ItemPictureAttachments_DeleteAttachment(object sender, AttachmentItemEventArgs args)  
    70.     {  
    71.         if (Request.QueryString["si"] != "" && Request.QueryString["si"] != null)  
    72.         {   
    73.             // GET PICTURE DATA  
    74.             ShopItemDL sidl = new ShopItemDL();              
    75.             int itemId = int.Parse(Request.QueryString["si"]);  
    76.             string filename = args.Item.FileName;  
    77.             string picfolder = sidl.SelectShopItemPicFolder(itemId);  
    78.   
    79.             // DELETE FILE FROM THE SERVER AND DELETE ROW FROM THE DATABASE IF IT EXISTS  
    80.             if(File.Exists(Server.MapPath(picfolder + filename)))  
    81.             {  
    82.                 ShopItemPic sip = new ShopItemPic();  
    83.   
    84.                 sip.ShopItemPicFilename = filename;  
    85.                 sip.ShopItemId = itemId;  
    86.                 sidl.DeleteShopItemPicture(sip);  
    87.   
    88.                 File.Delete(Server.MapPath(picfolder + filename));  
    89.             }  
    90.         }  
    91.     }  
    92.   
    93.     private void ItemPicture_FileUploaded(object sender, UploaderEventArgs args)  
    94.     {  
    95.         if (GetVisibleItemCount() >= 5)  
    96.             return;  
    97.   
    98.         // CHECK AND SEE IF THE IMAGE HAS ALREADY BEEN UPLOADED BEFORE  
    99.         if (Request.QueryString["si"] != "" && Request.QueryString["si"] != null)  
    100.         {  
    101.             foreach (AttachmentItem item in ItemPictureAttachments.Items)  
    102.             {  
    103.                 if (item.FileName == "x_" + Request.QueryString["si"] + "_" + args.FileName || item.FileName == args.FileName)  
    104.                 {  
    105.                     lblConfirm.Visible = true;  
    106.                     lblConfirm.Text += "<br />" + args.FileName + " already exists.";  
    107.                 }  
    108.             }  
    109.         }  
    110.   
    111.         using (System.IO.Stream stream = args.OpenStream())  
    112.         {  
    113.             ItemPictureAttachments.Upload(args.FileSize, args.FileName, stream);  
    114.         }  
    115.     }  
    116.   
    117.     protected override void OnPreRender(EventArgs e)  
    118.     {  
    119.         base.OnPreRender(e);  
    120.         if (GetVisibleItemCount() >= 5)  
    121.         {  
    122.             ItemPictureUploader.InsertButton.Enabled = false;  
    123.         }  
    124.         else  
    125.         {  
    126.             ItemPictureUploader.InsertButton.Enabled = true;  
    127.         }  
    128.     }  
    129.   
    130.     int GetVisibleItemCount()  
    131.     {  
    132.         int count = 0;  
    133.         foreach (AttachmentItem item in ItemPictureAttachments.Items)  
    134.         {  
    135.             if (item.Visible)  
    136.                 count++;  
    137.         }  
    138.         return count;  
    139.     }  

     And this is where I get Object reference not set to and instance error..
     
    1. UploadAttachments ItemPictureAttachments = (UploadAttachments)arcFileUploader.FindControl("ItemPictureAttachments");  
    2.   
    3. // ITEM ID OF THE PRODUCT BEING EDITED OR HAS BEEN ADDED TO THE DATABASE  
    4. // GETTING READY TO UPLOAD THE FILES TO THE SERVER  
    5. if (itemId > 0)  
    6. {  
    7.     if (ItemPictureAttachments.Items.Count > 0)  
    8.     {  
    9.         if (!Directory.Exists(HttpContext.Current.Server.MapPath(si.ShopItemPicFolder.ToString())))  
    10.             Directory.CreateDirectory(Server.MapPath(si.ShopItemPicFolder.ToString()));  
    11.   
    12.         foreach (AttachmentItem item in ItemPictureAttachments.Items)  
    13.         {  
    14.             if (!File.Exists(Server.MapPath(ViewState["picfolder"] + item.FileName)))  
    15.             {  
    16.                 ShopItemPic sipic = new ShopItemPic();  
    17.                 sipic.ShopItemId = itemId;  
    18.                 //I GET AN ERROR IN THIS LINE  
    19.                 sipic.ShopItemPicFormat = item.FileName.Substring(item.FileName.LastIndexOf(".") + 1, 3);  
    20.                 sipic.ShopItemPicFilename = "x_" + itemId + "_" + item.FileName;  
    21.                 sipic.ShopItemPicSize = item.FileSize;  
    22.                 int picresult = sidl.InsertShopItemPicture(sipic);  
    23.   
    24.                 if (picresult != -1)  
    25.                 {  
    26.                     item.CopyTo(Server.MapPath(si.ShopItemPicFolder + sipic.ShopItemPicFilename));  
    27.                 }  
    28.   
    29.                 //You can also open the uploaded file's data stream.  
    30.                 //System.IO.Stream data = item.OpenStream();  
    31.             }  
    32.         }  
    33.     }  

     
     
     Any ideas?
     
    Thanks,
     
    - Ryan
View Complete Thread