Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

if LINQ statement has no results?

Posted on 2011-03-03
5
Medium Priority
?
1,313 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
  • 3
  • 2
5 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

916 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