Click here to Skip to main content
16,012,166 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I new to asp.net. I am creating a web application. The purpose of this application is that it should display the datas and images from database in Gridview and on "Click" of "Selct" option in Gridview, the datas in database must be displayed in textbox and image must be displayed in image control.

The datas and image are stored in database using vb.net application and image is stored as Filepath.

The Filepath for the image is the vb.net application "start up point"

i.e some think like

"C:\Documents and Settings\sathish\Desktop\application name\application name folder\bin\Debug\..\..\" & imagesfoldername\filename.

Exactly it will be

"C:\Documents and Settings\sathish\Desktop\RegIR_NEW\RegIR\bin\Debug\..\..\Images\6-Q-IMG.jpg"

When I click on retrieve button which I created in "asp.net" application, In Gridview I am getting the image path as said above. In gridview properties I have selected the "Auto Generate Select Button - TRUE"

MY QUESTION IS WHEN I CLICK ON THE "selct" option in Gridview, I must get the image in Image control.

All other data like text I can get in textbox by using "Gridview SelectedIndex" Event.

IS THERE ANY CODING EXAMPLE THAT CAN READ THE FILEPATH AND PROVIDE ME THE IMAGE IN "image control"

I have used memory stream in vb.net to show image in picture box. But I cannot follow this here?

Can any one help me on this?

My coding in vb.net to retrieve image is:

Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim i As Integer
i = DataGridView1.CurrentRow.Index
If (i = DataGridView1.Rows.Count - 1) Then
Exit Sub
End If
Me.TextBox3.Text = DataGridView1.Item(1, i).Value
Me.TextBox4.Text = DataGridView1.Item(2, i).Value

Try
If Not IsDBNull(DataGridView1.Rows(e.RowIndex).Cells("Qimage").Value) Then


'Get image data from gridview column.



''Initialize image variable
Dim newImage As Image
'Read image data into a memory stream
Using ms As New MemoryStream(ReadFile(DataGridView1.Rows(e.RowIndex).Cells("Qimage").Value), 0, ReadFile(DataGridView1.Rows(e.RowIndex).Cells("Qimage").Value).Length)
ms.Write(ReadFile(DataGridView1.Rows(e.RowIndex).Cells("Qimage").Value), 0, ReadFile(DataGridView1.Rows(e.RowIndex).Cells("Qimage").Value).Length)

'Set image variable value using memory stream.
newImage = Image.FromStream(ms, True)
End Using

'set picture
PictureBox1.Image = newImage
End If

Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
End Sub



VB
Private Function ReadFile(ByVal sPath As String) As Byte()
       'Initialize byte array with a null value initially.
       Dim data As Byte() = Nothing

       'Use FileInfo object to get file size.
       Dim fInfo As New FileInfo(sPath)
       Dim numBytes As Long = fInfo.Length

       'Open FileStream to read file
       Dim fStream As New FileStream(sPath, FileMode.Open, FileAccess.Read)

       'Use BinaryReader to read file stream into byte array.
       Dim br As New BinaryReader(fStream)

       'When you use BinaryReader, you need to supply number of bytes to read from file.
       'In this case we want to read entire file. So supplying total number of bytes.
       data = br.ReadBytes(CInt(numBytes))
       Return data
   End Function
Posted
Updated 29-Nov-13 0:34am
v3
Comments
Suhani Mody 29-Nov-13 5:34am    
Can't you put an img tag where you want to display this image and make its "src" property dynamic to fetch it from your code?
Member 10419145 29-Nov-13 5:38am    
Hi,

I am new to asp.net and do not know how to use that can you please me?

Get This Value from the above String Images\6-Q-IMG.jpg" //use string split

and Add Following Lines


string n= "~/Images/6-Q-IMG.jpg";

Image1.ImageUrl=n;

you need detailed solution.. upload your code
 
Share this answer
 
Comments
Member 10419145 29-Nov-13 6:35am    
My coding in vb.net to retrieve image is:

Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim i As Integer
i = DataGridView1.CurrentRow.Index
If (i = DataGridView1.Rows.Count - 1) Then
Exit Sub
End If
Me.TextBox3.Text = DataGridView1.Item(1, i).Value
Me.TextBox4.Text = DataGridView1.Item(2, i).Value

Try
If Not IsDBNull(DataGridView1.Rows(e.RowIndex).Cells("Qimage").Value) Then


'Get image data from gridview column.



''Initialize image variable
Dim newImage As Image
'Read image data into a memory stream
Using ms As New MemoryStream(ReadFile(DataGridView1.Rows(e.RowIndex).Cells("Qimage").Value), 0, ReadFile(DataGridView1.Rows(e.RowIndex).Cells("Qimage").Value).Length)
ms.Write(ReadFile(DataGridView1.Rows(e.RowIndex).Cells("Qimage").Value), 0, ReadFile(DataGridView1.Rows(e.RowIndex).Cells("Qimage").Value).Length)

'Set image variable value using memory stream.
newImage = Image.FromStream(ms, True)
End Using

'set picture
PictureBox1.Image = newImage
End If

Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
End Sub



<pre lang="vb">Private Function ReadFile(ByVal sPath As String) As Byte()
'Initialize byte array with a null value initially.
Dim data As Byte() = Nothing

'Use FileInfo object to get file size.
Dim fInfo As New FileInfo(sPath)
Dim numBytes As Long = fInfo.Length

'Open FileStream to read file
Dim fStream As New FileStream(sPath, FileMode.Open, FileAccess.Read)

'Use BinaryReader to read file stream into byte array.
Dim br As New BinaryReader(fStream)

'When you use BinaryReader, you need to supply number of bytes to read from file.
'In this case we want to read entire file. So supplying total number of bytes.
data = br.ReadBytes(CInt(numBytes))
Return data
End Function</pre>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900