Solved

if LINQ statement has no results?

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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