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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pozze
    New Member
    • Feb 2008
    • 8

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

    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.
    Code:
    <script language="VB" runat="server">
    Public  Sub UploadBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
    
    'Get the posted file
    Dim fileDataStream As Stream =  MyFile.PostedFile.InputStream 
    
    'get file type
    Dim FileType As string = MyFile.PostedFile.ContentType
    'Get size of file
    Dim fileLength As Integer =  MyFile.PostedFile.ContentLength 
     
    'Create a byte array with file length
    Dim fileData() As Byte =  New Byte(fileLength) {} 
     
    'Read the stream into the byte array
    fileDataStream.Read(fileData,0,fileLength)
    
    'get file name (excluding the path)
    Dim strFileNamePath as String = MyFile.PostedFile.FileName
    Dim intFileNameLength = Instr(1, StrReverse(strFileNamePath), "\")
    Dim strFileNameOnly as String = Mid(strFileNamePath, (Len(strFileNamePath)-intFileNameLength)+2)
    
    Dim strConnection As String  = "Provider=SQLOLEDB;Driver={SQL Server};Server=xxx.com;Database=xxx;uid=xxx;pwd=xxx;"
    Dim DBConnection As New OleDbConnection(strConnection)
    Dim UpdateCommand as new oledbcommand
           UpdateCommand.Connection = DBConnection
           UpdateCommand.Commandtype = Commandtype.text 
           UpdateCommand.Commandtext= "INSERT INTO dbo.Files (FileName, FileSize, ContentType, FileData) Values(?,?,?,?)"
           UpdateCommand.Parameters.AddWithValue("?", strFileNameOnly)
           UpdateCommand.Parameters.AddWithValue("?", fileLength)
           UpdateCommand.Parameters.AddWithValue("?", FileType)
           UpdateCommand.Parameters.AddWithValue("?", fileData)
    							
    DBConnection.open()
    UpdateCommand.ExecuteNonQuery
    DBConnection.Close()
    	
    End Sub
    </script>
    And the upload form code is:
    Code:
    <h3>File Upload</h3>
    
    <form enctype="multipart/form-data" runat="server">
       File: <input id="myFile" type="file" runat="server">
       <input type=button value="Upload" OnServerClick="UploadBtn_Click" runat="server">
    </form>
  • pozze
    New Member
    • Feb 2008
    • 8

    #2
    In Addition to the above, I can also get the file and display it using the following code.

    Code:
    <script language="VB" runat="server">
    Private  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Dim ImageID As Integer = cint(Request.QueryString("ImageID"))
    
    Dim strConnection As String  = "Provider=SQLOLEDB;Driver={SQL Server};Server=xxx;Database=xxx;uid=xxxr;pwd=xxx;"
    	Dim DBConnection As New OleDbConnection(strConnection)
    	Dim UpdateCommand as new oledbcommand
    	       UpdateCommand.Connection = DBConnection
    	       UpdateCommand.Commandtype = Commandtype.text 
    	       UpdateCommand.Commandtext= "SELECT * FROM dbo.Files WHERE IsID = ? "
    	       UpdateCommand.Parameters.AddWithValue("?", ImageID)
    
    DBConnection.Open()
    Dim myReader As oledbDataReader = UpdateCommand.ExecuteReader
    If myReader.Read Then
            Response.ContentType = myReader("ContentType").ToString()
            Response.BinaryWrite(myReader("FileData"))
    end if
    DBConnection.Close()
     
    End Sub
    
    </script>
    But I still need to be able to display the images in a datagrid as well as other non binary data.

    Comment

    • pozze
      New Member
      • Feb 2008
      • 8

      #3
      Don't worry, I have figured it out myself :)

      Comment

      Working...