473,698 Members | 2,603 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Databind a PictureBox to a SQL Server...

Hi
I would like to databind a Picturebox to a SQL Server.
I have a typed Dataset "DSBoernekirken 1"
It has a table named "tpersoner" with a column "Photo"
I of course would like to bind the image directly to tpersoner.photo using
Properties (like I do with text fields), but this does not seem to be
possible.
Then I searched some news groups and Knowledgebase and found the following
code:
Sub ShowPicture()
'get the data from the database and put it in the dataset
OleDbDataAdapte r1.Fill(DataSet 11)
'get the byte array and convert it to a picture
Dim arrPicture() As Byte = CType(DataSet11 .Photos.picture , Byte())
Dim ms As New IO.MemoryStream (arrPicture)
'show the picture in the picturebox
PictureBox1.Ima ge = Image.FromStrea m(ms)
End Sub

I changed this to
Dim arrPicture() As Byte = CType(DsBoerneK irken1.tPersone r.PhotoColumn,
Byte()) ' Intellisense only allows Photocolumn, not Photo. Then I receive
the error "Value of type System.data.Dat acolumn can not be converted to
1-dimensional array of Byte"
' I have also tried
'Dim arrPicture2() As Byte =
CType(DsBoerneK irken1.tPersone rDataTable.Phot oColumn, Byte())
'Dim arrPicture3() As Byte = CType(DsBoerneK irken1.tPersone rDataTable.Phot o,
Byte())
' Dim arrPicture4() As Byte = CType(DsBoerneK irken1.tPersone r.Photo, Byte())

Dim ms As New IO.MemoryStream (arrPicture)
'show the picture in the picturebox
PictureboxPhoto .Image = Image.FromStrea m(ms)

Can anyone tell me what I am doing wrong?

*****'
I also found

Dim bytBLOBData() As Byte = ds.Tables("BLOB Test").Rows(c -
1)("BLOBData")
Dim stmBLOBData As New MemoryStream(by tBLOBData)
picBLOB.Image = Image.FromStrea m(stmBLOBData)

Which I changed to
Dim bytBLOBData() As Byte = DsBoerneKirken1 .tPersoner.Phot oColumn
Dim stmBLOBData As New MemoryStream(by tBLOBData)
PictureboxPhoto .Image = Image.FromStrea m(stmBLOBData)
With about the same error.

Best regards

Jan
Nov 20 '05 #1
17 4647
Cor
Hi Jan,

I think it has to do with the way you use the dataset
'Dim arrPicture3() As Byte = CType(......... ............... ..
DsBoerneKirken 1.tPersonerData Table.Photo


Maybe it can be something like
DsBoerneKirken1 .tPersonerDataT abele.rows(0)(" photo") and that is than the
first picture in your dataset.

But if that not works, you can try
DsBoerneKirken1 .tables(0).rows (0)("photo")

The rest was as far as I could see like has to be.

I hope this helps a little bit?

Cor
Nov 20 '05 #2
Hi Cor
Thanks for answering
So if I would like the picture from the current row to be shown do I have to
make my own pointer to insert in the ...rows(Current Row) code?

I receive an error
"Invalid Parameter Used" when I use rows(0).

Here is my exact code:
Dim arrPicture() As Byte = CType(DsBoerneK irken1.tPersone r.Rows(0)("phot o"),
Byte())
Dim ms As New IO.MemoryStream (arrPicture)
Try
PictureboxPhoto .Image = Image.FromStrea m(ms)
Catch ex As Exception
MsgBox(ex.Messa ge)
End Try

It is the "PictureboxPhot o.Image = Image.FromStrea m(ms)" line
that throws the exception.

Best regards

Jan

"Cor" <no*@non.com> skrev i en meddelelse
news:3f******** *************** @reader20.wxs.n l...
Hi Jan,

I think it has to do with the way you use the dataset
'Dim arrPicture3() As Byte = CType(......... ............... ..
DsBoerneKirken 1.tPersonerData Table.Photo


Maybe it can be something like
DsBoerneKirken1 .tPersonerDataT abele.rows(0)(" photo") and that is than the
first picture in your dataset.

But if that not works, you can try
DsBoerneKirken1 .tables(0).rows (0)("photo")

The rest was as far as I could see like has to be.

I hope this helps a little bit?

Cor

Nov 20 '05 #3
Cor
Hi Jan,

