枚举要么没有开始,要么已经完成

我正在一个应用程序工作正常,但我收到异常

Enumeration has either not started or has already finished.

这是我的代码,它产生这个异常。

 for (int i = 0; i < SelectedItems.Count; i++)
                {
                    lineDS = new DataSet();
                    datDs = new DataSet();
                    datDs = DatelineData(SelectedItems[i].ToString(), Starttime, Endtime, NUDTextBox, txtSensorLess, dtpStartDate, dtpEndDate);
                    lineDS =GraphlineDraw(SelectedItems[i].ToString(),Starttime,Endtime,NUDTextBox,txtSensorLess,dtpStartDate,dtpEndDate);
                    if (datDs.Tables[0].Rows.Count > 0 & lineDS.Tables[0].Rows.Count > 0)
                    {
                        var dates = (from dr in datDs.Tables[0].AsEnumerable()
                                     select new
                                     {
                                         date = dr.Field("DateRecorded"),
                                     }.date).ToList();

                        var Rate = (from dr in lineDS.Tables[0].AsEnumerable()
                                    select new
                                    {
                                        rate = dr.Field(SelectedItems[i])
                                    }.rate).ToList();
                        var datesDataSource = new EnumerableDataSource(dates);
                        datesDataSource.SetXMapping(x => dateAxis.ConvertToDouble(x));
                        var RateDataSource = new EnumerableDataSource(Rate);
                        RateDataSource.SetYMapping(y => y);
                        CompositeDataSource compositeDataSourceSenssor = new CompositeDataSource(datesDataSource, RateDataSource);
                        plotter.AddLineGraph(compositeDataSourceSenssor, colors[i], 1, SelectedItems[i]);
                        //Items.Add(new ItemVM((Brush)converter.ConvertFromString(colors[i].ToString()), SelectedItems[i]));
                    }
                    plotter.LegendVisible = false;
                    //listview.ItemsSource = Items;
                }

在上面的代码中

1.)SelectedItems是一个数组列表,它包含用户在运行时选择的列名。

2.)DatelineData是一种用于获取特定列的日期的方法。

3.)GraphLineDraw是一种用于获取列值的方法。

这是我的DatelineData方法:

 private DataSet DatelineData(string items, string Starttime, string Endtime, string NUDTextBox, string txtSensorLess, string dtpStartDate, string dtpEndDate)
    {
        try
        {                               
                SqlParameter colomNamePrm, startdatePrm, EnddatePrm, StartPrm, EndPrm;                                        
                connection = new SqlConnection(settings);
                connection.Open();
                DataSet dateDs = new DataSet();
                command = new SqlCommand();
                command.Connection = connection;
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "GetAvgDate";
                colomNamePrm = new SqlParameter("@ColumnName", SqlDbType.VarChar);
                startdatePrm = new SqlParameter("@StartDate", SqlDbType.VarChar);
                EnddatePrm = new SqlParameter("@EndDate", SqlDbType.VarChar);
                StartPrm = new SqlParameter("@Start", SqlDbType.VarChar);
                EndPrm = new SqlParameter("@End", SqlDbType.VarChar);
                colomNamePrm.Value =items;
                startdatePrm.Value = dtpStartDate +" " + Starttime;
                EnddatePrm.Value = dtpEndDate + " " + Endtime;
                StartPrm.Value = NUDTextBox;
                EndPrm.Value = txtSensorLess;
                command.Parameters.Add(colomNamePrm);
                command.Parameters.Add(startdatePrm);
                command.Parameters.Add(EnddatePrm);
                command.Parameters.Add(StartPrm);
                command.Parameters.Add(EndPrm);
                adapter.SelectCommand = command;
                adapter.Fill(dateDs); 

            return dateDs;
        }
        catch (Exception ex) { throw ex; }
        finally
        {
            command.Dispose();
            adapter.Dispose();
            connection.Close();
        }
    }

