• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 433
  • Last Modified:

Error at conversion from datarow to string using a dataset


I get this error when using a dataset to bind my data to a datalist control programmatically:
Conversion from type 'DataRowView' to type 'String' is not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Conversion from type 'DataRowView' to type 'String' is not valid.

Source Error:

Line 42:        
Line 43:         ' Get the row data
Line 44:         Dim imageId As String = e.Item.DataItem
Line 45:
Line 46:

My code is like this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim cImage As New imagesDL
        dlImages.DataSource = cImage.GetImagesAllSystem
    End Sub

Protected Sub dlImages_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlImages.ItemDataBound
        ' Get the row data
        Dim imageId As String = e.Item.DataItem

        ' Find the row controls
        Dim myILoad As Radactive.WebControls.ILoad.ILoad = e.Item.FindControl("ILoad1")
        Dim myLabel As Label = e.Item.FindControl("Label1")
        Dim myImageButton As ImageButton = e.Item.FindControl("ImageButton1")

        If Radactive.WebControls.ILoad.WebImage.FileExists(Server.MapPath("Uploads/images"), imageId) Then
            ' Load data inside the controls
            myILoad.Value = Radactive.WebControls.ILoad.WebImage.Load(Server.MapPath("Uploads/images"), imageId)
            myLabel.Text = "Original file name:" + myILoad.Value.SourceImageClientFileName
            myImageButton.ImageUrl = myILoad.Value.IconImage.FileUrl
            myImageButton.OnClientClick = "showILoadPreview('" + myILoad.ClientID + "'); return false;"
        End If
    End Sub

Grafeful fro help with what's missing!


Peter Nordberg
Peter Nordberg
1 Solution
You have to tell it which Item and cast it to a string
Dim imageId As String = CType(e.Item.DataItem,System.Data.DataRowView)("THE BOUND ITEM NAME").ToString()
Peter NordbergIT ManagerAuthor Commented:
Thanks! That worked!

Easy when you know!

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.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now