Solved

Error displaying image in gridview with path from access

Posted on 2006-06-19
2
388 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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
HTML table 7 54
varchar (max) 5 28
Paging GridView 7 32
How can I use this extension method? 8 35
AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now