3

I am working on a application everything works fine but i am getting exception

Enumeration has either not started or has already finished.

Here is my code which generate this exception.

 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<DateTime>("DateRecorded"),
                                     }.date).ToList();

                        var Rate = (from dr in lineDS.Tables[0].AsEnumerable()
                                    select new
                                    {
                                        rate = dr.Field<double>(SelectedItems[i])
                                    }.rate).ToList();
                        var datesDataSource = new EnumerableDataSource<DateTime>(dates);
                        datesDataSource.SetXMapping(x => dateAxis.ConvertToDouble(x));
                        var RateDataSource = new EnumerableDataSource<double>(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;
                }

In the above code

1.)SelectedItems is a arraylist it contains the column names which are selected by user at run time.

2.)DatelineData is a method which is using to get the dates of perticular column.

3.)GraphLineDraw is a method which is using to get the column values.

Here is my DatelineData method :

 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();
        }
    }

And this is GraphLineDraw method

  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();
        }
    }

please help its urgent.

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.<GetPoints>d__0.MoveNext()
   at Microsoft.Research.DynamicDataDisplay.CoordinateTransformExtensions.<DataToViewport>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.Windows.Application.Run()
   at IntelOptics_Vicksburg.App.Main() in C:\Documents and Settings\pc\Desktop\24-05-2012PM\Inteloptics Report        Codes\IntelOptics_Reports\IntelliOptics Reports\obj\x86\Debug\App.g.cs:line 0
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(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.Threading.ThreadHelper.ThreadStart()
sloth
  • 99,095
  • 21
  • 171
  • 219
Sunny
  • 3,185
  • 8
  • 34
  • 66
  • Do you have any multithreading going one and are you sure there are no uncatched exceptions happening. Also post the stacktrace of the exception. – CodingBarfield May 25 '12 at 06:30
  • In which line does the exception raise? – daryal May 25 '12 at 06:43
  • @DavidBrabant yes i try. – Sunny May 25 '12 at 06:47
  • 1
    Can you simply remove parts/simple lines of the code till you have something that works. The current code is really hard to debug from a distance. – CodingBarfield May 25 '12 at 06:50
  • @CodingBarfield this is the already trimmed code with only essential code,But if you have some doubt then i will clear. – Sunny May 25 '12 at 06:53
  • please remove the try catch block and see exactly which line it throwing the exception. – Eren Ersönmez May 25 '12 at 06:54
  • @Sunny its one of the few things we can do remotely. I can't test the code on my local machine and there are no obvious error's. If you change the code to simply use a list instead of a dataset from the server we get something we can actually test/run locally and help you debug faster. – CodingBarfield May 25 '12 at 06:55
  • @CodingBarfield ok i will do it :-) – Sunny May 25 '12 at 06:58
  • Please use *using* statements instead of try/catch/finally. And don't throw catched exceptions with *throw ex*, a simple *throw* will do. And if you just rethrow those exceptions, it's better not to catch them in general... – sloth May 25 '12 at 06:59
  • @dkson A suggestion without an explanation won't help. Either explain why you suggest it or stay on topic. – CodingBarfield May 25 '12 at 07:19
  • @CodingBarfield Learn more about those good coding practices from this http://stackoverflow.com/questions/75401/uses-of-using-in-c-sharp and this http://stackoverflow.com/questions/730250/is-there-a-difference-between-throw-and-throw-ex questions. – sloth May 25 '12 at 07:27

2 Answers2

7

Check if your collections dates and Rate have the same length before passing them to plotter.AddLineGraph via the compositeDataSourceSenssor.

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

And, like I said in my comment, you should really use using statements

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

to get rid of the nasty try/catch/finally block and call Dispose automatically .

EDIT: I checked again and changed my answer accordingly.

sloth
  • 99,095
  • 21
  • 171
  • 219
0

This problem usually arises when MoveNext is called twice on last row. MoveNext is being called when enumerator has ended.

Ebad Masood
  • 2,389
  • 28
  • 46
  • the trace you provided has this ' at Microsoft.Research.DynamicDataDisplay.DataSources.DataSourceHelper.d__0.MoveNext() at Microsoft.Research.DynamicDataDisplay.CoordinateTransformExtensions.d__0.MoveNext()' – Ebad Masood May 25 '12 at 06:55
  • It is being called indirectly. Not through your code. check the trace methods just below the lines i have pasted and see how is it being called. To me looks like some display method. – Ebad Masood May 25 '12 at 06:59
  • Which line is causing exception. go in each of the methods you have written and see the exact line. – Ebad Masood May 25 '12 at 07:08
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11735/discussion-between-sunny-and-ebad86) – Sunny May 25 '12 at 08:31