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,695 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 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