My "photo" was an example, maybe it is the same, but when I look at your
code it is then probably with an upercase. (it is the name of your photo
item in your SQL database (or the way you have named in your SQL select
script).

Cor
Here is my exact code:
Dim arrPicture() As Byte = CType(DsBoerneK irken1.tPersone r.Rows(0)("phot o"), Byte())
Dim ms As New IO.MemoryStream (arrPicture)
Try
PictureboxPhoto .Image = Image.FromStrea m(ms)
Catch ex As Exception
MsgBox(ex.Messa ge)
End Try

Nov 20 '05 #4
Hi Cor
Thanks again for your time.
"Photo" (with a capital P is the correct column name)
But I still receive the error even after correcting this.
And I am sure that the row contains a bitmap picture. (I can view it in MS
Access if I link the same table)

While searching newsgroups I found the solution or rather the problem:
"The code is correct. My guess is that the image you're getting was inserted
into the database either through Access or the VB OLE Container
control. If so, the image is wrapped with an OLE header. If the image is
stored purely as binary data, then this approach should work fine. "

I used Access to add picture.
I tried to add a picture from file using .Net code and then it works. But
then I cannot view it in Access. But I can live with that for the moment.

Jan


'

"Cor" <no*@non.com> skrev i en meddelelse
news:3f******** *************** @reader20.wxs.n l...
Hi Jan,

My "photo" was an example, maybe it is the same, but when I look at your
code it is then probably with an upercase. (it is the name of your photo
item in your SQL database (or the way you have named in your SQL select
script).

Cor
Here is my exact code:
Dim arrPicture() As Byte =

CType(DsBoerneK irken1.tPersone r.Rows(0)("phot o"),
Byte())
Dim ms As New IO.MemoryStream (arrPicture)
Try
PictureboxPhoto .Image = Image.FromStrea m(ms)
Catch ex As Exception
MsgBox(ex.Messa ge)
End Try


Nov 20 '05 #5
Cor
Hi Jan,
That I did not want to ask
..
What you was doing was reading a bitmap from the SQL database and then using
a memory stream loading it in the picture control.

But I am happy you found your solution.

Cor
Nov 20 '05 #6
Hi Cor,

I don't there's a solution yet. Do you remember that guy who wanted to
know how to get an image of of an OLE Wrapper? I think Jan's saying that he's
fallen into that tarpit as well. :-(

Regards,
Fergus
Nov 20 '05 #7
Cor
Hi Fergus,

I misunderstood the answer from Jan, it was early this morning and today I
had the same problem as you yesterday so I did it a little bit slowly today.

But I am sure you saw my messages about bitmaps in an other thread, the
brain connector for that is not yet connected. I saw this problem more as a
database problem, and that is solved I think.

So what is needed, that Jan gets a filter to skips that OLE header, maybe a
nice idea if Crirus looks to that, he had had such much help, that I think
he is best willing to give something back.

I am sure you know how to ask him.

:-)

Cor


Nov 20 '05 #8
Hi Cor,

I don't think this is one for Crirus as it's an OLE problem. It needn't be
a bitmap in the field, it could be a wave file, for example. The question is
how to open that damn wrapper. Who's got the OLE tin opener? ;-)

Unfortunately no solution came out of that other thread.

Regards,
Fergus
Nov 20 '05 #9
Hi again Cor
Now I have a working solution but I wonder: Is my way a smart way of doing
it?
The picturebox is not really bound to the SQL Server.

Here is how I do:
I have a public variable called CurrentRow that keeps track of the current
row (I bet that took you by surprise!)
Whenever the user clicks the (next, previous, first, last) button the
Currentrow variable is changed accordingly.

Example - Move to next row
Sub Nextrow
CurrentRow += 1
Me.BindingConte xt(DsBoerneKirk en1, "tPersoner").Po sition = CurrentRow
DsBoerneKirken1 _PositionChange d()
End sub
*************** **
Sub DsBoerneKirken1 _PositionChange d
Me.lblPostAfPos t.Text = (((Me.BindingCo ntext(DsBoerneK irken1,
"tPersoner").Po sition + 1).ToString + " af ") _
+ Me.BindingConte xt(DsBoerneKirk en1, "tPersoner").Co unt.ToString)

GetPicture()

End sub
*************** **
Sub GetPicture()
If DsBoerneKirken1 .tPersoner.Rows (CurrentRow)("P hoto") Is DBNull.Value
Then
PictureboxPhoto .Image = Nothing
Else
Dim arrPicture() As Byte =
CType(DsBoerneK irken1.tPersone r.Rows(CurrentR ow)("Photo"), Byte())
Dim ms As New IO.MemoryStream (arrPicture)
Dim im As Image = Image.FromStrea m(ms)
PictureboxPhoto .Image = im
End If
End Sub
*************** **
Saving a picture in the current row:
Private Sub btnIndsaetBille de
OpenFileGetPath ForPicture.Show Dialog()
Dim fs As New FileStream(Open FileGetPathForP icture.FileName ,
FileMode.OpenOr Create, FileAccess.Read )
Dim MyData(fs.Lengt h) As Byte
fs.Read(MyData, 0, fs.Length)
fs.Close()
DsBoerneKirken1 .tPersoner.Rows (CurrentRow)("P hoto") = MyData
SqlDA_tPersons. Update(DsBoerne Kirken1, "tPersoner" )
fs = Nothing
GetPicture() ' show the just added picture
End sub
*************** *************** ********
The picturebox is not bound to the sql server (at least not like the other
(textbox) controls.
It just "looks up" the picture
Is there a smarter way of doing this?

Best regards

Jan


"Cor" <no*@non.com> skrev i en meddelelse
news:3f******** *************** @reader21.wxs.n l...
Hi Jan,
That I did not want to ask
.
What you was doing was reading a bitmap from the SQL database and then using a memory stream loading it in the picture control.

But I am happy you found your solution.

Cor



Nov 20 '05 #10

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

Similar topics

3
12016
by: Marty McDonald | last post by:
I have <asp:Table... </asp:Table> on my webform. In codebehind, I populate a DataTable whose data should appear in the asp:Table. I created my own code to populate the asp:Table with the DataTable, then I discovered the asp:Table has a DataBind method. But the method takes no args and so I'm confused how to use it. Is there a link to see how DataBind works for asp:Table? Should I just use my own code anyway? Thanks... Here's my...
10
11601
by: aualias | last post by:
This seems like such a lame question, but how do you bind to a CheckBox with ASP.NET? With Windows Forms there is a DataBindings collection. I do not see it with the web version of the control, but there is a DataBinding event. Thanks. AU
0
1141
by: Bryce Budd | last post by:
Hi, I have a webform which has several server controls attached. The entire form is data bound to custom objects. The collection objects are cached using the Cache object. In my use case, I have to update my database, synchronize my cached collection and refresh my UI. After my database update I call dropdownlist1.databind() and my items are exactly the same when they should be less 1 item. If I understand the "control execution...
0
3879
by: akh | last post by:
I want to use de Drag and Drop ´s event to move a picture box from a form and within a Picture Box. But I have behaviour if the MyPBox As PictureBox as the Globale varible or not Thanks for your help, Here is the code.
3
63421
by: Tom | last post by:
I have a picturebox on my VB.NET form. The picturebox size mode is set to stretched. I then load an image into that form and display it. As the user moves the mouse over the form, I want to get and display (in the status bar) the image coordinates of the mouse location. However, if I use the picturebox's MouseMove event, I am getting the coordinates of the mouse over the PICTUREBOX, not the actual image underneath that (which is stretched)....
2
2389
by: jslaybaugh | last post by:
I am trying to bind to a GridView or DetailsView after a callback using the ClientCallback Manager in the new .NET 2.0 framework. See below for the HTML followed by the VB code: <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> <script type="text/javascript"> function GetEmployeeInfo(empID)
1
5894
by: =?Utf-8?B?cmxt?= | last post by:
I have standard gridview in an updatePanel with an AJAX timer that refreshes the data every 5 seconds by calling the grids DataBind method in the tick event. This works great. However, I have a checkbox on the web page which when checked I want to hide some rows. The code that does the hiding is in the RowDataBound event where I do a bunch of other stuff. This way every time the data is updated the rows are correctly displayed or hidden....
1
377
by: RN1 | last post by:
Consider the following code which binds records from a DB to a Repeater: <script runat="server"> Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs) Dim dSet As DataSet Dim sqlConn As SqlConnection Dim sqlDapter As SqlDataAdapter sqlConn = New SqlConnection(".....")
0
1437
by: amazingpeople | last post by:
I have a winform with one pictureBOx and one datanavigator control. I want to bind them to Northwind Employees table(Photo column) with the following code. I can see the images and can navigate through them also thru the datanavigator. But the images are not clear(hazy). can anybody help me out? --------------------------------------------------------------------- string connString = @"Data Source=local\SQLEXPRESS;Initial...
0
8680
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9030
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7738
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.