Solved

Error displaying image in gridview with path from access

Posted on 2006-06-19
2
394 Views
Last Modified: 2012-05-05
Hello, I have an access database IdNumber, name, position and url. I have images in /staffImages. The image name corresponds with the idNumber. So the first row 1 has an image with the name of 1.jpg.
 When I try to open the page and bind the gridview I get the following error. Any Ideas? My code is below:

Error:
Compiler Error Message: BC30109: 'String' is a class type and cannot be used as an expression.

Source Error:

 

Line 20: <ItemTemplate>
Line 21:
Line 22: <asp:Image ID="Image1" ImageUrl='<%# (string) FormatImageUrl( (string) Eval("Url")) %>' runat="server" />
Line 23:
Line 24: </ItemTemplate>
 


My codebehind:
Imports System.Data
Imports System.Data.OleDb

Partial Class staff
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        BindData()
    End Sub

    Private Sub BindData()

        Dim myConnection As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & Server.MapPath("App_Data/ASPNetDB.mdb"))

        Dim ad As OleDbDataAdapter = New OleDbDataAdapter("SELECT idNumber, Name, position,Url FROM Staff", myConnection)

        Dim ds As DataSet = New DataSet()

        ad.Fill(ds)

        GridView1.DataSource = ds

        GridView1.DataBind()

    End Sub
    Protected Function FormatImageUrl(ByVal url As String) As String

        If Not url Is Nothing AndAlso url.Length > 0 Then

            Return ("~/" & url)

        Else
            Return Nothing
        End If

    End Function

End Class


Page code:
          <asp:GridView ID="GridView1" runat="server">
           <Columns>

<asp:BoundField DataField="idNumber" HeaderText="idNumber" />

<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Position" HeaderText="Position" />

<asp:TemplateField HeaderText="Image">

<ItemTemplate>

<asp:Image ID="Image1" ImageUrl='<%# (string) FormatImageUrl( (string) Eval("Url")) %>' runat="server" />

</ItemTemplate>

</asp:TemplateField>

</Columns>
    </asp:GridView>
0
Comment
Question by:gogetsome
2 Comments
 
LVL 14

Accepted Solution

by:
jjardine earned 500 total points
ID: 16938426
It might be that you are trying to cast the result from FormatImageUrl  to a string when if there is no url it returns Nothing  try returning empty string and see what that does.
0
 

Author Comment

by:gogetsome
ID: 16942460
Thanks, that made me look closer. I changed this: <%# (string) FormatImageUrl( (string) Eval("Url")) %> to this and solved the problem: <%# (FormatImageUrl( Eval("Url"))) %>
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question