472,142 Members | 1,302 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,142 software developers and data experts.

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

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
Nov 20 '05 #1
5 16178
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
PictureboxPhoto.Image = Nothing
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
Dim fs As New FileStream(OpenFileGetPathForPicture.FileName,
FileMode.OpenOrCreate, FileAccess.Read)
Dim MyData(fs.Length) As Byte
fs.Read(MyData, 0, fs.Length)
DS1.table(0).Rows(CurrentRow)("Photo") = MyData
fs = Nothing
GetPicture() ' show the just added picture
End sub
I hope this helps a 1/8 byte?


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

Can do it ?

Volodya Sukhanov
Nov 20 '05 #3
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.


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
Me.Image1.Visible = False
ListBox1.AutoPostBack = True
ListBox1.DataSource = dsPictures.Tables(0)
ListBox1.DataTextField = "FileName"
ListBox1.DataValueField = "PictureID"
Catch sqlExc As SqlException
Me.Label1.Text = "Database Error" 'sqlExc.ToString
Catch exc As Exception
Me.Label1.Text = "Datbase Connection Failed!"
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/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)
Dim rdr As SqlDataReader = cmd.ExecuteReader()
Response.BinaryWrite(CType(rdr.Item("Picture"), Byte()))
End Sub

Nov 20 '05 #4
Very many thanks for an idea....

Volodya Sukhanov
Nov 20 '05 #5
* "Sukhanov Volodya" <su********@wline.ru> scripsit:
Very many thanks for an idea....

On which topic?

Herfried K. Wagner
MVP · VB Classic, VB.NET
Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by dave | last post: by
7 posts views Thread by Tom | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.