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,683 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

840 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