By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,223 Members | 1,371 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,223 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

Nov 20 '05 #2

P: n/a
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
Nov 20 '05 #3

P: n/a
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
///


Nov 20 '05 #4

P: n/a
Very many thanks for an idea....

Volodya Sukhanov
Nov 20 '05 #5

P: n/a
* "Sukhanov Volodya" <su********@wline.ru> scripsit:
Very many thanks for an idea....


On which topic?

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.