public FileStreamResult GetUasCsv()
        {
            string paramFileId = Request["fileId"] as string;

            Debug.WriteLine("* * *  ExportController GetUasCsv fileId: " + paramFileId);
            int fileId = 0;

            try
            {
                fileId = Convert.ToInt32(paramFileId);
            }
            catch (Exception exptn)
            {
                Debug.WriteLine("* * *  ExportController GetUasCsv Exception: " + exptn.Message);
            }
            Debug.WriteLine("* * *  ExportController GetUasCsv fileId: " + fileId.ToString());

            IUasSql uasSql    = new UasSql();
            string  tableName = uasSql.GetUasTableName(fileId);

            if (String.IsNullOrEmpty(tableName))
            {
                string errMsg = "Error: Invalid fileId: " + paramFileId;
                return(ProcessError(errMsg));
            }

            Debug.WriteLine("   Connection: " + uasSql.GetConnectionString());

            string sqlQuery = getUASQuery(tableName);

            return(CreateCsvStream(uasSql.GetConnectionString(), sqlQuery));
        }
        public string DownloadUasTables(CookieContainer cc)
        {
            // fileIds and filters
            // 0 - Config - All recs
            // 1 - Enterprise - a single record - the Enterprise of the User (from Session)
            // 2 and 3 - GroupTypes / Groups for the Enterprise
            // 4 - User - just the user logged in (from Session)

            IUasSql uas = new UasSql();
            string  msg = String.Empty;

            for (int i = 0; i < uas.GetNumberTables(); i++)
            {
                try
                {
                    HttpWebRequest httpRequest = null;

                    httpRequest = (HttpWebRequest)WebRequest.Create(sisOnlineURL + "Export/GetUasCsv?fileId=" + i.ToString());
                    httpRequest.CookieContainer = cc;
                    httpRequest.Method          = WebRequestMethods.Http.Get;

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

                            csvData.TableName = uas.GetUasTableName(i);
                            Debug.WriteLine("DataSync DownloadUasTables CvsReader on table: " + csvData.TableName);
                            // Debug.WriteLine("                          httpResponseStream.Length: " + httpResponseStream.Length.ToString());

                            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("        cvsData Table {0} contains {1} rows.", csvData.TableName, csvData.Rows.Count);
                            msg     = csvData.Rows.Count.ToString();
                            csvData = RemoveNullString(csvData);

                            Debug.WriteLine("       Modify csvData: " + csvData.TableName);
                            if (csvData == null)
                            {
                                Debug.WriteLine("       uh-oh csvData is Null!!!");
                            }

                            using (DbConnection connection = UasAdo.GetUasAdoConnection())
                            {
                                if (csvData.TableName == "uas_Group")
                                {
                                    SetParentGroupToSelf(ref csvData);
                                }

                                SetCreatedByAndModifiedByToOne(ref csvData);

                                MarkDuplicates(ref csvData);

                                FillDatabaseTable(csvData, connection);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("* * *  DataSync exception: " + ex.Message);
                    if (ex.InnerException != null && ex.InnerException.Message != null)
                    {
                        Debug.WriteLine("Inner exception: " + ex.InnerException.Message);
                    }
                    msg = ex.Message;
                }
            }
            return(msg);
        }