这是GraphLineDraw方法

  private DataSet GraphlineDraw(string selecteditem, string Starttime, string Endtime, string NUDTextBox, string txtSensorLess, string dtpStartDate, string dtpEndDate)
    {
        try
        {                                              
                SqlParameter colomNamePrm, startdatePrm, EnddatePrm, StartPrm, EndPrm;                                         
                connection = new SqlConnection(settings);
                connection.Open();
                DataSet objds = new DataSet();
                command = new SqlCommand();
                command.Connection = connection;
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "getAvgColumn";
                colomNamePrm = new SqlParameter("@ColumnName", SqlDbType.VarChar);
                startdatePrm = new SqlParameter("@StartDate", SqlDbType.VarChar);
                EnddatePrm = new SqlParameter("@EndDate", SqlDbType.VarChar);
                StartPrm = new SqlParameter("@Start", SqlDbType.VarChar);
                EndPrm = new SqlParameter("@End", SqlDbType.VarChar);
                colomNamePrm.Value =selecteditem;
                startdatePrm.Value = dtpStartDate + " " + Starttime;
                EnddatePrm.Value = dtpEndDate + " " + Endtime;
                StartPrm.Value = NUDTextBox;
                EndPrm.Value = txtSensorLess;
                command.Parameters.Add(colomNamePrm);
                command.Parameters.Add(startdatePrm);
                command.Parameters.Add(EnddatePrm);
                command.Parameters.Add(StartPrm);
                command.Parameters.Add(EndPrm);
                adapter.SelectCommand = command;
                adapter.Fill(objds);                    

            return objds;
        }
        catch (Exception ex) { throw ex; }
        finally
        {
            command.Dispose();
            adapter.Dispose();
            connection.Close();
        }
    }

请帮助其紧急。

UPDATE : Exception Stack Trace

 at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
   at System.Collections.Generic.List`1.Enumerator.System.Collections.IEnumerator.get_Current()
   at Microsoft.Research.DynamicDataDisplay.DataSources.EnumerablePointEnumerator`1.GetCurrent(Point& p)
   at Microsoft.Research.DynamicDataDisplay.DataSources.CompositeDataSource.CompositeEnumerator.GetCurrent(Point& p)
   at Microsoft.Research.DynamicDataDisplay.DataSources.DataSourceHelper.d__0.MoveNext()
   at Microsoft.Research.DynamicDataDisplay.CoordinateTransformExtensions.d__0.MoveNext()
   at Microsoft.Research.DynamicDataDisplay.BoundsHelper.GetDataBounds(IEnumerable`1 points)
  at Microsoft.Research.DynamicDataDisplay.BoundsHelper.GetViewportBounds(IEnumerable`1 dataPoints, DataTransform transform)
  at Microsoft.Research.DynamicDataDisplay.LineGraph.UpdateCore()
 at Microsoft.Research.DynamicDataDisplay.ViewportElement2D.Update()
   at Microsoft.Research.DynamicDataDisplay.ViewportElement2D.OnOutputChanged(Rect newRect, Rect oldRect)
   at Microsoft.Research.DynamicDataDisplay.LineGraph.OnOutputChanged(Rect newRect, Rect oldRect)
   at Microsoft.Research.DynamicDataDisplay.ViewportElement2D.OnViewportPropertyChanged(Object sender, ExtendedPropertyChangedEventArgs e)
   at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
   at Microsoft.Research.DynamicDataDisplay.Viewport2D.RaisePropertyChangedEvent(DependencyPropertyChangedEventArgs e)
   at Microsoft.Research.DynamicDataDisplay.Viewport2D.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
   at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata,         EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue,        OperationType operationType)
 at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
   at System.Windows.DependencyObject.SetValue(DependencyPropertyKey key, Object value)
   at Microsoft.Research.DynamicDataDisplay.Viewport2D.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
   at System.Windows.FrameworkElement.OnRenderSizeChanged(SizeChangedInfo sizeInfo)
   at Microsoft.Research.DynamicDataDisplay.Viewport2D.OnRenderSizeChanged(SizeChangedInfo sizeInfo)
   at System.Windows.ContextLayoutManager.fireSizeChangedEvents()
   at System.Windows.ContextLayoutManager.UpdateLayout()
   at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
   at System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()
   at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
   at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
   at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate        catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
   at System.Threading.ExecutionContext.runTryCode(Object userData)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
 at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate        catchHandler)
   at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32        numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.Run()
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run(Window window)
   at System.W
