Solved

if LINQ statement has no results?

Posted on 2011-03-03
5
1,270 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 74

Accepted Solution

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

Author Comment

by:awilderbeast
Comment Utility
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 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 500 total points
Comment Utility

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

Open in new window

0
 
LVL 1

Author Closing Comment

by:awilderbeast
Comment Utility
thanks alot!
0
 
LVL 74

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
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…

728 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

12 Experts available now in Live!

Get 1:1 Help Now