private DataTable GetDataTableForProducts(List<Product> products)
        {

            DataTable table = new DataTable();
            using (var reader = ObjectReader.Create(products))
            {
                table.Load(reader);
            }
            
            table.SetColumnsOrder(
                "Id",
                "Name",
                "Description",
                "Price",
                "Location",
                "Category",
                "Manufacturer",
                "Condition"
                );

            return table;
        }
        public static void DataTableToCSV()
        {
            GetFinancialYear();
            GetActualMonths();
            GetForecastMonths();

            SqlCommand cmd = new SqlCommand("SELECT DISTINCT TM.Resource_ID, TM.Project_ID, TM.Client_Name, TM.Sector_Name, TM.Project_Name, TM.Role_Name, TM.Resource_Name, TM.Type_Name,  TM.Source_Name, TM.Resource_Buy, TM.Resource_Sell" + strActualMonths + strForecastMonths + " FROM (SELECT TL.Resource_ID, PI.Project_ID, CL.Client_Name, RC.Resource_Name, PR.Resource_Buy, PR.Resource_Sell, ER.Role_Name, ES.Source_Name, PI.Project_Name, PS.Sector_Name, PT.Type_Name" + strActualMonths + " FROM  ProjectInformation as PI, RateCard as RC, TotalLookup as TL, ProjectResources as PR, ProjectSector as PS, ProjectType as PT, EmployeeRole as ER, EmployeeSource as ES, MonthlyTotals as TM, Clients as CL WHERE  PI.Project_ID = TL.Project_ID AND PI.Project_Client = CL.Client_ID AND  TL.Resource_ID = RC.Resource_ID  AND  PR.Project_ID = TL.Project_ID  AND  PR.Resource_ID = RC.Resource_ID  AND  PI.Project_Sector = PS.Sector_ID AND  PI.Project_Type = PT.Type_ID  AND  TL.IsActual = 'true'  AND  TM.Total_ID = TL.Total_ID  AND RC.Resource_Type = 'Employee' AND ER.Role_ID = PR.Employee_Role AND ES.Source_ID = PR.Employee_Source) as TM  LEFT OUTER JOIN (SELECT  PI.Project_ID, RC.Resource_Name" + strForecastMonths + "  FROM  ProjectInformation as PI, RateCard as RC, TotalLookup as TL, ProjectResources as PR, ProjectSector as PS, ProjectType as PT, MonthlyTotals as SM  WHERE  PI.Project_ID = TL.Project_ID AND  TL.Resource_ID = RC.Resource_ID AND  PR.Project_ID = PI.Project_ID AND  PR.Project_ID = TL.Project_ID AND  PR.Resource_ID = RC.Resource_ID AND  PI.Project_Sector = PS.Sector_ID AND  PI.Project_Type = PT.Type_ID AND  TL.IsActual = 'false' AND  SM.Total_ID = TL.Total_ID AND RC.Resource_Type = 'Employee') as SM on TM.Resource_Name = SM.Resource_Name AND TM.Project_ID = SM.Project_ID ORDER BY TM.Client_Name, TM.Project_Name, TM.Resource_Name", SQLcon);
            SQLcon.Open();

            dt = new DataTable();
            SqlDataAdapter sqlAd = new SqlDataAdapter(cmd);
            sqlAd.Fill(dt);
            SQLcon.Close();

            ActualIfPresent();

            //Convert nulls to zero's
            foreach (DataRow row in dt.Rows)
            {
                foreach (DataColumn col in dt.Columns)
                {
                    if (row.IsNull(col))
                        row.SetField(col, 0);
                }
            }

            //Remove unnecessary columns
            dt.Columns.Remove("Resource_ID");
            dt.Columns.Remove("Project_ID");

            dt.Columns.Add("Margin", typeof(double), "((Resource_Sell-Resource_Buy)/Resource_Sell)*100");
            dt.Columns.Add("Margin2");
            dt.Columns.Add("Resource_Sell_Euro", typeof(double), "Resource_Sell*1.26");
            dt.Columns.Add("Blank_Column");
            dt.Columns.Add("Total_Days");
            dt.Columns.Add("Total_Days2");
            dt.SetColumnsOrder("Client_Name", "Sector_Name", "Project_Name", "Type_Name", "Role_Name", "Resource_Name", "Source_Name", "Resource_Sell_Euro", "Resource_Sell", "Blank_Column", "Resource_Buy", "Margin2", "Total_Days", "Total_Days2");

            int intRowIndex = 0;

            foreach (DataRow row in dt.Rows)
            {
                double dblRowTotal = 0;
                for (int i = 14; i <= 26; i++)
                {
                    dblRowTotal += (double)(dt.Rows[intRowIndex][i]);
                }
                dt.Rows[intRowIndex]["Total_Days"] = dblRowTotal;
                dt.Rows[intRowIndex]["Total_Days2"] = dblRowTotal;
                dt.Rows[intRowIndex]["Margin2"] = (dt.Rows[intRowIndex]["Margin"]);
                if ((string)dt.Rows[intRowIndex]["Margin2"] == "∞" || (string)dt.Rows[intRowIndex]["Margin2"] == "-∞" || (string)dt.Rows[intRowIndex]["Margin2"] == "NaN" || (string)dt.Rows[intRowIndex]["Margin2"] == "Infinity" || (string)dt.Rows[intRowIndex]["Margin2"] == "-Infinity")
                {
                    dt.Rows[intRowIndex]["Margin2"] = "0";
                }
                else
                {
                    dt.Rows[intRowIndex]["Margin2"] = Math.Round(Convert.ToDouble(dt.Rows[intRowIndex]["Margin2"].ToString()), 0);
                }
                intRowIndex++;
            }

            dt.Columns.Remove("Margin");

            //Rename columns
            dt.Columns["Resource_Name"].ColumnName = "Name";
            dt.Columns["Project_Name"].ColumnName = "Project Name";
            dt.Columns["Sector_Name"].ColumnName = "Sector";
            dt.Columns["Client_Name"].ColumnName = "Client";
            dt.Columns["Type_Name"].ColumnName = "Type";
            dt.Columns["Role_Name"].ColumnName = "Role";
            dt.Columns["Source_Name"].ColumnName = "Source";
            dt.Columns["Resource_Buy"].ColumnName = "Pay (GBP)";
            dt.Columns["Resource_Sell"].ColumnName = "Sell (GBP)";
            dt.Columns["Resource_Sell_Euro"].ColumnName = "Sell (EUR)";
            dt.Columns["Blank_Column"].ColumnName = " ";
            dt.Columns["Total_Days"].ColumnName = "Total Days";
            dt.Columns["Total_Days2"].ColumnName = "Currently Mapped Out";
            dt.Columns["Margin2"].ColumnName = "Margin (%)";

            int RowIndex = 0;
            foreach (DataRow row in dt.Rows)
            {
                Id = intRowIndex;
                Client = dt.Rows[RowIndex][0].ToString();
                Sector = dt.Rows[RowIndex][1].ToString();
                Project_Name = dt.Rows[RowIndex][2].ToString();
                Project_Type = dt.Rows[RowIndex][3].ToString();
                Role = dt.Rows[RowIndex][4].ToString();
                Name = dt.Rows[RowIndex][5].ToString();
                Source = dt.Rows[RowIndex][6].ToString();
                Sell_Euro = double.Parse(dt.Rows[RowIndex][7].ToString());
                Sell_GBP = double.Parse(dt.Rows[RowIndex][8].ToString());
                spacer = "";
                Pay_GBP = dt.Rows[RowIndex][10].ToString();
                Margin = double.Parse(dt.Rows[RowIndex][11].ToString());
                Total_Days = double.Parse(dt.Rows[RowIndex][12].ToString());
                Currently_Mapped_Out = double.Parse(dt.Rows[RowIndex][13].ToString());
                M1 = double.Parse(dt.Rows[RowIndex][14].ToString());
                M2 = double.Parse(dt.Rows[RowIndex][15].ToString());
                M3 = double.Parse(dt.Rows[RowIndex][16].ToString());
                M4 = double.Parse(dt.Rows[RowIndex][17].ToString());
                M5 = double.Parse(dt.Rows[RowIndex][18].ToString());
                M6 = double.Parse(dt.Rows[RowIndex][19].ToString());
                M7 = double.Parse(dt.Rows[RowIndex][20].ToString());
                M8 = double.Parse(dt.Rows[RowIndex][21].ToString());
                M9 = double.Parse(dt.Rows[RowIndex][22].ToString());
                M10 = double.Parse(dt.Rows[RowIndex][23].ToString());
                M11 = double.Parse(dt.Rows[RowIndex][24].ToString());
                M12 = double.Parse(dt.Rows[RowIndex][25].ToString());
                RowIndex++;
            }
        }
 public static DataTable GetRegisteredParticipants(Event ev, int level)
 {
     if (level == 0)
         sql = "SELECT TeamID,TTID,CurrentLevel FROM EventRegistrations WHERE [email protected]";
     else
         sql = "SELECT TeamID,TTID,CurrentLevel FROM EventRegistrations WHERE [email protected] AND CurrentLevel>=" + level + "";
     string[] s = { "@eventID" };
     Connect.ConnectEMS();
     c = new Connect(sql, s, ev.ID);
     if (c.ds.Tables[0].Rows.Count == 0)
         return null;
     DataColumn nameColumn = new DataColumn();
     nameColumn.ColumnName = "Name";
     //DataColumn serialNoColumn = new DataColumn();
     //serialNoColumn.ColumnName = "SNo";
     DataColumn contactColumn = new DataColumn();
     contactColumn.ColumnName = "ContactNo";
     DataColumn collegeColumn = new DataColumn();
     collegeColumn.ColumnName = "College";
     DataColumn eventColumn = new DataColumn();
     eventColumn.ColumnName = "EventName";
     if (ev.IsSinglePlayer)
     {
         DataTable participantDataTable = c.ds.Tables[0];
         //participantDataTable.Columns.Add(serialNoColumn);
         participantDataTable.Columns.Add(nameColumn);
         participantDataTable.Columns.Add(contactColumn);
         participantDataTable.Columns.Add(collegeColumn);
         participantDataTable.Columns.Add(eventColumn);
         participantDataTable.Columns.Remove("TeamID");
         Connect.ConnectTT();
         int count = 1;
         foreach (DataRow row in participantDataTable.Rows)
         {
             //row[serialNoColumn] = count;
             sql = "SELECT Name,ContactNo,CollegeName FROM Participants,Colleges WHERE [email protected] AND Participants.CollegeID = Colleges.CollegeID";
             s[0] = "@ttID";
             c = new Connect(sql, s, row["TTID"]);
             row["Name"] = c.ds.Tables[0].Rows[0]["Name"].ToString();
             row["ContactNo"] = c.ds.Tables[0].Rows[0]["ContactNo"].ToString();
             row["College"] = c.ds.Tables[0].Rows[0]["CollegeName"].ToString();
             row["EventName"] = ev.Name;
             count++;
         }
         participantDataTable.SetColumnsOrder("TTID", "Name", "ContactNo", "College", "EventName", "CurrentLevel");
         participantDataTable.Columns["TTID"].ColumnName = "Tech Trishna ID";
         participantDataTable.Columns["CurrentLevel"].ColumnName = "Level";
         participantDataTable.Columns["ContactNo"].ColumnName = "Contact No";
         participantDataTable.Columns.Remove("EventName");
         if (level != 0)
             participantDataTable.Columns.Remove("Level");
         foreach (DataRow row in participantDataTable.Rows)
         {
             if (row["College"].ToString().Equals("Ajay Kumar Garg Engineering College"))
                 row["College"] = "A.K.G.E.C.";
         }
         return participantDataTable;
     }
     else
     {
         DataTable dt = c.ds.Tables[0];
         DataTable participantDataTable = new DataTable();
         DataColumn teamColumn = new DataColumn();
         DataColumn ttColumn = new DataColumn();
         DataColumn levelColumn = new DataColumn();
         levelColumn.ColumnName = "Level";
         teamColumn.ColumnName = "TeamID";
         ttColumn.ColumnName = "TTID";
         participantDataTable.Columns.Add(teamColumn);
         //participantDataTable.Columns.Add(serialNoColumn);
         participantDataTable.Columns.Add(ttColumn);
         participantDataTable.Columns.Add(levelColumn);
         participantDataTable.Columns.Add(nameColumn);
         participantDataTable.Columns.Add(contactColumn);
         participantDataTable.Columns.Add(collegeColumn);
         participantDataTable.Columns.Add(eventColumn);
         Connect.ConnectEMS();
         foreach (DataRow row in dt.Rows)
         {
             sql = "SELECT * FROM Teams WHERE [email protected]";
             s[0] = "@teamID";
             c = new Connect(sql, s, (int)row["TeamID"]);
             for (int i = 0; i < c.ds.Tables[0].Rows.Count; i++)
             {
                 participantDataTable.Rows.Add(c.ds.Tables[0].Rows[i]["TeamID"].ToString(), c.ds.Tables[0].Rows[i]["TTID"].ToString(), row["CurrentLevel"].ToString());
             }
         }
         Connect.ConnectTT();
         int count = 1;
         foreach (DataRow row in participantDataTable.Rows)
         {
             //row[serialNoColumn] = count;
             sql = "SELECT Name,ContactNo,CollegeName FROM Participants,Colleges WHERE [email protected] AND Participants.CollegeID = Colleges.CollegeID";
             s[0] = "@ttID";
             c = new Connect(sql, s, row["TTID"]);
             row["Name"] = c.ds.Tables[0].Rows[0]["Name"].ToString();
             row["ContactNo"] = c.ds.Tables[0].Rows[0]["ContactNo"].ToString();
             row["College"] = c.ds.Tables[0].Rows[0]["CollegeName"].ToString();
             row["EventName"] = ev.Name;
             count++;
         }
         participantDataTable.SetColumnsOrder("TeamID", "TTID", "Name", "ContactNo", "College", "EventName", "Level");
         participantDataTable.Columns["TTID"].ColumnName = "Tech Trishna ID";
         participantDataTable.Columns["TeamID"].ColumnName = "Team ID";
         participantDataTable.Columns["ContactNo"].ColumnName = "Contact No";
         participantDataTable.Columns.Remove("EventName");
         if (level != 0)
             participantDataTable.Columns.Remove("Level");
         foreach (DataRow row in participantDataTable.Rows)
         {
             row["Tech Trishna ID"] = "TT" + row["Tech Trishna ID"];
             if (row["College"].ToString().Equals("Ajay Kumar Garg Engineering College"))
                 row["College"] = "A.K.G.E.C.";
         }
         return participantDataTable;
     }
 }