title: Execute stored procedures in parallel
body: <p>I have these 2 methods</p>
  
  <pre><code>public DataTable GetData1(int Id)
  {
      DataTable dt = new DataTable();
  
      using (SqlConnection sqlcon = new SqlConnection(database.Connection.ConnectionString))
      {
          using (SqlCommand cmd = new SqlCommand("spGetData1", sqlcon))
          {
              cmd.CommandType = CommandType.StoredProcedure;
  
              cmd.Parameters.Add(new SqlParameter() { ParameterName = "@id", Value = Id});
  
              using (SqlDataAdapter da = new SqlDataAdapter(cmd))
              {
                  da.Fill(dt);
              }
          }
      }
  
      return dt;
  }
  
  public DataTable GetData2(int Id)
  {
      DataTable dt = new DataTable();
  
      using (SqlConnection sqlcon = new SqlConnection(database.Connection.ConnectionString))
      {
          using (SqlCommand cmd = new SqlCommand("spGetData2", sqlcon))
          {
              cmd.CommandType = CommandType.StoredProcedure;
  
              cmd.Parameters.Add(new SqlParameter() { ParameterName = "@id", Value = Id});
  
              using (SqlDataAdapter da = new SqlDataAdapter(cmd))
              {
                  da.Fill(dt);
              }
          }
      }
      return dt;
  }
  </code></pre>
  
  <p>and I would like to execute them at once and also get the data for further processing.</p>
  
  <p>I tried something like</p>
  
  <pre><code>var task1 = Task.Factory.StartNew(() =&gt; database.Data.GetData1(1));
  var task2 = Task.Factory.StartNew(() =&gt; database.Data.GetData2(2));
  
  var taskList = new List&lt;Task&gt; { task1, task2 };
  
  Task.WaitAll(taskList.ToArray());
  </code></pre>
  
  <p>but on the last line it crashes with</p>
  
  <blockquote>
    <p>there is one or more errors.`</p>
  </blockquote>
  
  <p>The inner exception is </p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object.</p>
  </blockquote>
  
  <p>Stack trace</p>
  
  <blockquote>
    <p>at System.Threading.Tasks.Task.WaitAll(Task[] tasks, Int32 millisecondsTimeout, CancellationToken cancellationToken)</p>
  </blockquote>
  
  <p>The <code>connectionString</code> is obtain from <code>System.Data.Entity.DbContext.Database</code> class</p>
  
  <pre><code> public class DatabaseRepository : IDisposable
      {
          DbContext dbContext;
  
          public DatabaseRepository()
          {
               dbContext = new DbContext("connection string ...");
               Data = new DataRepository(dbContext.Database);
  
          }
          public DataRepository Data { get; set; }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/TVADo.png" rel="noreferrer"><img src="https://i.stack.imgur.com/TVADo.png" alt="enter image description here"></a></p>
  
  <p>but the error is the same even i set connection string manually, so i dont think the error is here.</p>
  
  <pre><code>   using (SqlConnection sqlcon = new SqlConnection("connection string ..."))
     {
        using (SqlCommand cmd = new SqlCommand("spGetData2", sqlcon))
        {
         ...
        }
     }
  </code></pre>
  
  <p>How can I do that? I see some examples use <code>Async</code> return type, but I don't want to duplicate these methods.</p>