0
votes

I am getting an error for the life of me I can not resolve. The error I get is as follows:

[InvalidOperationException]: Sequence contains no elements at NHibernate.Linq.DefaultQueryProvider.ExecuteQuery(NhLinqExpression nhLinqExpression, IQuery query, NhLinqExpression nhQuery) at NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression)
at NHibernate.Linq.DefaultQueryProvider.Execute[TResult](Expression expression) at System.Linq.Queryable.First[TSource](IQueryable1 source) at Nichols.Web.Controllers.LoadController.RttInUse(String rttNumber) in c:\projects\NicholsFarms_demo_newCropYear\Nichols\src\Nichols.Web\Controllers\LoadController.cs:line 71 at lambda_method(Closure , ControllerBase , Object[] ) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3d() at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.b__3f() at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass33.b__32(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<>c__DisplayClass2b.b__1c() at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.b__1e(IAsyncResult asyncResult) at System.Web.Mvc.Controller.b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.MvcHandler.b__5(IAsyncResult asyncResult, ProcessRequestState innerState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

and the code I am TRYING to use is:

public ActionResult RttInUse(string rttNumber)
{
    int validNumber;
    int recommendRttNumber = 0;
    var cropYear = _session.Query<CropYear>().SingleOrDefault(x => x.IsCurrent);

    if (cropYear.RttRangeHaveBeenConfiguredForNewCropYear)
    {
        recommendRttNumber = _session.Query<Load>()
            .Where(x =>
                x.CropYear == cropYear &&
                !x.RttNumber.Contains("VOID") &&
                x.RttNumber.Length < 9)
            .OrderByDescending(x => x.Id)
            .Select(x => x.RttNumber)
            .FirstOrDefault(x => int.Parse(x) >= cropYear.RttNumberRangeStart &&
                                 int.Parse(x) <= cropYear.RttNumberRangeEnd).AsInt() + 1;
    }

    if (rttNumber == null || !int.TryParse(rttNumber, out validNumber))
    {
        return AsJson(
            "Rtt number invalid, numeric values only. Next Rtt Number should be " +
            recommendRttNumber);
    }

    var loadExists = _session.Query<Load>()
        .Any(x =>
            x.RttNumber.ToLower() == rttNumber.ToLower() &&
            x.CropYear == cropYear);

    if (loadExists)
    {
        return AsJson("Rtt Number already in use. Next Rtt Number should be " +
                      recommendRttNumber);
    }

    return AsJson(true);
}

Basically while operators enter a string such as 150001 on the form, the value they enter is compared against an acceptable range in the database.

Crop year has entries such that the year 2015 has an acceptable range of 150001 to 159999 to choose from which is RttNumberRangeStart to RttNumberRangeEnd; if it is in use, they should get recommended the current used rttnumber + 1.

1
Are you actually setting a default value somewhere? How about using the null coaleasing operator ?? together with the orDefault statements? - Thomas Fonn
The Error Dump says you are calling First(), not FirstOrDefault(). Is that error dump from the last run? We could be trying to debug the wrong problem. - James Curran
also at which line is the exception occurring? - Robert Petz
It would seem to be at Line 71 where it reads:if (rttNumber == null || !int.TryParse(rttNumber, out validNumber)) { return AsJson("Rtt number invalid, numeric values only. Next Rtt Number should be " + recommendRttNumber); } - John Edward Law
well that line number just doesn't make sense, there isn't anything about that line that is calling NHibernate's query system. are you sure you are working with a current build? have you cleaned your workspace and tested this again? @JamesCurran may be on the right track there - Robert Petz

1 Answers

2
votes

Probably the sequence below contains no elements:

.FirstOrDefault(x => int.Parse(x) >= cropYear.RttNumberRangeStart &&
                     int.Parse(x) <= cropYear.RttNumberRangeEnd)

so it cannot do the following on a null (default) element

.AsInt() + 1;

Assuming RttNumber is a string and AsInt() an extension method to convert an string to int, change it to return 0 on a null string.