473,385 Members | 1,379 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,385 software developers and data experts.

How to display image in datagridview or picturebox from sqlserver?

Prathap
37
I am using vb.net.I want to display image in datagridview or picture box from sqlserver .Could someone please help me.
Dec 30 '11 #1
4 9458
Hi,
See the Below codes, it may help for you.

.aspx Part
<asp:TemplateColumn HeaderText="Image">
<ItemTemplate>
<asp:Image
Width="150" Height="125"
ImageUrl='<%# FormatURL(DataBinder.Eval(Container.DataItem, "ImageID")) %>'
Runat=server />
</ItemTemplate>
</asp:TemplateColumn>

.vb Part
Public Sub Page_Load(sender As Object, e As EventArgs)

Dim strImageID as String = Request.QueryString("id")

' Create Instance of Connection and Command Object
Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("C onnectionString"))
Dim myCommand As New SqlCommand("Select ImageType, Image from real_images Where ImageID=" & strImageID, myConnection)

' I have used the select statement. But it would be much much better, if you
'could write a small stored procedure which contains the above sql statement.
' Mark the Command as a SPROC (in case, if you wrote the stored procedure
'myCommand.CommandType = CommandType.StoredProcedure

Try
myConnection.Open()
Dim myDataReader as SqlDataReader
myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConne ction)
Do While (myDataReader.Read())
Response.ContentType = myDataReader.Item("ImageType")
Response.BinaryWrite(myDataReader.Item("Image"))
Loop
myConnection.Close()
Catch SQLexc As SqlException

End Try

End Sub
Dec 30 '11 #2
Prathap
37
Hi Sivaranjani,
Thanks for your concern.
I am new in vb.net. The image has been stored in sql server as <binary data> and i dont know how to convert it to display in picture box.Please tell me what are the changes should i made to get it to work.Here is the code which i have done so far
Expand|Select|Wrap|Line Numbers
  1. Imports System.Data.SqlClient
  2. Imports System.Data
  3. Public Class Edit_Employee
  4.     Dim str As String = "Data Source=NET3\SQLEXPRESS;Initial Catalog=restpos;Persist Security Info=True;User ID=sa;Password=password"
  5.     Dim ds As New DataSet
  6.     Dim adp As New SqlDataAdapter
  7.     Dim con As New SqlClient.SqlConnection
  8. Private Sub Edit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  9.      adp = New SqlDataAdapter("select Emp_Image from Employees where Emp_UserId=106",con)
  10.      con.ConnectionString = str  
  11.      adp.Fill(ds)      
  12.      PictureBox1.Image = ds.Tables(0).Rows(0)
  13.   End sub   
  14. End Class 
Dec 31 '11 #3
Prathap
37
Getting error: Parameter is not valid,could someone please help.

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
con.ConnectionString = str
adp = New SqlDataAdapter("select Emp_Image from Employees where Emp_UserId='1' ", con)
adp.Fill(dsimage)
Dim saveImage As IO.MemoryStream
Dim imgByte As Byte()
If dsimage.Tables.Count > 0 Then
imgByte = dsimage.Tables(0).Rows(0)("Emp_Image")
saveImage = New IO.MemoryStream(imgByte)
PictureBox2.Image = Image.FromStream(saveImage)->Here i am getting error(parameter is not valid)
End If
End Sub
Jan 5 '12 #4
LoanB
62
Hi, Have a look at this:

Expand|Select|Wrap|Line Numbers
  1. ''' <summary>
  2. ''' Convert a byte array to an Image
  3. ''' </summary>
  4. ''' <param name="NewImage">Image to be returned</param>
  5. ''' <param name="ByteArr">Contains bytes to be converted</param>
  6. ''' <remarks></remarks>
  7. Public Sub Byte2Image(ByRef NewImage As Image, ByVal ByteArr() As Byte)
  8.   '
  9.   Dim ImageStream As MemoryStream
  10.  
  11.   Try
  12.     If ByteArr.GetUpperBound(0) > 0 Then
  13.       ImageStream = New MemoryStream(ByteArr)
  14.       NewImage = Image.FromStream(ImageStream)
  15.     Else
  16.       NewImage = Nothing
  17.     End If
  18.   Catch ex As Exception
  19.     NewImage = Nothing
  20.   End Try
  21.  
  22. End Sub



Source and Credit to: http://windevblog.blogspot.com/2008/...-and-vice.html
Jan 5 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Anup Jishnu via .NET 247 | last post by:
(Type your message here) -------------------------------- From: Anup Jishnu I am trying to display animage in a picturebox in VB.net for Windows CE.net The Image I have is in a byte array. How...
1
by: Dev | last post by:
Dear Friends, I am passing the image name, size (in bytes) and imgaeformat (like jpg or bmp or pdf) through the network. I want display the image into picturebox without saving image...
0
by: Qindong Zhang | last post by:
My asp.net application send image to web browser. The browser begin to display image before download finished. (In Slow Network). How can I force brower to display image after the image downloaded? ...
1
by: Tan | last post by:
Hi I am desperate for any help with display image in Gridview I have a gridview contain surname , forename ..... and image. I could not display image as my database store the column image as...
2
by: David | last post by:
I am trying to get an image to appear on all Mondays within the calendar control. I also want that image to be a link. How can I do this?
3
by: den 2005 | last post by:
Hi everyone, Here is code working on..Trying to insert record with a column with Image or VarBinary datatype in sql database from a existing jpeg image file, then retrieve this image from...
1
by: veer | last post by:
Hi i want to save image from picturebox into sql but i got the error 'ARTHEMATIC OPERATIONS RESULTED IN AN OVERFLOW' On line bytBLOBData = New Byte() {ms.Length} here is my code...
1
by: randyson | last post by:
i am working a "image processing system" project in vb.net using the ms access2007.i have to browse by clicking a button and open a image to display its name to a text box and that image to...
1
by: aznimah | last post by:
HI, i need help on how to draw a square dot when fire mouse click: -onto image in picturebox -the dot size is 10px X 10px -and return the position of dots (x,y) values. here i try some...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.