?
Solved

Specified cast is not valid

Posted on 2006-05-29
8
Medium Priority
?
447 Views
Last Modified: 2008-02-01
Hi,

I am trying to display image in a dataghrid. But in the item databound event I get an error that "specified cast is not valid" at line "Dim img As Image ..."

Item databound event:

Sub Item_Bound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim img As Image = CType(e.Item.FindControl("Img1"), Image)
            Dim anchor As HtmlAnchor = CType(e.Item.FindControl("PictureAnchor"), HtmlAnchor)
            If CType(e.Item.DataItem, DataRowView)("DFile") Is Nothing Then
                img.ImageUrl = anchor.HRef = CType(e.Item.DataItem, DataRowView)("DFile")
            Else
                img.ImageUrl = anchor.HRef = "NoImage.bmp"
            End If
        End If
    End Sub

HTML part:
<asp:TemplateColumn HeaderText="Picture">
<ItemTemplate>
<a href='<%# DataBinder.Eval(Container, "DataItem.DFile") %>' target="_blank" runat="server" id="PictureAnchor">
<img runat="server" width="20" src='<%# DataBinder.Eval(Container, "DataItem.DFile") %>' ID="Img1"/>
</a>
</ItemTemplate>
</asp:TemplateColumn>

Picture is stored in the table as ./Folder/image.jpeg

How can I solve the problem?

ayha
0
Comment
Question by:ayha1999
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 22

Expert Comment

by:Jason Scolaro
ID: 16786822
Try replacing the error'd line (Dim img ...) with this one:

Dim img As System.Web.UI.WebControls.Image = CType(e.Item.FindControl("Img1"), System.Web.UI.WebControls.Image)

-- Jason
0
 
LVL 26

Assisted Solution

by:Rejojohny
Rejojohny earned 500 total points
ID: 16787459
can u also post the code for the grid .. the aspx code .. would like to know what controls u have within the template column .. whether its a HTML or a web control ..
0
 
LVL 4

Expert Comment

by:garima_sikarwar
ID: 16788098
U can do it like this also : Create a function and call it from img tag

i think (DataRowView)("DFile") contains the path of the image..

<script runat=server>
public sub GetImageName(strDfile as string) as string

if not (strDfile is nothing) then
return strDfile
else
return "Noimage.bmp"
end sub
</script>

<asp:TemplateColumn HeaderText="Picture">
<ItemTemplate>
<a href='<%# DataBinder.Eval(Container, "DataItem.DFile") %>' target="_blank" runat="server" id="PictureAnchor">
<img runat="server" width="20" src='<%# GetImageName(DataBinder.Eval(Container, "DataItem.DFile")) %>' ID="Img1"/>
</a>
</ItemTemplate>
</asp:TemplateColumn>

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Author Comment

by:ayha1999
ID: 16790891
Hi scoja,

Same error with "Dim img As System.Web.UI.WebControls.Image = CType(e.Item.FindControl("Img1"), System.Web.UI.WebControls.Image)"

the vb code
 Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not Page.IsPostBack Then
                      BindData()
        End If
    End Sub

Sub BindReportData()
        Dim Con As New OleDbConnection(ConfigurationSettings.AppSettings("ConnectionString"))

        Dim cmd As New OleDbCommand
        Dim rdr As OleDbDataReader
     
        Dim strSQL As String = "select * from theTable"

        If Con.State = ConnectionState.Closed Then
            Con.Open()
        End If

        cmd = New OleDbCommand(strSQL, Con)
        Dim adapter As OleDbDataAdapter = New OleDbDataAdapter
        adapter.SelectCommand = cmd

        Dim RDataSet As DataSet = New DataSet
        adapter.Fill(RDataSet)
 
            grdReportResult.DataSource = RDataSet
            Try
                grdReportResult.DataBind()
            Catch e As HttpException
                If e.Message = "Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount." And grdReportResult.CurrentPageIndex > 0 Then
                    grdReportResult.CurrentPageIndex = grdReportResult.CurrentPageIndex - 1
                    BindReportData()
                Else
                    Throw e
                End If
            End Try
        End If

         End Sub


I will try garima's solutiona and come back.

ayha
0
 
LVL 22

Accepted Solution

by:
Jason Scolaro earned 500 total points
ID: 16790975
ayha,

Just as Rejo mentioned, it would help to see the HTML code that you've used to build the Grid.  If you're still receiving an error on that line after the code I gave you, then you're probably not using a .NET Image server control (<asp:Image>).  If you're using an HTML image element (<img>) then you need to use this class:

System.Web.UI.HtmlControls.HtmlImage

Good luck,
-- Jason
0
 
LVL 7

Author Comment

by:ayha1999
ID: 16792404
Hi,

Here is the html code which I have already posted ;

HTML part:
<asp:TemplateColumn HeaderText="Picture">
<ItemTemplate>
<a href='<%# DataBinder.Eval(Container, "DataItem.DFile") %>' target="_blank" runat="server" id="PictureAnchor">
<img runat="server" width="20" src='<%# DataBinder.Eval(Container, "DataItem.DFile") %>' ID="Img1"/>
</a>
</ItemTemplate>
</asp:TemplateColumn>

ayha
0
 
LVL 22

Expert Comment

by:Jason Scolaro
ID: 16792452
Ah -- sorry I missed that.  Then my last comment is your solution.  You're using the wrong Class, your <img runat="server ...> is not an Image, but rather an HtmlControls.HtmlImage.  Change your code to match that:

Dim img As System.Web.UI.HtmlControls.HtmlImage = CType(e.Item.FindControl("Img1"), System.Web.UI.HtmlControls.HtmlImage)

You'll probably have to change your Properties a bit, I don't think an HtmlImage has a property of ImageUrl, I believe it's Src.

-- Jason
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 16795810
it is "src" .. just as I suspected .. not a server control .. is there a reason for not using a server control and using HTML controls with runat=server? for ur scenario it would be best to use server controls .. "<asp:Image" and not "<img" .. then ur current code should work as it is ..

Rejo
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses

569 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