title: ASP.NET Charting Control - Dynamically Adding and Removing Series of Datapoints
body: <p>If you're familiar with ASP.NET's Charting controls, the Chart object contains a number of Series objects - which are series of datapoints that can be charted. Each series can be visualized in a different way (bar or point or line) on the same chart.</p>
  
  <p>I have a custom control that I use to create and remove and modify lists of Series in a UI. Upon clicking a button, the chart is created using those Series. If I try to re-display the chart, however (even with identical Series) it blows up and throws a NullReferenceException. </p>
  
  <p>Here's the relevant code - I've got an object wrapping Series (because I have some custom properties in there)</p>
  
  <pre><code>public class DataSeries
  {
      private Series _series = new Series();
      ... (bunch of other properties)
      public Series Series
      {
          get { return _series; }
          set { _series = value; }
      }
  }
  </code></pre>
  
  <p>In the control itself, I store a list of these as a property(I only create the object during non-postbacks because I want the list to persist):</p>
  
  <pre><code>private static List&lt;DataSeries&gt; seriesList;
  
  public List&lt;DataSeries&gt; ListOfSeries
      {
          get { return seriesList; }
          set { seriesList = value; }
      }
  
  protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {
              seriesList = new List&lt;DataSeries&gt;();
          }
      }
  </code></pre>
  
  <p>Then I have some amalgam of controls that I can use to add new DataSeries, remove them, and modify their properties. That all works.</p>
  
  <p>When they click 'Create Chart' on the page, this is the code that gets executed:</p>
  
  <pre><code>protected void refreshChart(object sender, EventArgs e)
  {
      chart.Series.Clear();
  
          foreach (DataSeries s in seriesControl.ListOfSeries)
          {
              string propertyName = s.YAxisProperty;
  
              //List of data to display for this series 
              List&lt;Sampled_Data&gt; sampleList = Sample.GetSamplesById(s.ComponentId);
  
              foreach (Sampled_Data dSample in sampleList)
              {
                  //GetPropertyValue returns a float associated with the propertyname
                  //selected for displaying as the Y Value
                  s.Series.Points.AddY(BindingLib.GetPropertyValue(dSample, propertyName));
              }
              chart.Series.Add(s.Series);
          }
      }
  </code></pre>
  
  <p>The first time I execute that piece of code, it works like a charm. 
  The second time I click on the button that executes 'refreshChart,' I get a NullReferenceException because the value of 's.Series.Points' is null. And I can't create a new object of the type that the Points property is - its constructor is private or protected.</p>
  
  <p>If I'm not manipulating the Points property between subsequent calls of this function, why is it becoming null? </p>
  
  <p>There are maybe a few solutions I could think of - make DataSeries inherit series instead of have one - then likely I could new the Points property if the error still persists. I could also deep copy the ListOfSeries and see if that solves my problem. I could also perhaps shove all my custom attributes into the Series object - it has a customfields property (or something similarly named). If I'm not passing around an object wrapping another one, that might eliminate the issue.</p>
  
  <p>Any ideas why this might be occurring and how it might be solved?</p>