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

How to display image in datagridview or picturebox from sqlserver?

Prathap
P: 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
Share this Question
Share on Google+
4 Replies


P: 16
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
P: 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
P: 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

P: 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

Post your reply

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