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.
And the upload form code is:
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>
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>
Comment