2
额外 编辑
意见: 1
@CodingBarfield好吧,我会做到这一点:-)
额外 作者 Sunny,
@CodingBarfield这是已经修剪的代码只有必要的代码,但如果你有一些疑问,那么我会清除。
额外 作者 Sunny,
@DavidBrabant是的,我尝试。
额外 作者 Sunny,
@daryal请立即检查更新。
额外 作者 Sunny,
@CodingBarfield现在请检查更新。
额外 作者 Sunny,
请使用使用语句而不是try/catch/finally。并且不要用 throw ex 引发捕捉的异常,一个简单的 throw 会执行。如果你只是重新抛出这些例外,最好不要捕捉它们。
额外 作者 sloth,
额外 作者 sloth,
@Sunny我编辑了我的答案
额外 作者 sloth,
异常会在哪条线上产生?
额外 作者 daryal,
请删除try catch块并确切地查看它抛出异常的哪一行。
额外 作者 Eren Ersönmez,
@Sunny是我们可以远程执行的少数几件事之一。我无法在本地机器上测试代码,并且没有明显的错误。如果您将代码更改为仅使用列表而不是来自服务器的数据集,我们就可以实际测试/运行本地代码,并帮助您更快地进行调试。
额外 作者 CodingBarfield,
你有没有多线程去做,你确定没有未捕获的异常发生。还发布异常的堆栈跟踪。
额外 作者 CodingBarfield,
你可以简单地删除部分代码/简单的代码行,直到你有一些工作。目前的代码真的很难从远处进行调试。
额外 作者 CodingBarfield,
@dkson没有解释的建议将无济于事。要么解释你为什么建议或继续讨论话题。
额外 作者 CodingBarfield,

2 答案

在通过 plotter.AddLineGraph 传递 plotter.AddLineGraph 之前,检查您的集合 datesRate 是否具有相同长度 > compositeDataSourceSenssor </代码>。

if (dates.Count() != Rate.Count())
{
   //serious problem:
   //won't work, since there are X values without Y values or vice versa
}

而且,就像我在评论中所说的那样,您应该使用使用语句

private DataSet DatelineData(...)
{
    using(var connection = new SqlConnection(settings))
    {
         SqlParameter colomNamePrm, startdatePrm, EnddatePrm, StartPrm, EndPrm;                                        
         connection.Open();
         using (var command = connection.CreateCommand())
         {
             ....
             return dateDs;
         }
    }
}

摆脱讨厌的try/catch/finally块并自动调用 Dispose

EDIT: I checked again and changed my answer accordingly.

7
额外
只是想为此感谢你!它非常帮助我!
额外 作者 Bubo,
是的,你是对的
额外 作者 Sunny,

当MoveNext在最后一行被调​​用两次时,通常会出现此问题。当枚举器结束时,MoveNext被调用。

0
额外
但在代码中我没有使用它。
额外 作者 Sunny,
额外 作者 Sunny,
所以我怎么能克服这个?
额外 作者 Sunny,
你有没有在代码中看到MoveNext?
额外 作者 Sunny,
您提供的跟踪具有以下内容:“Microsoft.Research.DynamicDataDisplay.DataSources.DataSource&zwnj; Helper。 d_&zwnj; _0.MoveNext()at Microsoft.Research.DynamicDataDisplay.CoordinateTransformExt&zwnj; ensions。 d__0.MoveNext()”
额外 作者 Ebad Masood,
它被间接调用。不通过你的代码。检查我已粘贴的行下面的跟踪方法,看看它是如何被调用的。对我来说,看起来像一些显示方法。
额外 作者 Ebad Masood,
哪条线路导致异常。去你写的每一种方法,看看确切的一行。
额外 作者 Ebad Masood,