Click here to Skip to main content
15,887,350 members
Home / Discussions / C#
   

C#

 
Questionforms and SSO login Pin
V.20-Sep-16 1:19
professionalV.20-Sep-16 1:19 
QuestionSSRS Loop Through Report Datasets and Modify at Runtime Pin
David_4119-Sep-16 9:20
David_4119-Sep-16 9:20 
AnswerRe: SSRS Loop Through Report Datasets and Modify at Runtime Pin
Gerry Schmitz19-Sep-16 10:58
mveGerry Schmitz19-Sep-16 10:58 
GeneralRe: SSRS Loop Through Report Datasets and Modify at Runtime Pin
David_4119-Sep-16 11:13
David_4119-Sep-16 11:13 
GeneralRe: SSRS Loop Through Report Datasets and Modify at Runtime Pin
Gerry Schmitz19-Sep-16 18:05
mveGerry Schmitz19-Sep-16 18:05 
GeneralRe: SSRS Loop Through Report Datasets and Modify at Runtime Pin
David_4120-Sep-16 5:19
David_4120-Sep-16 5:19 
GeneralRe: SSRS Loop Through Report Datasets and Modify at Runtime Pin
Gerry Schmitz20-Sep-16 5:31
mveGerry Schmitz20-Sep-16 5:31 
GeneralRe: SSRS Loop Through Report Datasets and Modify at Runtime Pin
David_4120-Sep-16 7:53
David_4120-Sep-16 7:53 
Thanks. Here is the code which handles modifying the Embeded Dataset to point to a different stored procedure. This code requires that you add a web reference to ReportingService2010.

What this code does not show is the following: Earlier in the code we read the SQL from a stored procedure and allow the user to pick some criteria and add a WHERE clause to the query. The new sql is then saved to a new stored procedure which includes the name of the original stored procedure plus _New.

So the goal here is to get the report to use the _New stored procedures. The datasets are embedded and must be repointed to the _New stored procedures.

The following code runs everything. At this time, the user has selected the report that they want to view.
C#
string LocalPath = "";

           if (DownloadReport(reportViewer1.ServerReport.ReportPath, out LocalPath))
           {
               string newPath = "";
               if (LoopThroughDatasetsXML(LocalPath, out newPath))
               {
                   CreateReports(cbMOE.Text + "_New", newPath, reportViewer1.ServerReport.ReportPath);
               }
           }


