How can I show an image field SQL Server using VB.Net ?

S

Sukhanov Volodya

Hello, all!
Does anybody knows, how I can show an image field SQL Server using VB. NET ?

I have an database, which has an table with an field which has type 'image'
(the type of field)
I want to save an picture in this field, and then show this picture by an
datagrid or other control.... (for each record desirable)
Does anybody knows how to do it ?

Volodya Sukhanov
 
C

Cor

Hi Sukhanov

Some code Jan send in to this newsgroup some time ago, after I had given
him a start example, so this is more complete, I changed it a little bit to
make it more common but did not test it till now, so there can be an error
in it, but basicly I find it a good example.

\\\
Sub GetPicture()
If Ds1.table(0).Rows(CurrentRow)("Photo") Is DBNull.Value
Then
PictureboxPhoto.Image = Nothing
Else
Dim arrPicture() As Byte =
CType(Ds1.table(0).Rows(Index)("Photo"), Byte())
Dim ms As New IO.MemoryStream(arrPicture)
Dim im As Image = Image.FromStream(ms)
PictureboxPhoto.Image = im
End If
End Sub
///
\\\
Saving a picture in the current row:
Private Sub button1
OpenFileGetPathForPicture.ShowDialog()
Dim fs As New FileStream(OpenFileGetPathForPicture.FileName,
FileMode.OpenOrCreate, FileAccess.Read)
Dim MyData(fs.Length) As Byte
fs.Read(MyData, 0, fs.Length)
fs.Close()
DS1.table(0).Rows(CurrentRow)("Photo") = MyData
SqlDA.Update(Ds1)
fs = Nothing
GetPicture() ' show the just added picture
End sub
///
I hope this helps a 1/8 byte?

Cor
 
S

Sukhanov Volodya

Many thanks.... but I probably wrote incorrect.... :(
I want to represent content of this field on my web page using ASP .NET and
VB. NET
and wanted to get data from the field 'image'....

Can do it ?

Volodya Sukhanov
 
C

Cor

Hi Suhkanov

This example I made myself.
Because you can only show an image on a webpage from an Url, there is first
sended a picture webpage as webform2 with only a picture. This page is the
image for the webform1.

In this way you can put as much pictures on a page as you want, depending of
course that you make the same amount of pseudo pages (and don't give them
the same name).

I hope it will be a nice pages.

Cor

\\\
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/testSQLPlaatjesWeb/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
///
 

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