TechGen 0 Newbie Poster

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 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
'Dim AlbumID As Integer
'Label1.Text = Request.QueryString(AlbumID)
'sqlMyCmd.Parameters.AddWithValue("@AlbumID", Label1.Text.Trim())
End Sub
End Class


and here is my code for the Imagehandler

showImage.ashx

<%@ WebHandler Language="VB" Class="ShowImage" %>

Imports System
Imports System.Configuration
Imports System.Web
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.ComponentModel

Public Class ShowImage
Implements IHttpHandler
Private seq As Long = 0
Private empPic() As Byte = Nothing

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim AlbumID As Int32
If context.Request.QueryString("AlbumID") IsNot Nothing Then
AlbumID = Convert.ToInt32(context.Request.QueryString("Album ID"))

'Else
' Throw New ArgumentException("No parameter specified")
End If

' Convert Byte[] to Bitmap
Dim newBmp As Bitmap = ConvertToBitmap(ShowAlbumImage(AlbumID))
If newBmp IsNot Nothing Then
newBmp.Save(context.Response.OutputStream, ImageFormat.Gif)
newBmp.Dispose()
End If

End Sub
Protected Function ConvertToBitmap(ByVal bmp() As Byte) As Bitmap
If bmp IsNot Nothing Then
Dim tc As TypeConverter = TypeDescriptor.GetConverter(GetType(Bitmap))
Dim b As Bitmap = CType(tc.ConvertFrom(bmp), Bitmap)
Return b
End If
Return Nothing
End Function
Public Function ShowAlbumImage(ByVal picid As Integer) As Byte()
Dim AlbumID As Int32
Dim conn As String = ConfigurationManager.ConnectionStrings("myConnect" ).ConnectionString
Dim connection As New SqlConnection(conn)
Dim sql As String = "SELECT [AlbumID], Name1, pic FROM Photos WHERE AlbumID = @AlbumID"
Dim cmd As New SqlCommand(sql, connection)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@AlbumID", SqlDbType.Int).Value = AlbumID
'cmd.Parameters.Add("@AlbumId", SqlDbType.Int).Value = context.Request.QueryString["id"];
Try
connection.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
If dr.Read() Then
seq = dr.GetBytes(0, 0, Nothing, 0, Integer.MaxValue) - 1
empPic = New Byte(seq) {}
dr.GetBytes(0, 0, empPic, 0, Convert.ToInt32(seq))
connection.Close()
End If

Return empPic

Catch
Return Nothing
Finally
connection.Close()
End Try
End Function


Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property

End Class

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.