none
Linq to Sql Invalid Cast Exception RRS feed

  • Question

  •   In The following code I am getting a Invalid Cast Exception Specified cast is not valid 
    
    I've checked the datatype in the dbml & Sql table. Does anyone have an idea? Thanks
    
     public List<details> GetDetailList(int SalesQuoteID)
        {
            List<details> List = new List<details>();
    
            var q = from p in db.QuoteHistories
                    where p.SalesQuoteID == SalesQuoteID
                    select new details { Notes = Convert.ToString(p.Notes), SalesQuoteID = Convert.ToInt32(p.SalesQuoteID),
                        status=Convert.ToInt32(p.status), QuoteHistoryID = Convert.ToInt32(p.QuoteHistoryID), 
                        created_dt =Convert.ToDateTime(p.created_dt), created_by = Convert.ToInt32(p.created_by) };
    
            if (q.Count() > 0)
                List =  q.ToList() ;
            return List;
    
        }
    

    Friday, November 4, 2011 5:48 PM

Answers

  • Hi WTFHolmes,

    I don't know your data, but just looking at it based on the names this conversion might be a problem if created_by is a string (like a user's name) instead of a integer:

    created_by = Convert.ToInt32(p.created_by)
    

     

     


    Tom Overton
    Friday, November 4, 2011 5:54 PM

All replies

  • Hi WTFHolmes,

    I don't know your data, but just looking at it based on the names this conversion might be a problem if created_by is a string (like a user's name) instead of a integer:

    created_by = Convert.ToInt32(p.created_by)
    

     

     


    Tom Overton
    Friday, November 4, 2011 5:54 PM
  • Sharp Eye, Thanks
    Friday, November 4, 2011 6:07 PM