Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to populate the first column in the listView, ListView2 when in insert mode. I've tried the code below and get the following error.

VB
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
      Dim item As ListViewDataItem = ListView2.Items()


      ListView2.Items.Add(item)
      Items(0) = Request.QueryString("pstr")


  End Sub




Server Error in '/' Application.

Unable to cast object of type 'System.Collections.Generic.List`1[System.Web.UI.WebControls.ListViewDataItem]' to type 'System.Web.UI.WebControls.ListViewDataItem'.
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: Unable to cast object of type 'System.Collections.Generic.List`1[System.Web.UI.WebControls.ListViewDataItem]' to type 'System.Web.UI.WebControls.ListViewDataItem'.

Source Error:



Line 33:
Line 34: Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Line 35: Dim item As ListViewDataItem = ListView2.Items()
Line 36:
Line 37:


Source File: C:\AJAX AutoCompleteExtenderlotaddresswrks2\vidinsert.aspx.vb Line: 35

Stack Trace:



[InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List`1[System.Web.UI.WebControls.ListViewDataItem]' to type 'System.Web.UI.WebControls.ListViewDataItem'.]
_Default2.Page_Load(Object sender, EventArgs e) in C:\AJAX AutoCompleteExtenderlotaddresswrks2\vidinsert.aspx.vb:35
System.Web.UI.Control.OnLoad(EventArgs e) +92
System.Web.UI.Control.LoadRecursive() +54
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772




Version
Posted

1 solution

The error is perfectly clear - you have declared a variable of type ListViewDataItem, and you're trying to store a List(Of ListViewDataItem) in it.

You're then trying to add the collection of items to itself, which makes absolutely no sense.

You're then assigning a query-string parameter to the first item in an unknown collection called Items, which you haven't shown.

Based on your rather brief description of what you're trying to achieve, you want something like this:
VB.NET
Protected Sub ListView2_ItemCreated(ByVal sender As Object, ByVal e As ListViewItemEventArgs) Handles ListView2.ItemCreated
    If e.Item.ItemType = ListViewItemType.InsertItem Then
        Dim txt As TextBox = TryCast(e.Item.FindControl("SomeTextBox", TextBox))
        If txt IsNot Nothing Then
            txt.Text = Request.QueryString("pstr")
        End If
    End If
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900