Solved

Error message " Unable to cast object of type 'System.Data.Linq.DataQuery`1[System.Int32]' to type 'System.IConvertible'.

Posted on 2008-10-30
1
2,667 Views
Last Modified: 2013-12-17
I'm attempting to return an integer based on a matching customer name
        {
         // get invoice number
            InvoiceHeader tOInvHeader = new InvoiceHeader();
            oAdmins.InvoiceNumber = oAdmins.InvoiceNumber + 1;
            txtInvoiceNumber.Text = Convert.ToString(oAdmins.InvoiceNumber);

            var cnum = (from tcusto in dc.Customers
                        where tcusto.CustomerName == cmbInvoiceCustomer.Text
                        select tcusto.CustomerID);

     ********       tOInvHeader.CustomerID = Convert.ToInt32(cnum);        ********** GET's ERROR
            tOInvHeader.OrderDate = Convert.ToDateTime(txtInviceDate);
            tOInvHeader.InvoiceNumber = Convert.ToInt32(oAdmins.InvoiceNumber);
            dc.InvoiceHeaders.InsertOnSubmit(tOInvHeader);
            dc.SubmitChanges();
            var cInvHeader = from tInvHeader in dc.InvoiceHeaders
                             where tInvHeader.CustomerID == tOInvHeader.CustomerID
                             select tInvHeader;
            // The following line
            oInvHeader = (InvoiceHeader)cInvHeader.SingleOrDefault<InvoiceHeader>();
            cmbInvoiceCustomer.DataContext = oInvHeader;
        }
0
Comment
Question by:jgw1212
1 Comment
 
LVL 19

Accepted Solution

by:
drichards earned 500 total points
ID: 22847676
It's telling you that the result of a LINQ qery does not implement the IConvertible interface and therefore you cannot use System.Convert to perform a type conversion on it.

You should be able to do:

    tOInvHeader.CustomerID = Convert.ToInt32(cnum.ElementAt(0));

Since the result does implement the IEnumerable interface.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

24 Experts available now in Live!

Get 1:1 Help Now