Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to create a report publishing tool with ASP.NET

In this article, we will try to make a tool used for publishing the Microsoft Reports to the Microsoft Web Service using ASP .NET.

As we know, time taken to upload all the files and link report files to data-sets is usually very high. So, we will try to make Report publishing tool which will create a Windows interface. In this interface, by just clicking the button we can upload the reports.

Note: Before proceeding, readers must have a basic knowledge on the following topics:

  • Web services
  • Microsoft's Reporting services and functions
  • Reports, data sets and data source
  • Windows application in Visual Studio
Listing 1: Shows the code to upload the reports to Reporting services
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Web.Services.Protocols;
using Microsoft.SqlServer.ReportingServices2010;

Now we will go through the steps that are required to create this tool. In the first step, we need to create a Windows application in Microsoft Visual studio and a graphical windows form interface.


Figure 1: Shows the snapshot of report publisher

Listing 2: Shows the code for the Button Click event to the Publish Button

private void btnPublish_Click(object sender, EventArgs e)
{
    lblErrorMsg.Text = String.Empty;
             
    if (flag && String.IsNullOrEmpty(txtDataSource.Text))
    {
        lblErrorMsg.Text = "Please select DataSource path !!";
        flag = false;
    }
    if (flag && String.IsNullOrEmpty(txtDataSets.Text))
    {
        lblErrorMsg.Text = "Please select DataSets path !!";
        flag = false;
    }
    if (flag && String.IsNullOrEmpty(txtReports.Text))
    {
        lblErrorMsg.Text = "Please select Reports path !!";
        flag = false;
    }
    if (flag && String.IsNullOrEmpty(txtServer.Text))
    {
        lblErrorMsg.Text = "Please select TargetServer path !!";
        flag = false;
    }
    if (flag)
    {               
        lblprogess.Visible = true;
        progressBar1.Visible = true;
        progressBar1.Show();
        progressBar1.Step = 20;
       
        ReportingService2010 rs = new ReportingService2010(); 
        // Example for http://com:8080/abcd/ReportService2010.asmx        //rs.Url=txtServer.Text+"/"+"ReportService2010.asmx";
        rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
        CreateFolders(rs);
        progressBar1.PerformStep();
        CreateDataSource(rs);
        progressBar1.PerformStep();
        createDataSet(rs, txtDataSets.Text);
        progressBar1.PerformStep();
        createReport(rs, txtReports.Text);
        progressBar1.PerformStep();
        moveFiles(rs);
        progressBar1.PerformStep();
        lblprogess.Text = "";
        progressBar1.Hide();
        lblprogess.Visible = false;
        progressBar1.Visible = false;
        SaveIntoFile();

    }
    else
    {
      //do something you wish
    }
}

Listing 3: Shows the code for creating folders in reporting service

private void CreateFolders(ReportingService2010 rs)
{
    createFolder(rs, "/", "abcd");
    createFolder(rs, "/abcd", "abcd_REPORTS");
    createFolder(rs, "/abcd", "DATASETS");
    createFolder(rs, "/abcd", "abcd_ODS");
}

private void createFolder(ReportingService2010 rs,string folderPath, string folderName)
{
    richTextBox1.SelectionColor  = Color.Green;
    
    richTextBox1.AppendText("\n["+DateTime.Now.ToString(
       "yyyy-MM-dd HH:mm:ss.fff")+"] Creating folder : " + folderName);
    try
    {
        rs.CreateFolder(folderName, folderPath, null);
        richTextBox1.SelectionColor  = Color.Green;
        richTextBox1.AppendText("\n["+DateTime.Now.ToString(
          "yyyy-MM-dd HH:mm:ss.fff")+"] Folder created: "+ folderName + " Successfully");
    }

    catch (Exception e)
    {
        richTextBox1.SelectionColor  = Color.Red;
        richTextBox1.AppendText("\n[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + 
           "] Error while creating folder : " + folderName);
        richTextBox1.AppendText("\n" + e.Message+"\n");
    }
}

In this step, after creating the folder we need to upload Reports, Datasets and Datastores.

Listing 4: Shows the code for uploading the reports

private void createReport(ReportingService2010 rs,string reportPath)
{
    foreach (string fileName in Directory.GetFiles(reportPath,"*.rdl"))
    {
        createReportInServer(rs,Path.GetFileNameWithoutExtension(fileName));
  }
}

