Retreiving Images from Database using an ImageHandler

Imagehandler , RetreiveImage , Binarydata Johannesburg, South Africa
  • 10 years ago

    Hi Coders

    I have an application where I use an ImageHandler to display pictures added using the fileupload control to sql2005 server database as Binarydata.

    The pictures upload fine and are shown in the table however when I attempt to then display these pics all I get is red x , all the other fields associated are showing

    is it possible that my operating system "Windows7" could be also be causing problems with displaying pics?

    'this is my upload page PicDisplay.aspx

    Imports System.Data.SqlClient Imports System.Web.Profile Imports System Imports System.Data Imports System.Configuration Imports System.Web Imports System.Web.Security Partial Class PicDisplay Inherits System.Web.UI.Page Private sqlMyConnect As SqlConnection Private sqlMyCmd As SqlCommand Private sqlMyDataRead As SqlDataReader Private strSql As String Private Sub OpenConnection() Try Dim strConString As String sqlMyConnect = New SqlConnection() strConString = Web.Configuration.WebConfigurationManager.Connecti onStrings("myConnect").ToString sqlMyConnect.ConnectionString = strConString sqlMyConnect.Open() Catch sqlEx As SqlException Dim err As SqlError For Each err In sqlEx.Errors lblMessage.Text = "SQL Error " & _ err.Number & ": " & err.Message Next End Try End Sub Private Sub GiveCommand(ByVal sSql As String) sqlMyCmd = New SqlCommand() sqlMyCmd.Connection = sqlMyConnect sqlMyCmd.CommandType = sqlMyCmd.CommandType.Text 'sqlMyCmd.CommandType = sqlMyCmd.CommandType.Text sqlMyCmd.CommandText = sSql

    End Sub Private Sub EndConnect() 'sqlMyCmd.Dispose() ''sqlMyConnect.Close() 'sqlMyConnect.Dispose() End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Using myConnection As New SqlConnection(ConfigurationManager.ConnectionStrin gs("myConnect").ConnectionString) Const SQL As String = "INSERT INTO [Photos] ([AlbumID],[Name1],[Description],[pic],[ThumbnailHeight],[ThumbnailWidth],[DateCreated]) VALUES (@AlbumID,@Name1 , @Description , @pic , @ThumbnailHeight ,@ThumbnailWidth ,@DateCreated)" Dim myCommand As New SqlCommand(SQL, myConnection) Call OpenConnection() Dim AlbumID As Integer = Convert.ToInt32(AlbumID) 'myCommand.Parameters.AddWithValue("@PhotoID", txtPhotoId.Text.Trim()) myCommand.Parameters.AddWithValue("@AlbumID", txtAlbumId.Text.Trim()) myCommand.Parameters.AddWithValue("@Name1", txtName.Text.Trim()) myCommand.Parameters.AddWithValue("@Description", txtDesc.Text.Trim()) myCommand.Parameters.AddWithValue("@ThumbnailHeigh t", txtThmbHet.Text.Trim()) myCommand.Parameters.AddWithValue("@ThumbnailWidth ", txtThmbWdth.Text.Trim()) myCommand.Parameters.AddWithValue("@DateCreated", txtDate.Text.Trim()) 'Dim id As Integer = Convert.ToInt32(cmd.ExecuteScalar()) txtAlbumId.Text = String.Format("AlbumID is {0}", AlbumID) Dim filedata As Byte() = New Byte(FileUpload1.PostedFile.ContentLength - 1) {} Dim uploadedPostedFile As HttpPostedFile = FileUpload1.PostedFile() uploadedPostedFile.InputStream.Read(filedata, 0, CInt(FileUpload1.PostedFile.ContentLength)) myCommand.Parameters.AddWithValue("@pic", filedata) 'Call OpenConnection() myConnection.Open() Call GiveCommand(strSql) myCommand.ExecuteNonQuery() Call EndConnect() Me.lblMessage.Text = "You are now registered with Udigg" End Using End Sub End Class

    here is my code for PicPage.aspx

    Imports System.Data.SqlClient Imports System.Web.Profile Imports System Imports System.Data Imports System.Configuration Imports System.Web Imports System.Web.Security Partial Class Picpage Inherits System.Web.UI.Page Private sqlMyConnect As SqlConnection Private sqlMyCmd As SqlCommand Private sqlMyDataRead As SqlDataReader Private strSql As

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Programs must be written for people to read, and only incidentally for machines to execute.”