Specified cast is not valid

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
LVL 7
ayha1999Asked:
Who is Participating?
 
Jason ScolaroCommented:
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
 
Jason ScolaroCommented:
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
 
RejojohnyCommented:
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
garima_sikarwarCommented:
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
 
ayha1999Author Commented:
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
 
ayha1999Author Commented:
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
 
Jason ScolaroCommented:
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
 
RejojohnyCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.