display images from SQL server in datareader

S

sean

Hi There,

I am trying to display some images from SQL Server, the image is displaying
correctly, but when I have more than one row in the table I only get one
image displayed? What am I doing wrong? is there a better way to do this?

Sean - thanks in advance


<%@ Page Language="vb" %>

<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System.Data.SqlClient" %>

<HTML>
<HEAD>
<title>Retrieving Image from the Sql Server</title>
<script runat=server>
Public Sub Page_Load(sender As Object, e As
EventArgs)
Dim myConnection As New
SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
Dim myCommand As New SqlCommand("Select
ListingImage,ListingImageType from tblListings", myConnection)
Try
myConnection.Open()
Dim myDataReader as
SqlDataReader
myDataReader =
myCommand.ExecuteReader(CommandBehavior.CloseConnection)
Do While
(myDataReader.Read())
'Dim bindata() As
Byte = myDataReader.Item("ListingImage")
'Response.BinaryWrite(bindata)

Response.BinaryWrite(myDataReader.Item("ListingImage"))

'Response.BinaryWrite(myDataReader.Item("ListingImageType"))

Loop
myConnection.Close()
Response.Write("image
displayed")
Catch SQLexc As SqlException
Response.Write("Read Failed
: " & SQLexc.ToString())
End Try
End Sub
</script>
</HEAD>
<body style="font: 10pt verdana">
</body>
</HTML>
 
C

Cor Ligthert

Hi Sean,

I expect you only see the last one, which is displayed in your browser as a
bitmap.

To have more, you can look for this example, however this is only the start
for the solution for your problem. You can show only one image as url in a
http imagebox.

The idea is that you redirect an image as a page and take that as an url for
an imagebox.

I hope this helps?

Cor


\\\For the database the image database sample from the Resource kit.
\\\It needs 2 forms with a listbox, a picturebox and a label on form1
\\\webform1
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim conn As New SqlClient.SqlConnection _
("Server=localhost;" & "DataBase=Northwind;" & _
"Integrated Security=SSPI")
Dim da As New SqlClient.SqlDataAdapter _
("SELECT FileName, PictureID FROM Picture", conn)
Dim ds As New DataSet
Me.Image1.Visible = False
ListBox1.AutoPostBack = True
Try
da.Fill(ds)
ListBox1.DataSource = ds.Tables(0)
ListBox1.DataTextField = "FileName"
ListBox1.DataValueField = "PictureID"
ListBox1.DataBind()
Catch sqlExc As SqlClient.SqlException
Me.Label1.Text = sqlExc.ToString
Catch exc As Exception
Me.Label1.Text = exc.ToString
End Try
End If
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles ListBox1.SelectedIndexChanged
Session.Item("img") = ListBox1.SelectedItem.Value
Image1.Visible = True
Image1.ImageUrl = "http://localhost/WebImage/WebForm2.aspx"
End Sub
///
\\\Webform1
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim conn As New SqlClient.SqlConnection("Server=localhost;" & _
"DataBase=Northwind;" & "Integrated Security=SSPI")
Dim sqlstr As String = _
String.Format("SELECT Picture FROM Picture WHERE (PictureID = {0})", _
CInt(Session.Item("img")))
Dim cmd As New SqlClient.SqlCommand(sqlstr, conn)
conn.Open()
Dim rdr As SqlClient.SqlDataReader = cmd.ExecuteReader()
rdr.Read()
Dim arrImage() As Byte
arrImage = (CType(rdr.Item("Picture"), Byte()))
Dim ms1 As New System.IO.MemoryStream(arrImage)
Dim origimage As System.drawing.Image
origimage = System.Drawing.Image.FromStream(ms1)
Dim PThumbnail As System.drawing.Image
PThumbnail = origimage.GetThumbnailImage(100, 100, Nothing, New IntPtr)
Dim ms2 As New System.IO.MemoryStream
PThumbnail.Save(ms2, Imaging.ImageFormat.Bmp)
arrImage = ms2.GetBuffer
Response.BinaryWrite(arrImage)
rdr.Close()
conn.Close()
End Sub
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top