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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Query Help Top 1 and Distinct? 6 49
PowerShell: ForEach ($y in $x.licenses | where {....} 3 55
Data organization issue 7 37
SSIS GUID Variable 2 37
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

726 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