I have a web application that was working fine but does not work anymore. It even works on my development computer (hooked up to a local database), but not on production. So on my development computer, I change the connection string to hit the production database and I get the Error: Sequence contains more than one element.
I believe it has to have something to do with the database, but I don't know how to figure out what it is, there are almost a thousand rows of data in it.
Below is the linq statement that gets the data. Any ideas on how I can fix this?
Thanks in advance
protected void lnqItems_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
DocksideValidationDataContext db = new DocksideValidationDataContext();
DateTime startDate = (radStartDate.SelectedDate.HasValue)
? radStartDate.SelectedDate.Value
: DateTime.Parse("1/1/1900");
DateTime endDate = (radEndDate.SelectedDate.HasValue)
? radEndDate.SelectedDate.Value
: DateTime.Parse("1/1/2500");
int prevalNum = (txtSummaryFormNumber.Text.Trim() == "")
? -1
: Convert.ToInt32(txtSummaryFormNumber.Text.Trim());
e.Result = (from x in db.SummaryForms
where (x.SummaryFormId == prevalNum
|| (prevalNum == -1)
&& (x.SampleDate >= startDate.Date)
&& (x.SampleDate <= endDate.Date)
&& (x.StateId == Convert.ToInt32(ddlState.SelectedValue)))
orderby (x.SampleDate) descending
select x).AsEnumerable()
.Select(x => new
{
x.SummaryFormId,
x.SampleDate,
DockName = x.Dock.Code + " " + x.Dock.Name,
Status = GetStatus(x),
Color = GetStatusColor(GetStatus(x))
})
.Where(x => ((prevalNum != -1
|| cboFilter.Text == "All")
|| (x.Status == cboFilter.Text)));
}
No it occurred to me, but I didn't think it would be usefully. Here is it:
System.InvalidOperationException: Sequence contains more than one element
Generated: Sat, 24 Sep 2011 15:57:41 GMT
System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: Sequence contains more than one element
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
at System.Data.Linq.EntityRef`1.get_Entity()
at Secure_SummaryForms.<GetStatus>b__4(SummaryFormVessel z)
at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source)
at Secure_SummaryForms.GetStatus(SummaryForm x)
at Secure_SummaryForms.<lnqItems_Selecting>b__0(SummaryForm x)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Web.UI.WebControls.QueryableDataSourceHelper.ToList(IQueryable query, Type dataObjectType)
at System.Web.UI.WebControls.LinqDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments)
at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)
at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
at System.Web.UI.WebControls.GridView.DataBind()
at System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound()
at System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls()
at System.Web.UI.Control.EnsureChildControls()
at System.Web.UI.WebControls.GridView.get_Rows()
at Secure_SummaryForms.Page_Load(Object sender, EventArgs e)
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at ASP.secure_summaryforms_aspx.ProcessRequest(HttpContext context)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Server Variables
Name Value
ALL_HTTP HTTP_CONNECTION:keep-alive HTTP_ACCEPT:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 HTTP_ACCEPT_CHARSET:ISO-8859-1,utf-8;q=0.7,*;q=0.7 HTTP_ACCEPT_ENCODING:gzip, deflate HTTP_ACCEPT_LANGUAGE:en-us,en;q=0.5 HTTP_COOKIE:ASP.NET_SessionId=yka231rdr1r3yqv0nmbhz1nh; .POWERBASECOOKIE=5FC2CF98E578B444ACA6B75A382EAEF31EFC781DA11AF7884B360A82E544539BEC5E5BB62DD012A1D6E7005869FFF8A205D632FB724AB01EFDBED78A9A035C76B22250F8FF0BCC3AD9AB56C4EEC44C05159110DF95A6344888CB3300B642E312F3B152475CD4FA631C0000699763AE173978375D598154CE85AEF83C81E1FAF82842DF48F1D9AC97872F09FFCA33B22B; .ASPXROLES=IS-YIxYt7iXk72HXdLNE18SBwgsEd2wo3D4GfRI72jYdSmmLhUxfzBMMpFYXg5CjiowldcwSKolBjaP8jrMUgh5-rQWjQhUTY02VS3IhD5Xcav_B-d-MiaumYD-0JMWFR9-1xcP2eHQXLpnZlXzcZXhwh6G2mEDVoNZGls8MNK8bAVlo2WuTqReMmk1GTyu5duoZzh_fCs1S6AZuhWY9bwdi28kA_lfoyp4SCm2zLgNddjhbsLmgN5oopdPo9-oONS7Z5br51qYu5sS9vq-hm_TbHhHL_W7XbIKo3pJccZ1-VXkLbtIAiS_hSccjFmk1f3ttWhUnmULJVQaNNepOHofbivSEQvhQQxX-f5hpGI4K-9FqcJ-kFAjw93CrnZ2rtu6n8i3tblPEDQeAbd1JkSJjmKGpSH_4PVh1MsXNDZwSSnbBHqTaO9iKiVAvUWvANLbBxmOc3w0SfP4DUbLtl3YZVCbG_JWr5s0f5Is8scr7Nc-AxK2HEzjNVber1EofJK48eLaVJPyvm-858Yhtp5XgqMeJQUwh5l9LYeZNbc8kqxPG7kOl0Zq7q8407I230 HTTP_HOST:docksidevalidation.org HTTP_REFERER:http://docksidevalidation.org/secure/default.aspx HTTP_USER_AGENT:Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0.2) Gecko/20100101 Firefox/6.0.2
ALL_RAW Connection: keep-alive Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Accept-Encoding: gzip, deflate Accept-Language: en-us,en;q=0.5 Cookie: ASP.NET_SessionId=yka231rdr1r3yqv0nmbhz1nh; .POWERBASECOOKIE=5FC2CF98E578B444ACA6B75A382EAEF31EFC781DA11AF7884B360A82E544539BEC5E5BB62DD012A1D6E7005869FFF8A205D632FB724AB01EFDBED78A9A035C76B22250F8FF0BCC3AD9AB56C4EEC44C05159110DF95A6344888CB3300B642E312F3B152475CD4FA631C0000699763AE173978375D598154CE85AEF83C81E1FAF82842DF48F1D9AC97872F09FFCA33B22B; .ASPXROLES=IS-YIxYt7iXk72HXdLNE18SBwgsEd2wo3D4GfRI72jYdSmmLhUxfzBMMpFYXg5CjiowldcwSKolBjaP8jrMUgh5-rQWjQhUTY02VS3IhD5Xcav_B-d-MiaumYD-0JMWFR9-1xcP2eHQXLpnZlXzcZXhwh6G2mEDVoNZGls8MNK8bAVlo2WuTqReMmk1GTyu5duoZzh_fCs1S6AZuhWY9bwdi28kA_lfoyp4SCm2zLgNddjhbsLmgN5oopdPo9-oONS7Z5br51qYu5sS9vq-hm_TbHhHL_W7XbIKo3pJccZ1-VXkLbtIAiS_hSccjFmk1f3ttWhUnmULJVQaNNepOHofbivSEQvhQQxX-f5hpGI4K-9FqcJ-kFAjw93CrnZ2rtu6n8i3tblPEDQeAbd1JkSJjmKGpSH_4PVh1MsXNDZwSSnbBHqTaO9iKiVAvUWvANLbBxmOc3w0SfP4DUbLtl3YZVCbG_JWr5s0f5Is8scr7Nc-AxK2HEzjNVber1EofJK48eLaVJPyvm-858Yhtp5XgqMeJQUwh5l9LYeZNbc8kqxPG7kOl0Zq7q8407I230 Host: docksidevalidation.org Referer: http://docksidevalidation.org/secure/default.aspx User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0.2) Gecko/20100101 Firefox/6.0.2
APPL_MD_PATH /LM/W3SVC/1154673598/Root
APPL_PHYSICAL_PATH C:\Inetpub\Websites\DocksideValidation\Website\
AUTH_TYPE Forms
AUTH_USER admin
AUTH_PASSWORD
LOGON_USER
REMOTE_USER admin
CERT_COOKIE
CERT_FLAGS
CERT_ISSUER
CERT_KEYSIZE
CERT_SECRETKEYSIZE
CERT_SERIALNUMBER
CERT_SERVER_ISSUER
CERT_SERVER_SUBJECT
CERT_SUBJECT
CONTENT_LENGTH 0
CONTENT_TYPE
GATEWAY_INTERFACE CGI/1.1
HTTPS off
HTTPS_KEYSIZE
HTTPS_SECRETKEYSIZE
HTTPS_SERVER_ISSUER
HTTPS_SERVER_SUBJECT
INSTANCE_ID 1154673598
INSTANCE_META_PATH /LM/W3SVC/1154673598
LOCAL_ADDR 192.168.1.131
PATH_INFO /secure/SummaryForms.aspx
PATH_TRANSLATED C:\Inetpub\Websites\DocksideValidation\Website\secure\SummaryForms.aspx
QUERY_STRING
REMOTE_ADDR 66.186.243.74
REMOTE_HOST 66.186.243.74
REMOTE_PORT 50339
REQUEST_METHOD GET
SCRIPT_NAME /secure/SummaryForms.aspx
SERVER_NAME docksidevalidation.org
SERVER_PORT 80
SERVER_PORT_SECURE 0
SERVER_PROTOCOL HTTP/1.1
SERVER_SOFTWARE Microsoft-IIS/6.0
URL /secure/SummaryForms.aspx
HTTP_CONNECTION keep-alive
HTTP_ACCEPT text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
HTTP_ACCEPT_CHARSET ISO-8859-1,utf-8;q=0.7,*;q=0.7
HTTP_ACCEPT_ENCODING gzip, deflate
HTTP_ACCEPT_LANGUAGE en-us,en;q=0.5
HTTP_COOKIE ASP.NET_SessionId=yka231rdr1r3yqv0nmbhz1nh; .POWERBASECOOKIE=5FC2CF98E578B444ACA6B75A382EAEF31EFC781DA11AF7884B360A82E544539BEC5E5BB62DD012A1D6E7005869FFF8A205D632FB724AB01EFDBED78A9A035C76B22250F8FF0BCC3AD9AB56C4EEC44C05159110DF95A6344888CB3300B642E312F3B152475CD4FA631C0000699763AE173978375D598154CE85AEF83C81E1FAF82842DF48F1D9AC97872F09FFCA33B22B; .ASPXROLES=IS-YIxYt7iXk72HXdLNE18SBwgsEd2wo3D4GfRI72jYdSmmLhUxfzBMMpFYXg5CjiowldcwSKolBjaP8jrMUgh5-rQWjQhUTY02VS3IhD5Xcav_B-d-MiaumYD-0JMWFR9-1xcP2eHQXLpnZlXzcZXhwh6G2mEDVoNZGls8MNK8bAVlo2WuTqReMmk1GTyu5duoZzh_fCs1S6AZuhWY9bwdi28kA_lfoyp4SCm2zLgNddjhbsLmgN5oopdPo9-oONS7Z5br51qYu5sS9vq-hm_TbHhHL_W7XbIKo3pJccZ1-VXkLbtIAiS_hSccjFmk1f3ttWhUnmULJVQaNNepOHofbivSEQvhQQxX-f5hpGI4K-9FqcJ-kFAjw93CrnZ2rtu6n8i3tblPEDQeAbd1JkSJjmKGpSH_4PVh1MsXNDZwSSnbBHqTaO9iKiVAvUWvANLbBxmOc3w0SfP4DUbLtl3YZVCbG_JWr5s0f5Is8scr7Nc-AxK2HEzjNVber1EofJK48eLaVJPyvm-858Yhtp5XgqMeJQUwh5l9LYeZNbc8kqxPG7kOl0Zq7q8407I230
HTTP_HOST docksidevalidation.org
HTTP_REFERER http://docksidevalidation.org/secure/default.aspx
HTTP_USER_AGENT Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0.2) Gecko/20100101 Firefox/6.0.2
Powered by ELMAH, version 1.1.11517.2009. Copyright (c) 2004-9, Atif Aziz. All rights reserved. Licensed under Apache License, Version 2.0.
The exception suggests that the set you are querying with Linq contains multiple items that match your query, and you are calling SingleOrDefault
which throws an exception in such a situation.
You might be looking to use FirstOrDefault
instead which takes the first match of the query.
Though it's hard to tell because I don't see a call to SingleOrDefault
in your pasted code sample.
I have solved the problem! Thanks for your imput. The issue was a duplicate record in the database was inserted and when the program was pulling the data back it was only suppose to pull one record back and it was getting 2. Thanks!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With