Task export on MSProject with error on blank/empty tasks

Jul 1 2020 10:48 AM
I created an Addin in C# VSTO to export tasks from MSProject to a SQLServer. My code works fine until the tasks in MSProject have an empty/blank entry.
 
I have no idea for a workaround. It seems, that die foreach isn't working here.
  1. MsProject.Tasks tasks = Globals.ThisAddIn.Application.ActiveProject.Tasks;  
  2. using (SqlConnection cnn = new SqlConnection(connectionString))  
  3. {  
  4. try  
  5. {  
  6. // Open the connection to the database.  
  7. cnn.Open();  
  8. foreach (Microsoft.Office.Interop.MSProject.Task t in tasks)  
  9. {  
  10. DateTime start = (DateTime)t.Start;  
  11. DateTime finish = (DateTime)t.Finish;  
  12. using (SqlCommand cmd = new SqlCommand(sql, cnn))  
  13. {  
  14. // Create and set the parameters values  
  15. cmd.Parameters.Add("@id", SqlDbType.Int).Value = t.ID;  
  16. cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = t.Name;  
  17. cmd.Parameters.Add("@start", SqlDbType.DateTime).Value = start;  
  18. cmd.Parameters.Add("@finish", SqlDbType.DateTime).Value = finish;  
  19. int rowsAdded = cmd.ExecuteNonQuery();  
  20. }  
  21. }  
  22. MessageBox.Show("Data transfered" );  
  23. }  
In MSProject it looks like:
 
 
Does anyone have an idea how to loop over or include the blank/empty tasks?
 
Currently i only get NullPointerReference of the object as Error and the Third task won't be export to SQL Server.

Answers (1)