I am using vb.net.I want to display image in datagridview or picture box from sqlserver .Could someone please help me.
How to display image in datagridview or picturebox from sqlserver?
Collapse
X
-
Hi,
See the Below codes, it may help for you.
.aspx Part
<asp:TemplateCo lumn HeaderText="Ima ge">
<ItemTemplate >
<asp:Image
Width="150" Height="125"
ImageUrl='<%# FormatURL(DataB inder.Eval(Cont ainer.DataItem, "ImageID")) %>'
Runat=server />
</ItemTemplate>
</asp:TemplateCol umn>
.vb Part
Public Sub Page_Load(sende r As Object, e As EventArgs)
Dim strImageID as String = Request.QuerySt ring("id")
' Create Instance of Connection and Command Object
Dim myConnection As New SqlConnection(C onfigurationSet tings.AppSettin gs("ConnectionS tring"))
Dim myCommand As New SqlCommand("Sel ect 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.Comm andType = CommandType.Sto redProcedure
Try
myConnection.Op en()
Dim myDataReader as SqlDataReader
myDataReader = myCommand.Execu teReader(Comman dBehavior.Close Connection)
Do While (myDataReader.R ead())
Response.Conten tType = myDataReader.It em("ImageType" )
Response.Binary Write(myDataRea der.Item("Image "))
Loop
myConnection.Cl ose()
Catch SQLexc As SqlException
End Try
End Sub -
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 farCode:Imports System.Data.SqlClient Imports System.Data Public Class Edit_Employee Dim str As String = "Data Source=NET3\SQLEXPRESS;Initial Catalog=restpos;Persist Security Info=True;User ID=sa;Password=password" Dim ds As New DataSet Dim adp As New SqlDataAdapter Dim con As New SqlClient.SqlConnection Private Sub Edit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load adp = New SqlDataAdapter("select Emp_Image from Employees where Emp_UserId=106",con) con.ConnectionString = str adp.Fill(ds) PictureBox1.Image = ds.Tables(0).Rows(0) End sub End ClassComment
-
Getting error: Parameter is not valid,could someone please help.
Private Sub Button4_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button4.Click
con.ConnectionS tring = str
adp = New SqlDataAdapter( "select Emp_Image from Employees where Emp_UserId='1' ", con)
adp.Fill(dsimag e)
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.Ima ge = Image.FromStrea m(saveImage)->Here i am getting error(parameter is not valid)
End If
End SubComment
-
Hi, Have a look at this:
Code:''' <summary> ''' Convert a byte array to an Image ''' </summary> ''' <param name="NewImage">Image to be returned</param> ''' <param name="ByteArr">Contains bytes to be converted</param> ''' <remarks></remarks> Public Sub Byte2Image(ByRef NewImage As Image, ByVal ByteArr() As Byte) ' Dim ImageStream As MemoryStream Try If ByteArr.GetUpperBound(0) > 0 Then ImageStream = New MemoryStream(ByteArr) NewImage = Image.FromStream(ImageStream) Else NewImage = Nothing End If Catch ex As Exception NewImage = Nothing End Try End Sub
Source and Credit to: http://windevblog.blogspot.com/2008/...-and-vice.htmlComment
Comment