code_junkie

What do i use instead of e.Values.add(… for the ListViewEditEventArgs to upload an Image

Nov 12th, 2011
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. byte[] img = null;
  2. if (uplImage.HasFile || !uplImage.FileName.ToLower().EndsWith(".jpg"))
  3. {
  4. try
  5. {
  6. img = new byte[uplImage.PostedFile.ContentLength];
  7. uplImage.PostedFile.InputStream.Read(img, 0, img.Length);
  8. }
  9. catch
  10. {
  11. fileuploadlbl.Text = "unable to upload " + uplImage.FileName.ToString();
  12. }
  13. }
  14. if (img == null)
  15. {
  16. e.Cancel = true;
  17. fileuploadlbl.Text = "Please choose a file to upload";
  18. }
  19.  
  20. try
  21. {
  22. e.Values.Add("ProjectPhoto", new System.Data.Linq.Binary(img));
  23. fileuploadlbl.Text = "File Upload Successful";
  24. }
  25. catch
  26. {
  27. fileuploadlbl.Text = "File Upload Failed, please try again";
  28. }
  29. }
  30.  
  31. TextBox myCaption = (TextBox)ProjectPhotosList.EditItem.FindControl("ProjectPhotoCaptionTextBox");
  32.  
  33. int mykey = int.Parse(ProjectPhotosList.DataKeys[e.ItemIndex].Value.ToString());
  34.  
  35. if (myFile.HasFile)
  36. {
  37.  
  38. //Get the posted file
  39. Stream fileDataStream = myFile.PostedFile.InputStream;
  40.  
  41. //Get length of file
  42. int fileLength = myFile.PostedFile.ContentLength;
  43.  
  44. //Create a byte array with file length
  45. byte[] fileData = new byte[fileLength];
  46.  
  47. //Read the stream into the byte array
  48. fileDataStream.Read(fileData, 0, fileLength);
  49.  
  50. //get the file type
  51. string fileType = myFile.PostedFile.ContentType;
  52.  
  53. //Open Connection
  54. PHJamesDataContext db = new PHJamesDataContext();
  55. //Find the Right Row
  56. PHJProjectPhoto Newphoto = (from p in db.PHJProjectPhotos
  57. where p.ProjectPhotoId == mykey
  58. select p).Single<PHJProjectPhoto>();
  59.  
  60.  
  61. Newphoto.ProjectPhoto = fileData;
  62.  
  63. db.SubmitChanges();
  64. }
Add Comment
Please, Sign In to add comment