C#
private bool DownloadReport(string reportPath, out string LocalFolder)
        {
            bool ret = true;
            ReportingService2010.ReportingService2010 rs = new ReportingService2010.ReportingService2010();
            rs.Url = "http://" + m_dbSettings.serverName + "/" + m_dbSettings.reportDbName + "/ReportService2010.asmx";
            rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
                    
            byte[] reportDefinition = null;
            XmlDocument doc = new XmlDocument();

            try
            {
                reportDefinition = rs.GetItemDefinition(reportPath); // Get the report from the server
                MemoryStream stream = new MemoryStream(reportDefinition);

                //start building the path to save the report to the users computer
                reportPath = reportPath.Replace(@"/", @"\");
                
                LocalFolder = Directory.GetCurrentDirectory();                

                LocalFolder = LocalFolder + @"\TrialData\DataAccess\Reports" + reportPath + ".rdl";
                string Folder = Path.GetDirectoryName(LocalFolder);

                //if the folder doesn't exist, create it
                if (!Directory.Exists(Folder))
                {
                    Directory.CreateDirectory(Folder);
                }

                doc.Load(stream);//convert the document to an xml file
                doc.Save(LocalFolder);//save the document
            }
            catch (Exception ex)
            {
                ret = false;
                MessageBox.Show("Problem saving reports. " + ex.Message);
                throw ex;
            }
            return ret;
        }

        /*
         * DRW 9/20/16
         * Loop through datasets in downloaded rdl and replace stored procedure with _New stored procedure
         * Build List<SP> of dataset names and original stored procedure names
         * Save rdl as original name with _New at the end
         */
        private bool LoopThroughDatasetsXML(string reportPath, out string NewFilename)
        {
            bool ret = true;
            NewFilename = "";
            List<SP> sp = new List<SP>();
            XDocument doc = XDocument.Load(reportPath);
            XNamespace ns = doc.Root.Name.Namespace;

            try
            {
                foreach (XElement xe in doc.Descendants(ns + "DataSet"))
                {
                    SP p = new SP();
                    string commandtext = xe.Element(ns + "Query").Element(ns + "CommandText").Value;
                    xe.Element(ns + "Query").Element(ns + "CommandText").Value = commandtext + "_New";
                    XAttribute a = xe.Attribute("Name");
                    p.SPName = commandtext;
                    p.DatasetName = a.Value;
                    sp.Add(p);
                }
            }
            catch (Exception ex)
            {
                ret = false;
                MessageBox.Show("Problem looping through rdl and updating to _New Stored Procedures.\n\n" + ex.Message);

            }
            if (ret)
            {
                try
                {
                    if (sp.Count > 0)
                    {
                        //set new filename
                        NewFilename = reportPath.Substring(0, reportPath.Length - 4) + "_New.rdl";
                        doc.Save(NewFilename);//save the document
                    }
                }
                catch (Exception ex)
                {
                   ret = false;
                MessageBox.Show("Problem saving modified rdl.\n\n" + ex.Message);                   
                }
            }
            return ret;            
        }

        /*
         * Upload a report from the users computer to the report server.
         * definitionpath is the full path to the report file on the users computer.
         * parentFolder is the parent folder of the report on the report server. The folder that the report will be uploaded to. 
         */
        private void CreateReports(string reportName, string definitionPath, string parentFolder)
        {
            ReportingService2010.ReportingService2010 rsc = new ReportingService2010.ReportingService2010();
            rsc.Credentials = System.Net.CredentialCache.DefaultCredentials;

            rsc.Url = "http://" + m_dbSettings.serverName + "/" + m_dbSettings.reportDbName + "/ReportService2010.asmx";
            Byte[] definition = null;
            ReportingService2010.Warning[] warnings = null;

            try
            { //read the report from the hard drive
                FileStream stream = File.OpenRead(definitionPath);
                definition = new Byte[stream.Length];
                stream.Read(definition, 0, (int)stream.Length);
                stream.Close();
            }

            catch (IOException e)
            {
                MessageBox.Show(e.Message);
            }

            try
            { //upload the report to the server - True means overwrite existing report on the server  
                /*
                 * DRW 1-26-2016 Remove .rdl from filename before uploading to server
                 */
                parentFolder = parentFolder.Replace(cbMOE.Text, "");//strip report name from server report path
                parentFolder = parentFolder.Substring(0, parentFolder.Length - 1); // strip off trailing slash
                reportName = reportName.Replace(".rdl", ""); //strip .rdl from filename
                rsc.CreateCatalogItem("Report", reportName, parentFolder, true, definition, null, out warnings);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Problem uploading report. " + ex.Message);
            }
        }

GeneralRe: SSRS Loop Through Report Datasets and Modify at Runtime Pin
Gerry Schmitz20-Sep-16 8:34
mveGerry Schmitz20-Sep-16 8:34 
GeneralRe: SSRS Loop Through Report Datasets and Modify at Runtime Pin
David_4120-Sep-16 8:39
David_4120-Sep-16 8:39 
GeneralRe: SSRS Loop Through Report Datasets and Modify at Runtime Pin
Gerry Schmitz20-Sep-16 9:43
mveGerry Schmitz20-Sep-16 9:43 
QuestionHow to show SSRS report in asp.net from report server Pin
Tridip Bhattacharjee18-Sep-16 21:52
professionalTridip Bhattacharjee18-Sep-16 21:52 
AnswerRe: How to show SSRS report in asp.net from report server Pin
Richard MacCutchan18-Sep-16 22:20
mveRichard MacCutchan18-Sep-16 22:20 
GeneralRe: How to show SSRS report in asp.net from report server Pin
#realJSOP19-Sep-16 1:32
mve#realJSOP19-Sep-16 1:32 
AnswerRe: How to show SSRS report in asp.net from report server Pin
ZurdoDev19-Sep-16 2:06
professionalZurdoDev19-Sep-16 2:06 
AnswerRe: How to show SSRS report in asp.net from report server Pin
Dave Kreskowiak19-Sep-16 2:28
mveDave Kreskowiak19-Sep-16 2:28 
GeneralRe: How to show SSRS report in asp.net from report server Pin
Pete O'Hanlon19-Sep-16 4:20
mvePete O'Hanlon19-Sep-16 4:20 
GeneralRe: How to show SSRS report in asp.net from report server Pin
Dave Kreskowiak19-Sep-16 5:47
mveDave Kreskowiak19-Sep-16 5:47 
GeneralRe: How to show SSRS report in asp.net from report server Pin
Richard MacCutchan19-Sep-16 6:40
mveRichard MacCutchan19-Sep-16 6:40 
AnswerRe: How to show SSRS report in asp.net from report server Pin
Gerry Schmitz19-Sep-16 7:01
mveGerry Schmitz19-Sep-16 7:01 
GeneralRe: How to show SSRS report in asp.net from report server Pin
Tridip Bhattacharjee19-Sep-16 21:35
professionalTridip Bhattacharjee19-Sep-16 21:35 
QuestionPrevent Multiple Same User Logins On A Desktop Application C# Pin
Zeyad Jalil18-Sep-16 2:26
professionalZeyad Jalil18-Sep-16 2:26 
AnswerRe: Prevent Multiple Same User Logins On A Desktop Application C# Pin
Wendelius18-Sep-16 3:41
mentorWendelius18-Sep-16 3:41 
GeneralRe: Prevent Multiple Same User Logins On A Desktop Application C# Pin
Zeyad Jalil18-Sep-16 19:31
professionalZeyad Jalil18-Sep-16 19:31 
GeneralRe: Prevent Multiple Same User Logins On A Desktop Application C# Pin
Wendelius18-Sep-16 21:38
mentorWendelius18-Sep-16 21:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.