Today we will try to create an C# winform application that will explore the SQL Server Reporting Service structure and create a report viewer using reportviewer control.
First, you must have SQL Reporting Server.I have found a detail step-by-step SQL Server 2005 Report installation guide here. After SQL Server Report has already installed, we need Web Service URL that will be referenced by our apps later. You can find this URL by open Reporting Service Configuration Manager menu, click Web Service URL. this URL should be like this "http://ServerName/ReportServer".
Now, create a new C# winform project.
- Add Web Service Reference.
Right click Project name, click Add Service Reference.
Input your Web Service URL + "/reportservice2005.asmx" in "Address" textbox.
If you webservice URL : http://MyServerName/MyReportServer, then you have to input in Address text box : http://MyServerName/MyReportServer/reportservice2005.asmx then click GO. If Service is found, it will be listed in Services List. In this apps we will use Web Service, instead of Service Reference. To add a web service of current Report Service, click Advance button, Service Reference Settings will be openned, click Add Web Reference. Once again input Web Service URL into Address textbox Click Go. If everything OK, Webservice description will be shown and default web reference name is your reporting server name. You can change this default if you want, then click Add Reference button.
After click Add Reference, in project explorer you will see a new folder named Web Reference is automatically generated,your Web Reference name has already added in this folder, ready to be used by our project. FYI, this web reference is only, some kind of "template". We don;t have to add another Web Service for different SQL Reporting Server. We only need to change Web Service URL, dynamically in source code.
OK, now we're ready to create a report service explorer.
Add A text box : ReportServerURL- (Url address);, Treeview:tvReportServer (Report Service Explorer), Listview:lvItems(Report Item) and a button:getFolders for refresh Reporting Service.
References :
using System;
//RSS_Report_Retrievers : Name Space; ReportWS : your web reference name
using RSS_Report_Retrievers.ReportWS;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Web.Services.Protocols;
add reporting Service variable in this form.
ReportWS.ReportingService2005 rs = new RSS_Report_Retrievers.ReportWS.ReportingService2005();
a helper procedure :
private void AddNode(string name,string BaseURL)
{
TreeNode newNode = new TreeNode(name);
newNode.ToolTipText = BaseURL;
tvReportServer.SelectedNode.Nodes.Add(newNode);
}
getFolders button click code :
private void getFolders_Click_1(object sender, EventArgs e)
{
tvReportServer.Nodes.Clear();
lvItems.Items.Clear();
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
TreeNode root = new TreeNode();
root.Text = "Root";
tvReportServer.Nodes.Add(root);
tvReportServer.SelectedNode = tvReportServer.TopNode;
CatalogItem[] items = null;
rs.Url = ReportServerURL.text;
// Retrieve a list of items from the server
try
{
items = rs.ListChildren("/", true);
int j = 1;
// Iterate through the list of items and find all of the folders
// and display them to the user
foreach (CatalogItem ci in items)
{
if (ci.Type == ItemTypeEnum.Folder)
{
Regex rx = new Regex("/");
int matchCnt = rx.Matches(ci.Path).Count;
if (matchCnt > j)
{
tvReportServer.SelectedNode =
tvReportServer.SelectedNode.LastNode;
j = matchCnt;
}
else if (matchCnt < selectednode =" tvReportServer.SelectedNode.Parent;" j =" matchCnt;" type ="=" strpath =" ci.Path.Split('/');" lvi =" new" text =" ci.Name;" tooltiptext =" ci.Path;" imageindex =" ci.Hidden" tag =" ItemTypeEnum.Report;" hideselection =" false;">
on Click node event :
private void tvReportServer_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
GetItems(e.Node.ToolTipText);
}
public void GetItems(string strPath)
{
CatalogItem[] items = null;
lvItems.Items.Clear();
try
{
//false : get item on selected folder
//true : get all item, include subfolder
items = rs.ListChildren(strPath=="" ? "/":strPath, false);
// Iterate through the list of items and find all of the folders
// and display them to the user
foreach (CatalogItem ci in items)
{
if (ci.Type == ItemTypeEnum.Report )
{
ListViewItem lvi = new ListViewItem();
lvi.Text = ci.Name;
lvi.ToolTipText = ci.Path;
switch (ci.Type)
{
case ItemTypeEnum.Report:
lvi.ImageIndex = ci.Hidden ? 5 : 1;
lvi.Tag = ItemTypeEnum.Report;
break;
}
lvItems.Items.Add(lvi);
}
}
}
catch (SoapException ex)
{
MessageBox.Show(ex.Detail.InnerXml.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Here is a sample application screen shot, in Part 2 we will add a Report Viewer.
to be continued....

Product Description