473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 16357
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
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
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
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
<http://www.mvps.org/dotnet>
Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
2
by: Bob | last post by:
Hello: I've tried showing an image field from an MS SQL Server 2000 image field on an ASP page but All I get is this http://http://new.confectioneryhouse.com/glutenfree/about.asp I added the...
7
by: Tom | last post by:
hello friends I want to specify a field to save icons I want to give this field a maximum of 120kb for each icon to be stored in the db. what datatype should I assign to this field and how do...
6
by: Steve Speirs | last post by:
Hi I'm trying to show/hide a simple piece of text and a text field on a form based on what choice is made from a drop down box. <select name="dropdown" size="1"> <option selected...
1
by: John Scott | last post by:
I am storing an image in an SQL database and have one field as an image datatype. I am also using a webservice to transport data. I want to be able to resize the image and pass back a thumbnail...
2
by: J Sahoo | last post by:
Hi, I have a browse page where I am showing few fields from my SQL Server table. One of the field is Image type, all the fields are bound to a single table. I want to show the image from the...
7
by: Neo Geshel | last post by:
Greetings. I have managed to stitch together an awesome method of posting text along with an image to a database, in a way that allows an unlimited number of previews to ensure that text and...
3
by: | last post by:
I'm using the DataList and GridView controls, and I am trying to wrap my head around the problem of conditionally showing or hiding cells/cell content based on the presence or absence of DB data. I...
3
by: premprakashbhati | last post by:
hi, good evening.. i am going to upload an image in a web form .....for that iam using HTML input(file) control and one web control button i.e., Upload_Button() here is the code ...its work fine...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.