Solved

if LINQ statement has no results?

Posted on 2011-03-03
5
1,303 Views
Last Modified: 2012-08-14
hi all, below is my linq statment

the ID of is 10000 which the table its being asked to get data from doesnt have an ID for this record yet

so at the moment i get the below error

but i want to obviously still load the page if this table doesnt yet have an ID for the record (it will be added when they fill something in)

can someone show me hwo to do it?
Thanks
##################### ERROR #########################
Server Error in '/' Application.

Sequence contains no elements

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: Sequence contains no elements

Source Error: 


Line 33:         using (CompanyDataContext dc = new CompanyDataContext())
Line 34:         {
Line 35:             var CompQuery = (from C in dc.CompanyInsurances
Line 36:                              where C.ID == Convert.ToInt16(StrID)
Line 37:                              select C).Single();

Source File: e:\netfolder\companies\insurance.aspx.cs    Line: 35 

Stack Trace: 


[InvalidOperationException: Sequence contains no elements]
   System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) +4539
   System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) +207
   System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) +500
   System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute(Expression expression) +50
   System.Linq.Queryable.Single(IQueryable`1 source) +383
   Insurance.Page_Load(Object sender, EventArgs e) in e:\netfolder\companies\insurance.aspx.cs:35
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +25
   System.Web.UI.Control.LoadRecursive() +71
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3048

Open in new window

using (CompanyDataContext dc = new CompanyDataContext())
        {
            var CompQuery = (from C in dc.CompanyInsurances
                             where C.ID == Convert.ToInt16(StrID)
                             select C).Single();

Open in new window

0
Comment
Question by:awilderbeast
[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
  • 3
  • 2
5 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 35028608
Try SingleOrDefault() rather than Single().
0
 
LVL 1

Author Comment

by:awilderbeast
ID: 35028655
that did the trick but then my next lines...

can i wrap all them in some if statement that will do nothing if there is no entry?

Thanks
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 


Line 37:                              select C).SingleOrDefault();
Line 38: 
Line 39:             PLLimit.Text = CompQuery.PLLimit;

Open in new window

0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 500 total points
ID: 35028842

if (CompQuery != null)
{
   // do work
}

Open in new window

0
 
LVL 1

Author Closing Comment

by:awilderbeast
ID: 35028863
thanks alot!
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35028874
NP. Glad to help  : )
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

623 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