Using AsyncFileUpload and stored procedure

Oct 25 2020 5:49 AM
I been over a lot of AsyncFileUpload that save to hard drive and save to DB, but with not a stored procedure.
 
I tried with the following code, but when I view the database, I get 0x89 and my varbinary(max) data field.
 
Thanks,
  1. if (AsyncFileUpload1.HasFile && AsyncFileUpload1.PostedFile != null)  
  2. {  
  3. string filename = System.IO.Path.GetFileName(AsyncFileUpload1.FileName);  
  4. string contentType = AsyncFileUpload1.ContentType.ToString();  
  5. using (Stream fs=AsyncFileUpload1.PostedFile.InputStream)  
  6. {  
  7. using (BinaryReader br = new BinaryReader(fs))  
  8. {  
  9. byte[] bytes = br.ReadBytes((Int32)fs.Length);  
  10. using (SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["xxx"].ConnectionString))  
  11. {  
  12. SqlCommand sqlcmd = new SqlCommand("usp_CustomerClientSave", conn1);  
  13. sqlcmd.CommandType = CommandType.StoredProcedure;  
  14. sqlcmd.Parameters.Add(new SqlParameter("@ClientID", System.Data.SqlDbType.Int)).Value = Convert.ToInt32(Decrypt(Request.QueryString["id"].ToString()));  
  15. sqlcmd.Parameters.Add(new SqlParameter("@UpdatedByID", System.Data.SqlDbType.Int)).Value = Convert.ToInt32(Session["id"].ToString());  
  16. sqlcmd.Parameters.Add(new SqlParameter("@filename", System.Data.SqlDbType.VarChar,100)).Value = filename;  
  17. sqlcmd.Parameters.Add(new SqlParameter("@ContentType", System.Data.SqlDbType.VarChar, 25)).Value = contentType;  
  18. sqlcmd.Parameters.Add(new SqlParameter("@Data", System.Data.SqlDbType.VarBinary,8000)).Value = bytes;  
  19. try  
  20. {  
  21. conn1.Open();  
  22. sqlcmd.ExecuteNonQuery();  
  23. }  
  24. catch (Exception ex)  
  25. {  
  26. throw new System.Exception(ex.ToString());  
  27. }  
  28. finally  
  29. {  
  30. conn1.Close();  
  31. sqlcmd.Dispose();  
  32. }  
  33. }  
  34. }  
  35. }  
  36. }  

Answers (1)