Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old September 14th, 2006, 10:33 AM
Registered User
Join Date: Sep 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Arraylist is datasource for listbox. Failing.

I have an custom class that I use for populating an ArrayList that I then use as the DataSource for a listbox in ASP.Net.
Problem is it was written in VB but needs to be C#. I have completed the conversion except for one statement that does not work as it did in VB.

Problem statement is when trying to pull the SelectedItem.

VB: (Works Great!)
strCode = CType(cboLocation.SelectedItem,clsListElements).Lo cType

C#: (Fails with an error)
strCode = ((clsListElements)System.Convert.ChangeType(cboLoc ation.SelectedItem, typeof(clsListElements))).LocType;
ERROR: 'System.InvalidCastException' {"Object must implement IConvertible."}

My custom class C# (Partial Code):

public class clsListElements
    private string strDescription;
    private string strID;
    private string strLocType;

    public clsListElements(string strPassedDescription, string strPassedID, string strPassedLocType)
        this.strDescription = strPassedDescription;
        this.strID = strPassedID;
        this.strLocType = strPassedLocType.Trim();
    public string LocType
            return this.strLocType;

Code to load arraylist and to set listbox datasource to that arraylist:

   SqlDataReader myReader = myCommand.ExecuteReader();

   while (myReader.Read())
     if (myReader["LocationType"].ToString() == CommonRoutine.C_LOCATIONTYPE_BOTH || myReader["LocationType"].ToString()

        aryList.Add(new clsListElements((string)myReader["LocationName"], myReader["LocationID"].ToString(), myReader{"LocationType"].ToString()));

    cboLocation.DataSource = aryList;
    cboLocation.DataTextField = "Description";
    cboLocation.DataValueField = "IDNum";

Similar Threads
Thread Thread Starter Forum Replies Last Post
Failing DAO Tests dgalehouse BOOK: Beginning Spring Framework 2 ISBN: 978-0-470-10161-2 13 September 28th, 2009 07:13 PM
Listbox,Postback and Datasource nichola_x_rose ASP.NET 1.0 and 1.1 Basics 5 March 22nd, 2007 04:13 AM
Object datasource VS DataSource SoftMind BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 4 July 27th, 2006 10:44 PM
ListBox DataSource jugomkd C# 2 October 9th, 2004 11:30 PM
Q. My sessions are failing... what's wrong?? richard.york PHP FAQs 0 August 4th, 2004 07:24 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.