Having problems getting images to show in a datagrid using VB.NET
Tried writing the code and using Visual Studio
Pasted the code, hope that it is ok to do that!!
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim strDatabaseNameAndLocation As String
strDatabaseNameAndLocation = Server.MapPath("shop.mdb")
Dim strSQLCommand As String
strSQLCommand = "SELECT Product.* FROM Product ORDER BY Product.productID ASC;"
Dim objOleDbConnection As System.Data.OleDb.OleDbConnection
objOleDbConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & strDatabaseNameAndLocation)
objOleDbConnection .Open()
Dim objOleDbCommand As System.Data.OleDb.OleDbCommand
objOleDbCommand = New System.Data.OleDb.OleDbCommand(strSQLCommand, objOleDbConnection)
Dim objOleDbDataReader As System.Data.OleDb.OleDbDataReader
objOleDbDataReader = objOleDbCommand.ExecuteReader()
gdvproducts.DataSource = objOleDbDataReader
Dim datDataTable As System.Data.DataTable
datDataTable = New System.Data.DataTable()
datDataTable.Load(objOleDbDataReader)
gdvproducts.DataSource = datDataTable
gdvproducts.DataBind()
objOleDbConnection .Close()
End Sub
Protected Sub gdvproducts_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Protected Sub Page_Load(sender As Object, e As System.EventArgs)
Dim strDatabaseNameAndLocation As String
strDatabaseNameAndLocation = Server.MapPath("shop.mdb")
Dim strSQLCommand As String
strSQLCommand = "SELECT Product.* FROM Product ORDER BY Product.productID ASC;"
Dim objOleDbConnection As System.Data.OleDb.OleDbConnection
objOleDbConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & strDatabaseNameAndLocation)
objOleDbConnection.Open()
Dim objOleDbCommand As System.Data.OleDb.OleDbCommand
objOleDbCommand = New System.Data.OleDb.OleDbCommand(strSQLCommand, objOleDbConnection)
Dim objOleDbDataReader As System.Data.OleDb.OleDbDataReader
objOleDbDataReader = objOleDbCommand.ExecuteReader()
gdvproducts.DataSource = objOleDbDataReader dvproducts.DataBind() objOleDbConnection.Close()
End Sub
Please note. You can avoid repeating System.Data.OleDb in every line if you add the namespace "System.Data.OleDb" to the page.
Kindly mark this post as "Answer", if it helped you.
In your select "SELECT Product.* FROM Product ORDER BY Product.productID ASC;" what the format that the picture return, is the filename from image or another data ?
Kindly mark this post as "Answer", if it helped you.
Check this sample code. compare with yours and correct if necessary.My images are stored in
img folder of my site My table tblpics contains 2 fields: username and ImageName. In imagename field image paths are stored as
img/id001.jpg. img/id002.jpg and so on..
Sub fillGV()
Dim strDatabaseNameAndLocation As String
strDatabaseNameAndLocation = Server.MapPath("~/App_Data/basic.mdb")
Dim strSQLCommand As String
strSQLCommand = "SELECT * FROM tblpics"
Dim objOleDbConnection As System.Data.OleDb.OleDbConnection
objOleDbConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & strDatabaseNameAndLocation)
objOleDbConnection.Open()
Dim objOleDbCommand As System.Data.OleDb.OleDbCommand
objOleDbCommand = New System.Data.OleDb.OleDbCommand(strSQLCommand, objOleDbConnection)
Dim objOleDbDataReader As System.Data.OleDb.OleDbDataReader
objOleDbDataReader = objOleDbCommand.ExecuteReader()
gdvproducts.DataSource = objOleDbDataReader
gdvproducts.DataBind()
objOleDbConnection.Close()
End Sub
Protected Sub Page_Load(sender As Object, e As System.EventArgs)
fillGV()
End Sub
Kindly mark this post as "Answer", if it helped you.
It think the problem is binding a HyperiLnk field to ImageField.
When I tried binding HyperLink Field to Template field it worked. And when I bind TextFiled giving the url of image to Imagefield it also worked . But when I bind Hyperlink Filed to Image field it didn't work.
I suggest to use an image control in a template field to display image as I did. Or change your HyperLInk Field to Text Field.
Kindly mark this post as "Answer", if it helped you.
basheerkal. I have it almost exactly like you have and changed my data to text in the database but it's still the same. There seems to be a large section of the url missing in the html output. There are like 4 folders missed out after the main site address
and it jumps straight to my images folder.
bouncyger
Member
8 Points
68 Posts
Images in datagrid
Apr 27, 2012 09:09 PM|LINK
Having problems getting images to show in a datagrid using VB.NET
Tried writing the code and using Visual Studio
Pasted the code, hope that it is ok to do that!!
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim strDatabaseNameAndLocation As String
strDatabaseNameAndLocation = Server.MapPath("shop.mdb")
Dim strSQLCommand As String
strSQLCommand = "SELECT Product.* FROM Product ORDER BY Product.productID ASC;"
Dim objOleDbConnection As System.Data.OleDb.OleDbConnection
objOleDbConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & strDatabaseNameAndLocation)
objOleDbConnection .Open()
Dim objOleDbCommand As System.Data.OleDb.OleDbCommand
objOleDbCommand = New System.Data.OleDb.OleDbCommand(strSQLCommand, objOleDbConnection)
Dim objOleDbDataReader As System.Data.OleDb.OleDbDataReader
objOleDbDataReader = objOleDbCommand.ExecuteReader()
gdvproducts.DataSource = objOleDbDataReader
Dim datDataTable As System.Data.DataTable
datDataTable = New System.Data.DataTable()
datDataTable.Load(objOleDbDataReader)
gdvproducts.DataSource = datDataTable
gdvproducts.DataBind()
objOleDbConnection .Close()
End Sub
Protected Sub gdvproducts_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Web Site Development</title>
</head>
<body>
<form id="webform" runat="server">
<div>
<asp:GridView ID="gdvproducts" runat="server" AutoGenerateColumns="False"
DataKeyNames="productID" OnSelectedIndexChanged="gdvproducts_SelectedIndexChanged">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="productID" HeaderText="Product ID"
InsertVisible="False" ReadOnly="True" SortExpression="productID" />
<asp:BoundField DataField="productID" HeaderText="Name"
SortExpression="productName" />
<asp:BoundField DataField="productDescription" HeaderText="Description"
SortExpression="productDescription" />
<asp:BoundField DataField="productPrice" HeaderText="Price"
SortExpression="productPrice" />
<asp:ImageField DataImageUrlField="image" headertext="Picture" dataimageurlformatstring="~\images\{0}"/>
</Columns>
<SelectedRowStyle BackColor="#CCCCCC" Font-Bold="True" />
</asp:GridView>
</div>
</form>
</body>
</html>
basheerkal
Star
10672 Points
2426 Posts
Re: Images in datagrid
Apr 28, 2012 02:14 AM|LINK
This is enough. no need of a Table
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Dim strDatabaseNameAndLocation As String strDatabaseNameAndLocation = Server.MapPath("shop.mdb") Dim strSQLCommand As String strSQLCommand = "SELECT Product.* FROM Product ORDER BY Product.productID ASC;" Dim objOleDbConnection As System.Data.OleDb.OleDbConnection objOleDbConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & strDatabaseNameAndLocation) objOleDbConnection.Open() Dim objOleDbCommand As System.Data.OleDb.OleDbCommand objOleDbCommand = New System.Data.OleDb.OleDbCommand(strSQLCommand, objOleDbConnection) Dim objOleDbDataReader As System.Data.OleDb.OleDbDataReader objOleDbDataReader = objOleDbCommand.ExecuteReader() gdvproducts.DataSource = objOleDbDataReader dvproducts.DataBind() objOleDbConnection.Close() End SubPlease note. You can avoid repeating System.Data.OleDb in every line if you add the namespace "System.Data.OleDb" to the page.
(Talk less..Work more)
bouncyger
Member
8 Points
68 Posts
Re: Images in datagrid
Apr 28, 2012 02:37 PM|LINK
Thanks for your reply, in the source of the webppage the link to the pics has #nameofpic%23 at the end "
And it's still not showing the pictures....this has worked for me before in the same database and page!!
pierrefrc
Participant
947 Points
201 Posts
Re: Images in datagrid
Apr 28, 2012 09:19 PM|LINK
Hello
In your select "SELECT Product.* FROM Product ORDER BY Product.productID ASC;" what the format that the picture return, is the filename from image or another data ?
bouncyger
Member
8 Points
68 Posts
Re: Images in datagrid
Apr 28, 2012 10:17 PM|LINK
Thanks for replying mate!!
in the database it is a hyperlink field, access 2003 with format /filename.jpg
pierrefrc
Participant
947 Points
201 Posts
Re: Images in datagrid
Apr 28, 2012 10:22 PM|LINK
The filename exists in path ~/images/filename?
Do you have any sample of the html with the path of image?
basheerkal
Star
10672 Points
2426 Posts
Re: Images in datagrid
Apr 29, 2012 05:36 AM|LINK
Check this sample code. compare with yours and correct if necessary.My images are stored in img folder of my site My table tblpics contains 2 fields: username and ImageName. In imagename field image paths are stored as img/id001.jpg. img/id002.jpg and so on..
See my GridView:
<asp:GridView ID="gdvproducts" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundField DataField="username" HeaderText="UserName" SortExpression="username" /> <asp:TemplateField HeaderText="Image"> <ItemTemplate> <asp:Image ID="Image1" runat="server" ImageUrl='<%# Bind("ImageName") %>'/> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>My Vb.net Code:
Sub fillGV() Dim strDatabaseNameAndLocation As String strDatabaseNameAndLocation = Server.MapPath("~/App_Data/basic.mdb") Dim strSQLCommand As String strSQLCommand = "SELECT * FROM tblpics" Dim objOleDbConnection As System.Data.OleDb.OleDbConnection objOleDbConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & strDatabaseNameAndLocation) objOleDbConnection.Open() Dim objOleDbCommand As System.Data.OleDb.OleDbCommand objOleDbCommand = New System.Data.OleDb.OleDbCommand(strSQLCommand, objOleDbConnection) Dim objOleDbDataReader As System.Data.OleDb.OleDbDataReader objOleDbDataReader = objOleDbCommand.ExecuteReader() gdvproducts.DataSource = objOleDbDataReader gdvproducts.DataBind() objOleDbConnection.Close() End Sub Protected Sub Page_Load(sender As Object, e As System.EventArgs) fillGV() End Sub(Talk less..Work more)
bouncyger
Member
8 Points
68 Posts
Re: Images in datagrid
Apr 29, 2012 10:34 AM|LINK
Thanks guys but still not winning
putting n the code you gave me as is missed out my images folder so i added /images/picturename in the table cell
but i am getting
/images/a4padsmall.jpg#/images/a4padsmall.jpg# in the source code?
Any ideas?
basheerkal
Star
10672 Points
2426 Posts
Re: Images in datagrid
Apr 29, 2012 11:31 AM|LINK
It think the problem is binding a HyperiLnk field to ImageField.
When I tried binding HyperLink Field to Template field it worked. And when I bind TextFiled giving the url of image to Imagefield it also worked . But when I bind Hyperlink Filed to Image field it didn't work.
I suggest to use an image control in a template field to display image as I did. Or change your HyperLInk Field to Text Field.
(Talk less..Work more)
bouncyger
Member
8 Points
68 Posts
Re: Images in datagrid
Apr 29, 2012 12:28 PM|LINK
basheerkal. I have it almost exactly like you have and changed my data to text in the database but it's still the same. There seems to be a large section of the url missing in the html output. There are like 4 folders missed out after the main site address and it jumps straight to my images folder.