Solved

if LINQ statement has no results?

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

830 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