Solved

Error displaying image in gridview with path from access

Posted on 2006-06-19
2
391 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

919 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

12 Experts available now in Live!

Get 1:1 Help Now