// GET: DataSync
        public ActionResult Index()
        {
            List <DataSync> items = new List <DataSync>();

            for (int resultTableIndex = 0; resultTableIndex < formsSql.GetNumberOfResultsTables(); resultTableIndex++)
            {
                DataSync ds = new DataSync
                {
                    ID           = resultTableIndex,
                    tableName    = formsSql.GetResultsTableName(resultTableIndex),
                    SISOnlineCnt = 0,
                    VentureCnt   = formsSql.GetTableRecordCountVenture(formsSql.GetResultsTableName(resultTableIndex)),
                    SyncStatus   = String.Empty
                };

                CookieContainer cc = new CookieContainer();
                try
                {
                    string         lnk     = ConfigurationManager.AppSettings["SISOnlineUrl"] + "Defws/Login?UserId=" + SessionHelper.LoginInfo.LoginID + "&pwrd=" + SessionHelper.LoginInfo.Password;
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(lnk);
                    request.Timeout         = -1;
                    request.CookieContainer = cc;

                    using (WebResponse response = request.GetResponse())
                    {
                        Debug.WriteLine("DataSync HttpWebResponse Status: " + ((HttpWebResponse)response).StatusDescription);
                        using (Stream dataStream = ((HttpWebResponse)response).GetResponseStream())
                        {
                        }
                    }
                }
                catch (Exception xcptn)
                {
                    Debug.WriteLine("DataSync HttpWebRequest: " + xcptn.Message);
                }

                try
                {
                    HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(sisOnlineURL + "DataSync/GetResultsDataTableCount?fileId=" + resultTableIndex.ToString());
                    httpRequest.Method          = WebRequestMethods.Http.Get;
                    httpRequest.CookieContainer = cc;
                    // Get back the HTTP response for web server
                    using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse())
                    {
                        using (Stream httpResponseStream = httpResponse.GetResponseStream())
                        {
                            byte[] rsp = new byte[httpResponse.ContentLength];
                            httpResponseStream.Read(rsp, 0, (int)httpResponse.ContentLength);

                            Debug.WriteLine("DataSync.Index HttpWebResponse Status: " + httpResponse.StatusDescription);
                            Debug.WriteLine(System.Text.Encoding.UTF8.GetString(rsp));

                            int onlineCnt = Convert.ToInt32(System.Text.Encoding.UTF8.GetString(rsp));
                            ds.SISOnlineCnt = onlineCnt;

                            ds.VentureCnt = formsSql.GetTableRecordCountVenture(formsSql.GetResultsTableName(resultTableIndex));
                        }
                    }
                }
                catch (System.Net.WebException wex)
                {
                    ds.SyncStatus = "SIS Server not found.";
                    Debug.WriteLine("* * *  DataSync index exception " + wex.Message);
                }
                catch (Exception excptn)
                {
                    ds.SyncStatus = excptn.Message;
                    Debug.WriteLine("* * *  DataSync index exception: " + excptn.Message);
                }
                items.Add(ds);
            }

            //Session["timeout"] = Business.Timeout.GetTotalTimeoutMinutes(SessionHelper.LoginStatus.EnterpriseID);
            //// Added by RRB 8/25/15 as an example of how to use it.
            //SessionHelper.SessionTotalTimeoutMinutes = Business.Timeout.GetTotalTimeoutMinutes(SessionHelper.LoginStatus.EnterpriseID);

            bool justLoggedIn = SessionHelper.Read <bool>("justLoggedIn");

            DataSync dsc = new DataSync();

            if (justLoggedIn)
            {
                dsc.autoDownload = true;
                SessionHelper.Write("justLoggedIn", false);
            }
            ViewBag.TableCnt    = items.Count;
            ViewBag.ResultTable = items;


            return(View("indexSync", dsc));
        }
        /// <summary>
        /// Processes the long running action.
        /// </summary>
        /// <param name="id">The id.</param>
        public string ProcessDataSync(string id)
        {
            int rspCnt         = 0;
            int resultTableCnt = formsSql.GetNumberOfResultsTables();

            lock (syncRoot)
            {
                ProcessStatus[id] = 1;
            }

            try
            {
                // Delete all result table first
                for (int DeleteOrderId = resultTableCnt - 1; DeleteOrderId >= 0; DeleteOrderId--)
                {
                    rspCnt = formsSql.DeleteTableContent(formsSql.GetResultsTableName(DeleteOrderId));
                    // ProcessStatus[id] = 2, 3, 4
                    lock (syncRoot)
                    {
                        ProcessStatus[id] += 1;
                    }
                }

                for (int fileId = 0; fileId < resultTableCnt; fileId++)
                {
                    //  2. Execute the URL to run the method on the remote server:
                    //       Export/GetResponsesCsv?fileId=1
                    // Construct HTTP request to get the file
                    HttpWebRequest httpRequest = (HttpWebRequest)
                                                 WebRequest.Create(sisOnlineURL + "Export/GetResponseCsv?fileId=" + fileId.ToString());
                    httpRequest.Method = WebRequestMethods.Http.Get;

                    DataTable csvData = new DataTable();
                    // Get back the HTTP response for web server
                    using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse())
                    {
                        Stream httpResponseStream = httpResponse.GetResponseStream();

                        // ProcessStatus[id] = 5, 7, 9
                        lock (syncRoot)
                        {
                            ProcessStatus[id] += 1;
                        }

                        using (var reader = new CsvReader(httpResponseStream))
                        {
                            // the CSV file has a header record, so we read that first
                            reader.ReadHeaderRecord();
                            csvData.Fill(reader);

                            Debug.WriteLine("Table contains {0} rows.", csvData.Rows.Count);
                        }
                        rspCnt  = csvData.Rows.Count;
                        csvData = RemoveNullString(csvData);
                    }
                    //  3. Read the FileStreamResult
                    //    - Read through the records

                    /*   using (SqlBulkCopy bulkCopy = new SqlBulkCopy(formsSql.GetConnectionString(), SqlBulkCopyOptions.KeepNulls | SqlBulkCopyOptions.KeepIdentity))
                     * {
                     *     bulkCopy.DestinationTableName = formsSql.GetResultsTableName(fileId);
                     *     bulkCopy.ColumnMappings.Clear();
                     *     foreach (var column in csvData.Columns)
                     *         bulkCopy.ColumnMappings.Add(column.ToString(), column.ToString());
                     *     bulkCopy.WriteToServer(csvData);
                     * }
                     */
                    csvData.TableName = formsSql.GetResultsTableName(fileId);

                    DbConnection connection = UasAdo.GetUasAdoConnection();

                    FillDatabaseTable(csvData, connection);
                    // ProcessStatus[id] = 6, 8, 10
                    lock (syncRoot)
                    {
                        ProcessStatus[id] += 1;
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("* * *  DataSync exception: " + ex.Message);
            }
            return(id);
        }