Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 14th, 2006, 10:33 AM
Registered User
Join Date: Sep 2006
Location: , , .
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";


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 03:04 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.