How to convert from type 'ListItem' to type 'String'? RRS feed

  • Question

  • User-830563764 posted

    In a VS 2008 ASP.net 3.5 aspx.vb file, Response.Write(DDL.SelectedItem) worked very well.
    But "Dim filenameDDL As String = DDL.SelectedItem.Text" did not.  Then I tried the following codes
    and found the problem -- Conversion from type 'ListItem' to type 'String' is not valid.

    Why Response.Write(DDL.SelectedItem) worked so well? 
    How can convert ListItem to String, so that "Dim filenameDDL As String = DDL.SelectedItem.Text" will work?

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

    Source Error:

    Line 70:                 Session.Add("FileSelected", DDL.SelectedItem)
    Line 71: 'Dim ItemSelected = DDL.SelectedItem
    Line 72: Dim filenameDDL As String = Session("FileSelected")
    Line 73: 'Dim filenameDDL As String = DDL.SelectedItem.Text
    Line 74: 'Dim fileSelected As String = DDL.Items.IndexOf(item:=DDL.SelectedItem)

    Source File: C:\Documents and Settings\Jeffrey\My Documents\Visual Studio 2008\WebSites\MasterPagesTutorial01\ContentsDDLPlaceHolder.aspx.vb    Line: 72

    Thursday, August 27, 2009 11:27 PM


  • User2019981500 posted



    Dim filenameDDL As String =Convert.ToString( Session("FileSelected"))


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 27, 2009 11:56 PM