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

            Debug.WriteLine("* * *  ExportController GetResponseCsv fileId: " + paramFileId);

            // Handle older versions of Venture
            string paramVentureVersion = Request["ventureVersion"] as string;
            string msgVentureVersion   = String.IsNullOrEmpty(paramVentureVersion) ? "* No ventureVersion. *" : paramVentureVersion;

            int fileId = 0;

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


            IFormsSql formsSql  = new FormsSql();
            string    tableName = formsSql.GetResultsTableName(fileId);

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

            string sqlQuery = GetSqlQryForCsv(fileId);

            return(CreateCsvStream(formsSql.GetConnectionString(), sqlQuery));
        }
        private string GetSqlQryForCsv(int fileId)
        {
            IFormsSql formsSql  = new FormsSql();
            string    tableName = formsSql.GetResultsTableName(fileId);

            StringBuilder stringBuilder = null;

            List <int>  formResultIds = SessionHelper.Read <List <int> >("formResultIds");
            List <int?> contactIds    = SessionHelper.Read <List <int?> >("contactIds");


            if (formResultIds == null || contactIds == null)
            {
                GetAuthorizedFormResultIds(formsSql, out formResultIds, out contactIds);
                SessionHelper.Write("formResultIds", formResultIds);
                SessionHelper.Write("contactIds", contactIds);
            }

            if (formResultIds != null && formResultIds.Count > 0)
            {
                if (tableName == "Contact" || tableName == "Recipient")
                {
                    stringBuilder = getSqlQryForRecCon(tableName, contactIds);

                    return(stringBuilder.ToString());
                }

                if (tableName == "def_FormResults")
                {
                    // if 'old' Venture
                    string frFields = "*";
                    if (1 == 3)
                    {
                        frFields = "formResultId, formId, formStatus, sessionStatus, dateUpdated, deleted, locked, archived, EnterpriseID, GroupID, subject, interviewer, assigned, training, reviewStatus, statusChangeDate ";
                    }

                    stringBuilder = new StringBuilder("SELECT " + frFields + " FROM def_FormResults");
                }
                else if (tableName == "def_ItemResults")
                {
                    stringBuilder = new StringBuilder("SELECT * FROM def_ItemResults");
                }
                else if (tableName == "def_ResponseVariables")
                {
                    stringBuilder = new StringBuilder("SELECT def_ResponseVariables.* FROM def_ResponseVariables");
                    stringBuilder.Append(" JOIN def_ItemResults on def_ItemResults.itemResultId = def_ResponseVariables.itemResultId");
                }

                stringBuilder.Append(" WHERE (");
                for (int i = 0; i < formResultIds.Count; i++)
                {
                    stringBuilder.Append("formResultId = " + formResultIds[i]);

                    if (i < formResultIds.Count - 1)
                    {
                        stringBuilder.Append(" OR ");
                    }
                }

                stringBuilder.Append(")");

                return(stringBuilder.ToString());
            }

            return(null);
        }