Problems Displaying an image retrieved from SQL Se
Hello and thanks for taking a moment to read this message. I have a table in SQL server where I have inserted images with a Stream object and the images are stored in a table as Binary. What I am trying to do is retrieve the correct image via a querystring id that is passed to a page with a datagrid. Then I want to display it in the data grid or some other server side control. I have two pages in order to do this.
The first page has the data grid. It selects the image. It calls a function that passes an argrument (the id of the image) to another page. It then takes the data returned by the other page and binds it to the data grid. The code in the first page with the data grid looks like this. <Script Runat="Server">
Public Sub Page_Load(sender As Object, e As EventArgs)
If Not Page.IsPostBack Then
BindGrid()
End If
End Sub
Sub BindGrid()
Dim intID3 As Integer
intID3 = Int32.Parse( Request.QueryString( "id" ) )
Dim myConnection = New SqlConnection ( "Server=localhost;uid=jazzcatone;pwd=funkdafied;da tabase=Beatles" )
Dim myCommand = New SqlCommand("usp_retrieve_pic", myConnection)
myCommand.CommandType = CommandType.StoredProcedure
myCommand.Parameters.Add( "@ID", intID3 )
Try
myConnection.Open()
DG_Persons.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConne ction)
DG_Persons.DataBind()
Catch SQLexc As SqlException
Response.Write("Error occured while Generating Data. Error is " & SQLexc.ToString())
End Try
End Sub
Function FormatURL(strArgument) as String Return ("readrealimages.aspx?id=" & strArgument)
End Function
</Script>The second page ( called readrealimages.aspx) simply harnesses the image and writes it.
<Script Runat ="Server">
Public Sub Page_Load(sender As Object, e As EventArgs)
Dim strImageID as String = Request.QueryString("id")
Dim myConnection = New SqlConnection( "Server=localhost;uid=jazzcatone;pwd=funkdafied;da tabase=Beatles" )
Dim myCommand As New SqlCommand("Select * from Pictures Where ID=" & strImageID, myConnection)
Try
myConnection.Open()
Dim myDataReader as SqlDataReader
myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConne ction)
Do While (myDataReader.Read())
Response.ContentType = myDataReader.Item("Image_Type")
Response.BinaryWrite(myDataReader.Item("Image_NAME "))
Loop
myConnection.Close()
Catch SQLexc As SqlException
End Try
End Sub
</Script>
Everything is processed, (I think)the page runs ( meaning it doesn't crash) but no image is displayed. I am new with working with images in SQL Server. Can someone tell me what is wrong with my code, or if there is an easier way to do this? (Perhaps with a path to the image and a Response.Write ????) Any suggestions would be appreciated . If you need a more in depth explanation, please feel free to ask.
Best Regards,
Jason
|