Viewing Binary Image from SQL !

  • Thread starter Thread starter Ola Myrgart
  • Start date Start date
O

Ola Myrgart

Hi !

I wonder if there is a simple solution to how to dynamically view an
image from a SQL DB. It´s a binary file and the insertfunction into SQL
works fine. I can easily view all the text from the SQL DB. I can also
view the image separatly.

I load the text in a separate file : details.aspx
I load the image in another file : showpic.aspx

In the details.aspx file I put an <img> tag to the showpic.aspx as
follows:

<asp:Image id="Image" runat="server"
ImageUrl="showpic.aspx?id=22"></asp:Image>

In the above example the imagefile is static "id=22". How can I make it
dynamic so that the "id" changes when I click on details in the
Datagrid? How does the syntax look like ?

Help much needed !

OM "Myggan"
 
Hi Ola,

I did make once this it is with a button, but a datagrid would do it also of course,

\\\
There has to be an imagebox, a button and a label on the 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 SqlConnection(connStr)
Dim cmd As New SqlCommand("SELECT FileName, PictureID FROM Picture", conn)
da = New SqlDataAdapter(cmd)
cbd = New SqlCommandBuilder(da)
dsPictures = New DataSet
da.Fill(dsPictures)
Me.Image1.Visible = False
ListBox1.AutoPostBack = True
Try
ListBox1.DataSource = dsPictures.Tables(0)
ListBox1.DataTextField = "FileName"
ListBox1.DataValueField = "PictureID"
ListBox1.DataBind()
Catch sqlExc As SqlException
Me.Label1.Text = "Database Error" 'sqlExc.ToString
Catch exc As Exception
Me.Label1.Text = "Datbase Connection Failed!"
End Try
conn.Close()
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/testSQLPictures/WebForm2.aspx"
End Sub
///
\\\
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim conn As New SqlConnection(connStr)
Dim sqlstr As String = String.Format("SELECT Picture FROM Picture WHERE (PictureID = {0})", CInt(Session.Item("img")))
Dim cmd As New SqlCommand(sqlstr, conn)
conn.Open()
Dim rdr As SqlDataReader = cmd.ExecuteReader()
rdr.Read()
Response.BinaryWrite(CType(rdr.Item("Picture"), Byte()))
rdr.Close()
conn.Close()
End Sub
///


I hope this helps a little bit?

Cor
 
Hi !

Thank you for your answer. It did help a bit. I tried this codesnippet:

strselect = String.Format("SELECT Image FROM Kunder WHERE (@KundID =
{0})", CInt(Session.Item("Image")))

It worked but it only shows the first image in the SQL databaes no
matter what link in the DataGrid I click on. The referensID that is
KundID, doesn´t change it seems so the same picture comes up on every
click.

Any ideas ??

OM "Myggan"
 
Back
Top