private void createReportInServer(ReportingService2010 rs,string reportName)
{
    Byte[] definition = null;
    Warning[] warnings = null;
    try
    {
        
        FileStream stream = File.OpenRead(Path.Combine(txtReports.Text,reportName+".rdl"));
        definition = new Byte[stream.Length];
        stream.Read(definition, 0, (int)stream.Length);
        stream.Close();
    }
    catch (Exception e)
    {
        richTextBox1.SelectionColor  = Color.Red;
        richTextBox1.AppendText("\n[" + DateTime.Now.ToString(
          "yyyy-MM-dd HH:mm:ss.fff") + "] Error while reading report : " + reportName);
        richTextBox1.AppendText("\n\n" + e.Message+"\n");
    }

    try
    {
        string parent = "/abcd/abcd_ODS";
        CatalogItem report = rs.CreateCatalogItem("Report", reportName, parent,
                    true, definition, null, out warnings);
        
        if (warnings != null)
        {
            foreach (Warning warning in warnings)
            {
                richTextBox1.SelectionColor  = Color.Violet;
                richTextBox1.AppendText("\n[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + 
                  "] Warning while creating report : " + reportName);
                richTextBox1.AppendText("\n" + warning.Message+"\n");
            }
        }
            
        else
        {
            richTextBox1.SelectionColor  = Color.Green;
            richTextBox1.AppendText("\n[" + 
              DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + 
              "] Report: " + reportName + " created successfully with no warnings");
        }
    }
    catch (Exception e)
    {
        richTextBox1.SelectionColor  = Color.Red;
        richTextBox1.AppendText("\n\n[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + 
           "] Error while creating report : " + reportName);
        richTextBox1.AppendText("\n" + e.Message);
    }
   
}

Listing 5: Shows the code for creating data sets into the reporting service

private void createDataSet(ReportingService2010 rs, string datasetPath)
{
    foreach (string fileName in Directory.GetFiles(datasetPath,"*.rsd"))
    {
        createDataSetInServer(rs, Path.GetFileNameWithoutExtension(fileName));
    }
}

private void createDataSetInServer(ReportingService2010 rs, string DataSetName)
{
    Byte[] definition = null;
    Warning[] warnings = null;

    try
    {

        FileStream stream = File.OpenRead(Path.Combine(txtDataSets.Text, DataSetName+".rsd"));
        definition = new Byte[stream.Length];
        stream.Read(definition, 0, (int)stream.Length);
        stream.Close();
    }
    catch (Exception e)
    {
        richTextBox1.SelectionColor  = Color.Red;
        richTextBox1.AppendText("\n\n[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + 
          "] Error while reading Dataset : " + DataSetName);
        richTextBox1.AppendText("\n" + e.Message);
    }
    try
    {
        string parent = "/abcd/abcd_ODS";
        CatalogItem dataset = rs.CreateCatalogItem("DataSet", DataSetName, parent,
                    true, definition, null, out warnings);
        if (warnings != null)
        {
            foreach (Warning warning in warnings)
            {
                richTextBox1.SelectionColor  = Color.Violet;
                richTextBox1.AppendText("\n[" + DateTime.Now.ToString(
                  "yyyy-MM-dd HH:mm:ss.fff") + "] Warning while creating dataset : " + DataSetName);
                richTextBox1.AppendText("\n" + warning.Message+"\n");
            }
        }
        else
        {
            richTextBox1.SelectionColor  = Color.Green;
            richTextBox1.AppendText("\n[" + DateTime.Now.ToString(
              "yyyy-MM-dd HH:mm:ss.fff") + "] DataSet: " + 
              DataSetName + " created successfully with no warnings");
        }
    }
    catch (Exception e)
    {
        richTextBox1.SelectionColor  = Color.Red;
        richTextBox1.AppendText("\n[" + 
          DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + 
          "] Error while creating dataset : " + DataSetName);
        richTextBox1.AppendText("\n"+e.Message+"\n");
    }
}

Listing 6: Shows the code for creating Reports Data source and binding to SQL Server

private void CreateDataSource(ReportingService2010 rs)
{
    richTextBox1.SelectionColor  = Color.Green;
    richTextBox1.AppendText("\n[" + DateTime.Now.ToString(
      "yyyy-MM-dd HH:mm:ss.fff") + "] Creating dataSource....");
        
    string parent = "/abcd/abcd_ODS";
    string name = "abcd_ODS";
 // Define the data source definition.
    DataSourceDefinition definition = new DataSourceDefinition();
    definition.CredentialRetrieval = CredentialRetrievalEnum.Integrated;
    definition.ConnectString = "Data Source=comp_name\\abcd;Initial Catalog=abcd_ODS";
    definition.Enabled = true;
    definition.EnabledSpecified = true;
    definition.Extension = "SQL";
    definition.ImpersonateUserSpecified = false;
    
    definition.WindowsCredentials = true;

    try
    {
        rs.CreateDataSource(name, parent, true, definition, null);
        richTextBox1.SelectionColor  = Color.Green;
        richTextBox1.AppendText("\n[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + 
          "] Data Source " + name + " created at " + parent + " Successfully !!");
    }

    catch (Exception ex)
    {
        richTextBox1.SelectionColor  = Color.Red;
        richTextBox1.AppendText("\n[" + DateTime.Now.ToString(
          "yyyy-MM-dd HH:mm:ss.fff") + "] Error While creating DATASOURCE..\n");
        richTextBox1.AppendText(ex.Message);
        richTextBox1.AppendText("\n");
    }
}

Conclusion

In this article, we have achieved the target of creating a report publishing tool and for this we need to have a basic knowledge of Web services, Microsoft's Reporting services and functions, Reports, data sets and data source and Windows application in Visual Studio.



I am well versed with Computer Programming languages and possess good working knowledge on software languages such as C, Java, PHP, HTML and CSS

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login