private void RenderizarRelatorios()
        {
            string serializedObject;
            string strDecompressedString;
            RGSws.RGSWsSoapClient _wsRGS = new RGSws.RGSWsSoapClient();
            ReportsContainer reportContainer = new ReportsContainer();

            //reportContainer.CredentialType = CredentialType.NetworkCredential;
            //reportContainer.ReportServiceUrl = "http://wibr001505/ReportServer/ReportService2005.asmx";
            //reportContainer.ReportExecutionUrl = "http://wibr001505/ReportServer/ReportExecution2005.asmx";

            //ReportContainer///////////////////////////////////////////////////////////////////////////////
            reportContainer.CredentialType = CredentialType.CustomCredential;
            reportContainer.ReportUserLogin = "******";
            reportContainer.ReportUserPassword = "******";
            reportContainer.NetworkDomain = "ReportServiceNetworkDomain";
            reportContainer.ReportServiceUrl = "http://192.168.0.120/ReportServer/ReportService2005.asmx";
            reportContainer.ReportExecutionUrl = "http://192.168.0.120/ReportServer/ReportExecution2005.asmx";
            reportContainer.LogReports = true;
            ////////////////////////////////////////////////////////////////////////////////////////////////

            //Report////////////////////////////////////////////////////////////////////////////////////////
            Report report = new Report();
            report.ReportName = "REPORTS/TESTS/MyReport";
            report.ReportFormat = ReportFormat.EXCEL;

            report.Parameters.Add("Param1", "2012-01-01");
            report.Parameters.Add("Param2", "value");

            report.StoreReportInDB = true;
            ////////////////////////////////////////////////////////////////////////////////////////////////

            reportContainer.Reports.Add(report);

            serializedObject = JsonHelper.ConvertToJSON(reportContainer);

            strDecompressedString = GZipHelper.Decompress(_wsRGS.RenderReports(serializedObject));

            Feedback _feed = JsonHelper.ConvertToObject<Feedback>(strDecompressedString);

            if (_feed.Status == Feedback.TypeFeedback.Success)
            {
                txtResultado.Text = strDecompressedString;
                reportContainer = JsonHelper.ConvertToObject<ReportsContainer>(_feed.Output.ToString());

                if (reportContainer.Reports[0].ReportGenerationStatus)
                    GeraArquivo(reportContainer.Reports[0].ReportFile, true);
                else
                    txtResultado.Text = reportContainer.Reports[0].ReportErrorDescription;
            }
            else
                txtResultado.Text = _feed.Message[0];
        }
        private void UpdateReportStatus(Report report)
        {
            try
            {
                 _sqlDAc = new SqlDataAccess(_connectionString);
                 _sqlValues = new Dictionary<string, object>();

                 _sqlValues.Add("@reportGUID", report.ReportGuid);
                 _sqlValues.Add("@jsonReportResult", JsonHelper.ConvertToJSON(report));
                 if (!report.ReportGenerationStatus)
                     _sqlValues.Add("@errorDescription", report.ReportErrorDescription);

                 _sqlCommand = "UpdateReportStatus";

                 _sqlDAc.ExecuteByStoredProc(SqlDataAccess.ReturnType.NonQuery, _sqlCommand, ref _sqlValues);
            }
             catch (Exception ex)
             {
                 Exception newException = new Exception("Erro na geracao do historico do relatorio", ex);
                 throw newException;
             }
        }
        private void SaveReportInformation(Report report, ReportsContainer reportsContainer)
        {
            _sqlDAc = new SqlDataAccess(_connectionString);
            _sqlValues = new Dictionary<string, object>();

            _sqlValues.Add("@jsonReport", JsonHelper.ConvertToJSON(report));
            _sqlValues.Add("@ReportName", report.ReportName);
            _sqlValues.Add("@reportsContainerGUID", reportsContainer.ReportsContainerGuid);

            if (report.ReportGuid != new Guid())
                _sqlValues.Add("@reportGUID", report.ReportGuid);

            _sqlCommand = "SaveReportInformation";

            var result = _sqlDAc.ExecuteByStoredProc(SqlDataAccess.ReturnType.Scalar, _sqlCommand, ref _sqlValues);

            report.ReportGuid = new Guid(result.ToString());
        }
        private Report RenderReportItem(ReportsContainer reportsContainer, Report reportItem)
        {
            try
            {
                byte[] reportFile = null;
                string ReportDbFilestreamPathName = string.Empty;

                //Verificando se o arquivo será armazenado no BD para gerar seu GUID e seu DBPathName (necessário para o filestream)
                if (reportItem.StoreReportInDB)
                {
                    //Instanciando a classe BDFileStream
                    _dbFileStream = new BDFileStream.BDFileStream(_fsConnectionString);

                    Dictionary<string, string> placeholderInfo = _dbFileStream.GenerateFilePlaceholder();
                    reportItem.ReportGuid = new Guid(placeholderInfo["GUIDFile"]);
                    ReportDbFilestreamPathName = placeholderInfo["bdPathName"];
                }

                if (reportsContainer.LogReports)
                {
                    //Salva as informações sobre o relatório no Banco de dados
                    SaveReportInformation(reportItem, reportsContainer);
                }

                //Renderiza o relatório
                reportFile = RenderReport(reportItem);

                //Define a variável do status de renderização do relatório
                reportItem.ReportGenerationStatus = true;

                if (reportItem.StoreReportInDB)
                {
                    //Utilizando o método que armazena o relatório no SQL
                    _dbFileStream.StoreFileByDbPathName(reportFile, ReportDbFilestreamPathName);
                }

                if (reportsContainer.LogReports)
                {
                    //Atualiza o status do relatório
                    UpdateReportStatus(reportItem);
                }

                //Armazena o arquivo no objeto Report
                reportItem.ReportFile = reportFile;
            }
            catch(Exception ex)
            {
                if (reportItem.StoreReportInDB && reportItem.ReportGuid != new Guid())
                    _dbFileStream.DeleteFileByGuid(reportItem.ReportGuid);

                throw ex;
            }

            return reportItem;
        }
        private byte[] RenderReport(ReportsContainer reportsContainer, Report report)
        {
            byte[] reportFile = null;
            string ReportDbFilestreamPathName = string.Empty;

            if (string.IsNullOrEmpty(reportsContainer.ReportServiceUrl) || string.IsNullOrEmpty(reportsContainer.ReportExecutionUrl))
            {
                throw new Exception("Atributos ReportServicesUrl e ReportExecutionUrl devem ser definidos");
            }

            //Instancia as classes do ReportingService
            _rsService = new ReportingService2005();
            _rsExecution = new ReportExecutionService();

            if (reportsContainer.CredentialType == CredentialType.DefaultCredential)
            {
                _rsService.Credentials = CredentialCache.DefaultCredentials;
                _rsExecution.Credentials = CredentialCache.DefaultCredentials;
            }
            else if (reportsContainer.CredentialType == CredentialType.NetworkCredential)
            {
                _rsService.Credentials = CredentialCache.DefaultNetworkCredentials;
                _rsExecution.Credentials = CredentialCache.DefaultNetworkCredentials;
            }
            else
            {
                _credential = new NetworkCredential
                {
                    UserName = reportsContainer.ReportUserLogin,
                    Password = reportsContainer.ReportUserPassword,
                    Domain = reportsContainer.NetworkDomain
                };
                _rsService.Credentials = _credential;
                _rsExecution.Credentials = _credential;
            }

            //Define a URL do Servidor que vai gerar o relatório
            _rsService.Url = reportsContainer.ReportServiceUrl;
            _rsExecution.Url = reportsContainer.ReportExecutionUrl;

            _reportName = @"/" + report.ReportName;
            _historyID = null;
            _forRendering = false;

            //Carrega a session para o relatorio selecionado
            ReportExecution2005.ExecutionInfo _executionInfo = _rsExecution.LoadReport(_reportName, _historyID);

            //Prepara os parametros do relatório.
            ReportExecution2005.ParameterValue[] reportExecutionParameters = new ReportExecution2005.ParameterValue[report.Parameters.Count];

            //lista todos os parametros do objeto Report e carrega nos parametros do relatório
            int contador = 0;
            foreach (KeyValuePair<string, object> parametro in report.Parameters)
            {
                reportExecutionParameters[contador++] = new ReportExecution2005.ParameterValue
                {
                    Name = parametro.Key,
                    Value = parametro.Value.ToString()
                };
            }

            //definindo os valores dos parametros do relatório
            _rsExecution.SetExecutionParameters(reportExecutionParameters, "en-us");

            //o relatorio é armazenado como um array de bytes
            if (report.ReportFormat == ReportFormat.HTML4)
                reportFile = _rsExecution.Render("HTML4.0", _deviceInfo, out _extension, out _encoding, out _mimeType, out _warnings, out _streamIDs);
            else
                reportFile = _rsExecution.Render(report.ReportFormat.ToString(), _deviceInfo, out _extension, out _encoding, out _mimeType, out _warnings, out _streamIDs);

            return reportFile;
        }
        private byte[] RenderReport(Report report)
        {
            byte[] reportFile = null;
            string ReportDbFilestreamPathName = string.Empty;

            _reportName = @"/" + report.ReportName;
            _historyID = null;
            _forRendering = false;

            //busca no reportService os parametros exigidos pelo relatório (caso queira utilizar para alguma verificação)
            //ReportService2005.ParameterValue[] _parameterValues = null;
            //ReportService2005.DataSourceCredentials[] _dsCredentials = null;
            //ReportService2005.ReportParameter[] _reportParameters = null;
            //_reportParameters = _rsService.GetReportParameters(_reportName, _historyID, _forRendering, _parameterValues, _dsCredentials);

            //Carrega a session para o relatorio selecionado
            ReportExecution2005.ExecutionInfo _executionInfo = _rsExecution.LoadReport(_reportName, _historyID);

            //Prepara os parametros do relatório.
            ReportExecution2005.ParameterValue[] reportExecutionParameters = new ReportExecution2005.ParameterValue[report.Parameters.Count];

            //lista todos os parametros do objeto Report e carrega nos parametros do relatório
            int contador = 0;
            foreach (KeyValuePair<string, object> parametro in report.Parameters)
            {
                reportExecutionParameters[contador++] = new ReportExecution2005.ParameterValue
                {
                    Name = parametro.Key,
                    Value = parametro.Value.ToString()
                };
            }

            //definindo os valores dos parametros do relatório
            _rsExecution.SetExecutionParameters(reportExecutionParameters, "en-us");

            //o relatorio é armazenado como um array de bytes
            if (report.ReportFormat == ReportFormat.HTML4)
                reportFile = _rsExecution.Render("HTML4.0", _deviceInfo, out _extension, out _encoding, out _mimeType, out _warnings, out _streamIDs);
            else
                reportFile = _rsExecution.Render(report.ReportFormat.ToString(), _deviceInfo, out _extension, out _encoding, out _mimeType, out _warnings, out _streamIDs);

            return reportFile;
        }