Can't find items in Attachments

Last post 08-17-2009, 4:52 AM by Raime910. 3 replies.
Sort Posts: Previous Next
  •  08-16-2009, 5:34 AM 54718

    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
  •  08-16-2009, 3:27 PM 54721 in reply to 54718

    Re: Can't find items in Attachments

    Okay the problem was this;
     
    ItemPictureAttachments.TempDirectory = "~/shop/uploads/temp/"; 
     
    I removed it and it worked.
     
    Thanks,
     
    -Ryan
  •  08-16-2009, 9:27 PM 54724 in reply to 54721

    Re: Can't find items in Attachments

    Ryan,
     
    You should set that property in the Init event, and add the item in the Load event
     
    Regards,
    Terry
     
  •  08-17-2009, 4:52 AM 54733 in reply to 54724

    Re: Can't find items in Attachments

    Thanks Terry, another thing.. I have this function that resize images before it copies the image to my server;
    1. private void ItemPicture_FileUploaded(object sender, UploaderEventArgs args)  
    2.     {  
    3.         if (GetVisibleItemCount() >= 5)  
    4.             return;  
    5.   
    6.         using (System.IO.Stream stream = args.OpenStream())  
    7.         {  
    8.             ItemPictureAttachments.Upload(args.FileSize, args.FileName, ResizeFromStream(640, stream));  
    9.             // ItemPictureAttachments.Items.Add(args.FileSize, args.FileName, stream);  
    10.         }  
    11.     }  
    12.   
    13.     public Stream ResizeFromStream(int MaxSideSize, Stream Buffer)  
    14.     {  
    15.         int intNewWidth;  
    16.         int intNewHeight;  
    17.         System.Drawing.Image imgInput = System.Drawing.Image.FromStream(Buffer);  
    18.   
    19.         // GET IMAGE FORMAT  
    20.         ImageFormat fmtImageFormat = imgInput.RawFormat;  
    21.   
    22.         // GET ORIGINAL WIDTH AND HEIGHT  
    23.         int intOldWidth = imgInput.Width;  
    24.         int intOldHeight = imgInput.Height;  
    25.   
    26.         // IS LANDSCAPE OR PORTRAIT ??   
    27.         int intMaxSide;  
    28.   
    29.         if (intOldWidth >= intOldHeight)  
    30.         {  
    31.             intMaxSide = intOldWidth;  
    32.         }  
    33.         else  
    34.         {  
    35.             intMaxSide = intOldHeight;  
    36.         }  
    37.   
    38.   
    39.         if (intMaxSide > MaxSideSize)  
    40.         {  
    41.             // SET NEW WIDTH AND HEIGHT  
    42.             double dblCoef = MaxSideSize / (double)intMaxSide;  
    43.             intNewWidth = Convert.ToInt32(dblCoef * intOldWidth);  
    44.             intNewHeight = Convert.ToInt32(dblCoef * intOldHeight);  
    45.         }  
    46.         else  
    47.         {  
    48.             intNewWidth = intOldWidth;  
    49.             intNewHeight = intOldHeight;  
    50.         }  
    51.           
    52.         // CREATE NEW BITMAP  
    53.         Bitmap bmpResized = new Bitmap(imgInput, intNewWidth, intNewHeight);  
    54.   
    55.   
    56.         // SAVE BITMAP TO STREAM  
    57.         MemoryStream imgStream = new MemoryStream();  
    58.         bmpResized.Save(imgStream, imgInput.RawFormat);  
    59.   
    60.         // RELEASE RESOURCES  
    61.         imgInput.Dispose();  
    62.         bmpResized.Dispose();  
    63.         Buffer.Close();  
    64.   
    65.         return imgStream;  
    66.     }  
    But for some reason it return a file with 0 bytes and copies this empty file into my server. What am I missing?
     
    Thanks,
     
    - Ryan
View as RSS news feed in XML