youhana metry

youhana metry

  • NA
  • 58
  • 3.6k

Facing Problem When Upload File Using Asp.net

Nov 26 2018 2:44 PM
iam facing problem when i uplad file to sql database it gave me the following message
can you please push your help
 
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 20: { Line 21: Line 22: string filename = Path.GetFileName(FileUpload1.PostedFile.FileName); Line 23: string extension = Path.GetExtension(filename); Line 24: string contentType = FileUpload1.PostedFile.ContentType;
 
the code that i use
  1. protected void Button1_Click(object sender, EventArgs e)  
  2. {  
  3. string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);  
  4. string extension = Path.GetExtension(filename);  
  5. string contentType = FileUpload1.PostedFile.ContentType;  
  6. HttpPostedFile file = FileUpload1.PostedFile;  
  7. byte[] document = new byte[file.ContentLength];  
  8. file.InputStream.Read(document, 0, file.ContentLength);  
  9. if ((extension == ".pdf") || (extension == ".doc") || (extension == ".docx") || (extension == ".xls"))//extension  
  10. {  
  11. if (file.ContentLength <= 31457280)//size  
  12. {  
  13. //Insert the Data in the Table  
  14. SCP.Insert_Script(filename, txtfilename.Text.Trim(), extension, contentType, document, document.Length);  
  15. }  
  16. else  
  17. { Response.Write("Inavlid File size"); return; }  
  18. }  
  19. else  
  20. {  
  21. Response.Write("Inavlid File"); return;  
  22. }  
  23. }  
  24. for business layer I Used the following Code  
  25. public void Insert_Script(string _Name_File, string _DisplayName,string _Extension,string _ContentType, byte[] _FileData,int _FileSize)  
  26. {  
  27. DAL_CCRMUI.DataAccessLayer Dal = new DAL_CCRMUI.DataAccessLayer();  
  28. Dal.Open();  
  29. SqlParameter[] param = new SqlParameter[6];  
  30. param[0] = new SqlParameter("@Name_File", SqlDbType.VarChar,100)  
  31. {  
  32. Value =_Name_File  
  33. };  
  34. param[1] = new SqlParameter("@DisplayName", SqlDbType.VarChar,50)  
  35. {  
  36. Value = _DisplayName  
  37. };  
  38. param[2] = new SqlParameter("@Extension", SqlDbType.VarChar,10)  
  39. {  
  40. Value = _Extension  
  41. };  
  42. param[3] = new SqlParameter("@ContentType", SqlDbType.VarChar,200)  
  43. {  
  44. Value = _ContentType  
  45. };  
  46. param[4] = new SqlParameter("@FileData", SqlDbType.VarBinary, 5000)  
  47. {  
  48. Value = _FileData  
  49. };  
  50. param[5] = new SqlParameter("@FileSize", SqlDbType.BigInt)  
  51. {  
  52. Value = _FileSize  
  53.   
  54. };  
  55. Dal.ExecuteProcedure("Insert_Document", param);  
  56. Dal.Close();  
  57. }  

Answers (4)