{"Unable to cast object of type 'System.Int32' to type 'System.Web.UI.WebControls.ListItem'."}

Posted on 2012-09-21
Last Modified: 2012-09-21
I am getting the following error:
{"Unable to cast object of type 'System.Int32' to type 'System.Web.UI.WebControls.ListItem'."}

Code below

foreach (ListItem  item in data)

Question by:Meinhoonaa
    LVL 22

    Expert Comment

    need more code than that...

    what is data? what type is it?

    Author Comment

    var data = Users.GetUserAccountsAll(UserID);
                foreach (ListItem  item in data)
                    lstDealerships.SelectedValue = item.Value;

     public static IQueryable GetUserAccountsAll(int userid)
                ELTDataClasses db = new ELTDataClasses();
                var Data = (from dtuseraccountxref in (db.user_account_xrefs)
                            where dtuseraccountxref.user_id == userid
                return Data.Distinct();

    LVL 5

    Accepted Solution

    'data' collection is not compatible with ListItem type

    here is something that you can do

    var data = Users.GetUserAccountsAll(UserID);
                foreach (var item in data)
                   if(item != null)
                    lstDealerships.SelectedValue = item.ToString();

    Let me know if it works for you

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now