469,602 Members | 2,143 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

VB.NET WEB Display images stored in sql 2005 in datagrid?

8
Hi,
I need to display images and other record information retrieved from an SQL 2005 database in a datagrid on a web page. I'm coding in VB .net I have recently changed over from VB ASP and i'm still getting used to .net

I can successfully upload images and other data into the SQL Database, I'm currently storing RecordID, File Name, Type of File (mime), File Size, and File Data.

I can retreive non binary data to a datagrid but I cannot figure out how to display the images in the datagrid as well.

Below is the code I'm using to store the image and other data to the database.
Expand|Select|Wrap|Line Numbers
  1. <script language="VB" runat="server">
  2. Public  Sub UploadBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
  3.  
  4. 'Get the posted file
  5. Dim fileDataStream As Stream =  MyFile.PostedFile.InputStream 
  6.  
  7. 'get file type
  8. Dim FileType As string = MyFile.PostedFile.ContentType
  9. 'Get size of file
  10. Dim fileLength As Integer =  MyFile.PostedFile.ContentLength 
  11.  
  12. 'Create a byte array with file length
  13. Dim fileData() As Byte =  New Byte(fileLength) {} 
  14.  
  15. 'Read the stream into the byte array
  16. fileDataStream.Read(fileData,0,fileLength)
  17.  
  18. 'get file name (excluding the path)
  19. Dim strFileNamePath as String = MyFile.PostedFile.FileName
  20. Dim intFileNameLength = Instr(1, StrReverse(strFileNamePath), "\")
  21. Dim strFileNameOnly as String = Mid(strFileNamePath, (Len(strFileNamePath)-intFileNameLength)+2)
  22.  
  23. Dim strConnection As String  = "Provider=SQLOLEDB;Driver={SQL Server};Server=xxx.com;Database=xxx;uid=xxx;pwd=xxx;"
  24. Dim DBConnection As New OleDbConnection(strConnection)
  25. Dim UpdateCommand as new oledbcommand
  26.        UpdateCommand.Connection = DBConnection
  27.        UpdateCommand.Commandtype = Commandtype.text 
  28.        UpdateCommand.Commandtext= "INSERT INTO dbo.Files (FileName, FileSize, ContentType, FileData) Values(?,?,?,?)"
  29.        UpdateCommand.Parameters.AddWithValue("?", strFileNameOnly)
  30.        UpdateCommand.Parameters.AddWithValue("?", fileLength)
  31.        UpdateCommand.Parameters.AddWithValue("?", FileType)
  32.        UpdateCommand.Parameters.AddWithValue("?", fileData)
  33.  
  34. DBConnection.open()
  35. UpdateCommand.ExecuteNonQuery
  36. DBConnection.Close()
  37.  
  38. End Sub
  39. </script>
  40.  
And the upload form code is:
Expand|Select|Wrap|Line Numbers
  1. <h3>File Upload</h3>
  2.  
  3. <form enctype="multipart/form-data" runat="server">
  4.    File: <input id="myFile" type="file" runat="server">
  5.    <input type=button value="Upload" OnServerClick="UploadBtn_Click" runat="server">
  6. </form>
  7.  
Feb 23 '08 #1
2 2647
pozze
8
In Addition to the above, I can also get the file and display it using the following code.

Expand|Select|Wrap|Line Numbers
  1. <script language="VB" runat="server">
  2. Private  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
  3. Dim ImageID As Integer = cint(Request.QueryString("ImageID"))
  4.  
  5. Dim strConnection As String  = "Provider=SQLOLEDB;Driver={SQL Server};Server=xxx;Database=xxx;uid=xxxr;pwd=xxx;"
  6.     Dim DBConnection As New OleDbConnection(strConnection)
  7.     Dim UpdateCommand as new oledbcommand
  8.            UpdateCommand.Connection = DBConnection
  9.            UpdateCommand.Commandtype = Commandtype.text 
  10.            UpdateCommand.Commandtext= "SELECT * FROM dbo.Files WHERE IsID = ? "
  11.            UpdateCommand.Parameters.AddWithValue("?", ImageID)
  12.  
  13. DBConnection.Open()
  14. Dim myReader As oledbDataReader = UpdateCommand.ExecuteReader
  15. If myReader.Read Then
  16.         Response.ContentType = myReader("ContentType").ToString()
  17.         Response.BinaryWrite(myReader("FileData"))
  18. end if
  19. DBConnection.Close()
  20.  
  21. End Sub
  22.  
  23. </script>
  24.  
  25.  
But I still need to be able to display the images in a datagrid as well as other non binary data.
Feb 23 '08 #2
pozze
8
Don't worry, I have figured it out myself :)
Feb 25 '08 #3

Post your reply

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

Similar topics

2 posts views Thread by John Do | last post: by
13 posts views Thread by moondaddy | last post: by
2 posts views Thread by Olav Tollefsen | last post: by
4 posts views Thread by Alek Davis | last post: by
5 posts views Thread by Peter Lapic | last post: by
3 posts views Thread by den 2005 | last post: by
3 posts views Thread by five40 | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.