473,320 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Converting binary(max) into Stream in asp.net

Hi, PLease can anyone help me in solving my problem..

Thanks in advance...

I am using asp.net 2.0 c# , the project that I am working on is uploading a document and saving it into database (SQL Server 2005).

The error message that i am getting when executing the query in the InsertFile function and the UpdateFile function is:" No mapping exists from object type System .Web.HttpinputStream to a known Managed provider native type"

I am using layering technique so i have a Class library that contain the functions that connect to the database and do all the transactions.Function:
(by the way how can access upload file from layering )

Expand|Select|Wrap|Line Numbers
  1. public int InsertFile(String FileName, Stream upload) 
  2. {
  3.  
  4. SqlConnection conn = new SqlConnection(ConnectionString); 
  5. conn.Open();
  6.  
  7. SqlCommand cmd = new SqlCommand("INSERT INTO Files( FileName,FileBytes ) VALUES ( @FileName ,@upload)", conn); 
  8. cmd.CommandType = CommandType.Text;
  9.  
  10. cmd.Parameters.Add(new SqlParameter("@FileName", FileName));cmd.Parameters.Add(new SqlParameter("@upload", upload)); 
  11. cmd.ExecuteNonQuery();
  12.  
  13. //get the autonumber of the record that was just inserted
  14.  
  15. cmd.CommandText = "SELECT @@identity";int id = int.Parse(cmd.ExecuteScalar().ToString()); 
  16. conn.Close();
  17.  
  18. return id; 
  19. }
  20.  
  21.  
  22.  
  23. public void UpdateFile(Int32 Id, Stream upload) 
  24. {
  25.  
  26. int bufferLen = 8040; 
  27. BinaryReader br = new BinaryReader(upload);
  28.  
  29. byte[] chunk = br.ReadBytes(bufferLen); 
  30.  
  31.  
  32. SqlConnection conn = new SqlConnection(ConnectionString); 
  33.  
  34.  
  35. SqlCommand cmd = new SqlCommand("UPDATE Files SET FileBytes=@Buffer WHERE Id = @Id", conn);cmd.CommandType = CommandType.Text; 
  36.  
  37.  
  38. cmd.Parameters.Add("@Buffer", SqlDbType.VarBinary, bufferLen).Value = chunk; 
  39.  
  40.  
  41. cmd.Parameters.AddWithValue("@Id", Id); 
  42. conn.Open();
  43.  
  44. cmd.ExecuteNonQuery();
  45.  
  46. SqlCommand cmdAppend = new SqlCommand("UPDATE Files SET FileBytes.WRITE(@Buffer, NULL,0) WHERE Id=@Id", conn); 
  47. cmdAppend.Parameters.AddWithValue("@Id", Id);
  48.  
  49. cmdAppend.Parameters.Add("@Buffer", SqlDbType.VarBinary, bufferLen); 
  50. chunk = br.ReadBytes(bufferLen); 
  51.  
  52. conn.Close();
  53.  
  54. while (chunk.Length > 0) 
  55. {
  56.  
  57. cmdAppend.Parameters["@Buffer"].Value = chunk; 
  58. cmdAppend.ExecuteNonQuery();
  59.  
  60. chunk = br.ReadBytes(bufferLen);
  61.  
  62. }
  63.  
  64. br.Close();
  65.  
  66.  
  67.  
  68. }
  69.  
  70.  And where i am using these function is in the .cs file "Upload.aspx.cs"
  71.  
  72.  Function:
  73.  
  74. private void LoadData() 
  75. {
  76.  
  77. rptFiles.DataSource = FilesAccess.Instance.GetAll(); 
  78. rptFiles.DataBind();
  79.  
  80. }
  81.  
  82. protected void btnAdd_Click(object sender, EventArgs e) 
  83. {
  84.  
  85. if (upFile.FileName != string.Empty) 
  86. {
  87.  
  88. if (CheckFileType(upFile.FileName)) 
  89. {
  90.  
  91.  
  92.  
  93. AddFile(upFile.FileName.ToString(), upFile.FileContent);
  94.  
  95. LoadData();
  96.  
  97. }
  98.  
  99. }
  100.  
  101. }
  102.  
  103. private bool CheckFileType(string fileName) 
  104. {
  105.  
  106. return Path.GetExtension(fileName).ToLower() == ".doc"; 
  107. }
  108.  
  109. private void AddFile(String fileName,Stream upload) 
  110. {
  111.  
  112. int newFileId = FilesAccess.Instance.InsertFile(fileName, upload);FilesAccess.Instance.UpdateFile(newFileId, upFile.FileContent); 
  113. LoadData();
  114.  
  115. }
Jul 14 '08 #1
1 2489
Plater
7,872 Expert 4TB
Have you tried the MemoryStream class?
I think it supports convertina byte[] to a stream (and maybe vice-versa)
Jul 14 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Larry Woods | last post by:
I am trying to write a binary stream from ASP (VBScript). No combination seems to work: set x=Server.CreateObject("ADODB.Stream") x.open x.type=1 ' binary x.write ?????? No matter what I...
2
by: Mariusz Sakowski | last post by:
I'm writing class which will be able to store large numbers (my ambition is to make it able to operand on thousands of bits) and perform various operations on it (similiar to those available with...
1
by: BH | last post by:
I'm trying a simple object serialization and deserialization, and keep getting this error: System.Runtime.Serialization.SerializationException: Binary stream does not contain a valid...
1
by: RP2001 | last post by:
Other than using the BinaryReader Class, is there any other way to read in from a binary stream/file? Thanks! -RP
4
by: Pedro Leite | last post by:
Good Afternoon. the code below is properly retreiving binary data from a database and saving it. but instead of saving at client machine is saving at the server machine. what is wrong with my...
2
by: DBuss | last post by:
OK, I'm reading a multicast socket. It attaches fine, reads fine, all of that. The problem is that while some of the data I get is normal text (ASCII String), some of it is Binary Integer. ...
5
by: sreedhar1980 | last post by:
HI All, Could you please Help me to Handle Binary Stream Object in Java Script.
0
by: Vince Filby | last post by:
Hi, We are working with distributing Lucene.net. We have Master Index Server which takes responsibility of distributing the index searching to multiple Index Servers by calling the remote...
2
gauravgmbhr
by: gauravgmbhr | last post by:
Hi All, I am a newbie to ASP. Can some one tell me what wrong in the code. I am trying to write data from HTML text area to an binary ADO stream. temp = split(inString,vbcrlf) //inStr in...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.