locked
ListBox code and Error message RRS feed

  • Question

  • User-1999980185 posted

    I converted VB code to C# using Telerik Code Converter and I am getting two errors that I do not understand.  Please see code and error message below.

    Code Below:
     foreach (var Li in ListBox_Mon.Items)
                {
                    if (Li.selected == true)                     // Error on Line 110
                    {
                        if (Opt == "1")
                        {
                            D_Month = Li.text.ToString();        // Error on Line 114
                            Convert_Month();
                            ddesc = Session["ddesc_Data"].ToString();
                            Textbox_srchcol.Text = "Description Column";
                            Textbox_srchdata.Text = ddesc;
                            Do_ListView_History_Desc();
                        }
                    }
                }
    
    
    Error Messages Below:
    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS1061	'object' does not contain a definition for 'selected' and no extension method 'selected' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)	FirstCWeb	C:\Users\Edward\documents\visual studio 2015\Projects\MyFirstCWeb_Solution\FirstCWeb\listBoxHistoryDateNew.aspx.cs	110	Active
    
    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS1061	'object' does not contain a definition for 'text' and no extension method 'text' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)	FirstCWeb	C:\Users\Edward\documents\visual studio 2015\Projects\MyFirstCWeb_Solution\FirstCWeb\listBoxHistoryDateNew.aspx.cs	114	Active
    


    Please help.

    Monday, September 24, 2018 6:51 PM

Answers

  • User-1999980185 posted

    I had to change VAR to   foreach (ListItem Li in ListBox_Mon.Items)  then the code work.   Thanks.  I will close the ticket.  Thanks again.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 24, 2018 8:14 PM

All replies

  • User475983607 posted

    Try reading changing the capitalization.

    if (Li.Selected == true) 
    D_Month = Li.Text.ToString();

    Also try simply using intellisense to auto complete the item.  Lastly, reading the openly published API documentation for the item, in this case a listbox, that you trying to access is very helpful.  Take a few minutes to understand what the code is doing rather than just copy and paste.

    Monday, September 24, 2018 7:01 PM
  • User-1999980185 posted

    Made changes but same error messages.  I am not understanding something about listbox.

    foreach (var Li in ListBox_Mon.Items)
                {
                    if (Li.Selected == true)                     //  Line 110
                    {
                        if (Opt == "1")
                        {
                            D_Month = Li.Text.ToString();        // Line 114
                            Convert_Month();
                            ddesc = Session["ddesc_Data"].ToString();
                            Textbox_srchcol.Text = "Description Column";
                            Textbox_srchdata.Text = ddesc;
                            Do_ListView_History_Desc();
                        }
                    }
                }
    
    
    Error Messages Below
    
    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS1061	'object' does not contain a definition for 'Selected' and no extension method 'Selected' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)	FirstCWeb	C:\Users\Edward\documents\visual studio 2015\Projects\MyFirstCWeb_Solution\FirstCWeb\listBoxHistoryDateNew.aspx.cs	110	Active
    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS1061	'object' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)	FirstCWeb	C:\Users\Edward\documents\visual studio 2015\Projects\MyFirstCWeb_Solution\FirstCWeb\listBoxHistoryDateNew.aspx.cs	114	Active
    

    Monday, September 24, 2018 7:17 PM
  • User475983607 posted

    Made changes but same error messages.  I am not understanding something about listbox.

    I assume ListBox_Mon is an ASP.NET ListControl and you are trying to loop over the ListItems.

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.listcontrol?view=netframework-4.7.2

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.listcontrol.items?view=netframework-4.7.2#System_Web_UI_WebControls_ListControl_Items

    Other than that I'm not sure what you're converting...

    Monday, September 24, 2018 7:40 PM
  • User-1999980185 posted

    I had to change VAR to   foreach (ListItem Li in ListBox_Mon.Items)  then the code work.   Thanks.  I will close the ticket.  Thanks again.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 24, 2018 8:14 PM