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