I tested it and it works, I used almost the same method, here's the complete code....
'ASPX code.....
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="TestImageFromDatabaseSQL.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR">
<meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:placeholder id="PlaceHolder1" runat="server"></asp:placeholder></form>
</body>
</HTML>
'ASP code
Imports System.Data.SqlClient
Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents PlaceHolder1 As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents Button1 As New System.Web.UI.WebControls.Button()
#Region " Web Form Designer Generated Code "
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
PlaceHolder1.Controls.Add(Button1)
Button1.Text = "Display Image"
Dim nid As Integer = Convert.ToInt32(Request.QueryString("id"))
Dim con As New SqlConnection("server=127.0.0.1;Trusted_Connection=yes;database=NorthWind")
Dim cmd As New SqlClient.SqlCommand("SELECT Image FROM Images WHERE ID = " & nid, con)
If con.State = ConnectionState.Closed Then con.Open()
Dim dr As SqlClient.SqlDataReader = cmd.ExecuteReader()
If dr.Read() Then
Response.ContentType = "image/JPEG"
Response.BinaryWrite(CType(dr("Image"), Byte()))
End If
If Not con.State = ConnectionState.Closed Then con.Close()
If Not dr.IsClosed Then dr.Close()
If Not cmd Is Nothing Then cmd.Dispose()
Catch ex As Exception
Response.Write(ex.ToString)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Response.Redirect("http://localhost/TestImageFromDatabaseSQL/WebForm1.aspx?id=6")
End Sub
End Class