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,650 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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 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

11 Experts available now in Live!

Get 1:1 Help Now