943,851 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 406
  • ASP.NET RSS
Aug 5th, 2009
-1

i want opposite of this

Expand Post »
Hi here image is getting converted into binary and getting inserted into sqltable .

Now I want the opposite of it i mean i want to retrieve the binary
data and display it as image.



ASP.NET Syntax (Toggle Plain Text)
  1. protected void btnupload_Click(object sender, EventArgs e)
  2. {
  3.  
  4. // set temporary variable for database connection
  5. SqlConnection SqlCon = new SqlConnection("Data Source=localhost;Initial Catalog=ArjunPlastic;Uid=sa;Pwd=vagi0903");
  6. SqlCon.Open();
  7.  
  8. string SQL = "INSERT INTO pictures (Filename,Picture) VALUES ('flower',@image)";
  9. SqlCommand SqlComm = new SqlCommand(SQL,SqlCon);
  10.  
  11. SqlParameter parimage= new SqlParameter("@image", SqlDbType.Image);
  12.  
  13. // convert image file to byte array and pass to sql parameter value
  14. parimage.Value = FileToByteArray("C:\\Documents and Settings\\Administrator\\Desktop\\4014_thumb.jpg");
  15. SqlComm.Parameters.Add(parimage);
  16. SqlComm.ExecuteNonQuery();
  17.  
  18. }
  19.  
  20. public byte[] FileToByteArray(string FileName)
  21. {
  22. byte[] Buffer = null;
  23.  
  24. // Open file for reading
  25. FileStream FileStream = new FileStream(FileName,FileMode.Open,FileAccess.Read);
  26.  
  27. // attach filestream to binary reader
  28. BinaryReader BinaryReader = new BinaryReader(FileStream);
  29.  
  30. // get total byte length of the file
  31. long TotalBytes = new FileInfo(FileName).Length;
  32.  
  33. // read entire file into buffer
  34. Buffer = BinaryReader.ReadBytes((Int32)TotalBytes);
  35.  
  36. // close file reader
  37. FileStream.Close();
  38. FileStream.Dispose();
  39. BinaryReader.Close();
  40. return Buffer;
  41. }
Last edited by peter_budo; Aug 5th, 2009 at 3:15 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Similar Threads
Reputation Points: 6
Solved Threads: 0
Junior Poster in Training
coollife is offline Offline
85 posts
since Aug 2009
Aug 5th, 2009
0

Re: i want opposite of this

Please use code tags when posting code on daniweb:

[code=c#]
...code here...
[/code]


Next -- Please stop opening duplicate thread. I have already given you the answer to that in http://www.daniweb.com/forums/thread209172.html

For some reason you have in your head that I am not storing the image as binary on the SQL server which is not the case. Please try the code first.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Aug 5th, 2009
-1

Re: i want opposite of this

Thread closed, double posting is not welcome.
Stick to original post.
Last edited by peter_budo; Aug 5th, 2009 at 3:17 pm.
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in ASP.NET Forum Timeline: Urgent Answer needed
Next Thread in ASP.NET Forum Timeline: Popup window with zoomed image





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC