Saturday, October 31, 2015

Create Custom Report Viewer Using the WebBrowser Control in Windows Application





In this article, I will show you how to Create Custom Report Viewer Using the WebBrowser Control in Windows Application.
1. First of all open Visual Studio 2010 and go to File-> New-> Project.
1-Create Custom Report Viewer Using the WebBrowser Control in Windows Application
2. A New Project window opens. In that window select Visual C# from left pane and select Windows Forms Application as shown in below screen shot.
2-Create Custom Report Viewer Using the WebBrowser Control in Windows Application
Then click on OK button.
3. Visual Studio 2010 will add a single form to the project calledForm1.cs. Rename that form to WindowsReportViewer.cs.
4. Resize that form to 1024 x 768 dimension.
4-Create Custom Report Viewer Using the WebBrowser Control in Windows Application
5. Now add a textbox, and name it txtReportURL. Also add a button namedbtnShowReport and set the button Text property to Show Report.
6. Then add WebBrowser control named as WebBrowser from common controls. Now your form design looks like below :
6-Create Custom Report Viewer Using the WebBrowser Control in Windows Application
7. Now write below code into the click event of Show Report button.
?
1
2
3
4
private void btnShowReport_Click(object sender, EventArgs e)
{
    WebBrowser.Navigate(txtReportURL.Text);
}
7-Create Custom Report Viewer Using the WebBrowser Control in Windows Application
8. Now press F5 button. In textbox enter the URL to access the report which is something like below :
http://bhushan-pc/ReportServer2012/Pages/ReportViewer.aspx?%2fStartSSRS%2fPersonAddressDetails&rs:Command=Render 
9. Then click on Show Report button. You will see result like below :
9-Create Custom Report Viewer Using the WebBrowser Control in Windows Application

Use Report Viewer Control to View SSRS Report in Windows Application





1. First of all open Visual Studio 2010 and go to File-> New-> Project.
1-Use Report Viewer Control to View SSRS Report in Windows Application
2. A New Project window opens. In that window select Visual C# from left pane and select Windows Forms Application as shown in below screenshot.
2-Use Report Viewer Control to View SSRS Report in Windows Application
Then click on OK button.
3. Visual Studio 2010 will add a single form to the project calledForm1.cs. Rename that form to ViewerForm.cs.
4. Resize that form to 1024 x 768 dimension.
4-Use Report Viewer Control to View SSRS Report in Windows Application
5. Now add a ReportViewer control named as rptViewer from Reportingcontrols.
6. Then add a textbox, and name it txtReportURL. Also add a button namedbtnShowReport and set the button Text property to Show Report. Now your form design looks like below:
6-Use Report Viewer Control to View SSRS Report in Windows Application
7. Now write below code into the click event of Show Report button.
?
1
2
3
4
5
6
7
private void btnShowReport_Click(object sender, EventArgs e)
{
    rptViewer.ProcessingMode = ProcessingMode.Remote;
    rptViewer.ServerReport.ReportServerUrl = new Uri(@"http://bhushan-pc/ReportServer2012");
    rptViewer.ServerReport.ReportPath = txtReportURL.Text;
    rptViewer.RefreshReport();
}
7-Use Report Viewer Control to View SSRS Report in Windows Application
8. Now press F5 button. In textbox enter the Path to access the report which is something like below :
/StartSSRS/PersonWithPhoneNumber
9. Then click on Show Report button. You will see result looks like below :
9-Use Report Viewer Control to View SSRS Report in Windows Application

Export SSRS Report into PDF Format from ASP.NET Application





Now a days, it is often required to execute SSRS Report from ASP.NET web application and export that report on the disk in different formats like PDF,XLS etc.
In this article, I will show you how to Export SSRS Report into PDF from ASP.NET Web Application.
I assumed that you already developed a SSRS Report and deployed it  to the Report Server.
1. First of all open Visual Studio 2010 and create a new website or open an existing website. In that add new web page and give appropriate name.
2. Now in your web page add following controls:
  • One  ScriptManager
  • One Button
  • One MicrosoftReportViewer
So your web page design will look like following:
2-Export SSRS Report into PDF Format from ASP.NET Application
3. Now add following code on button’s click event in your .cs file :
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
MyReportViewer.ProcessingMode = ProcessingMode.Remote;
MyReportViewer.ServerReport.ReportServerUrl = new Uri("http://10.88.141.76:8080/ReportServer2012"); // Report Server URL
MyReportViewer.ServerReport.ReportPath = "/StartSSRS/PersonAddressDetails";                         // Report Name
MyReportViewer.ServerReport.Refresh();
Microsoft.Reporting.WebForms.ReportParameter[] reportParameterCollection = new Microsoft.Reporting.WebForms.ReportParameter[1];
reportParameterCollection[0] = new Microsoft.Reporting.WebForms.ReportParameter();
reportParameterCollection[0].Name = "City";                                                         //Parameter Name
reportParameterCollection[0].Values.Add("Seattle");                                                 //Parameter Value
MyReportViewer.ServerReport.SetParameters(reportParameterCollection);
Warning[] warnings;
string[] streamids;
string mimeType, encoding, extension, deviceInfo;
deviceInfo = "True";
byte[] bytes = MyReportViewer.ServerReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings);
Response.Buffer = true;
Response.Clear();
Response.ContentType = mimeType;
/*
    This header is for saving it as an Attachment and popup window should display to to offer save as or open a PDF file
    Response.AddHeader("Content-Disposition", "attachment; filename=" + extension);       
*/
/*
    This header is use for open it in browser.
    Response.AddHeader("content-disposition", "inline; filename=myfile." + extension);
    Response.BinaryWrite(bytes);
*/
//Creatr PDF file on disk
string pdfPath = @"D:\TempReports\PersonAddressDetails." + extension;       // Path to export Report.
System.IO.FileStream pdfFile = new System.IO.FileStream(pdfPath, System.IO.FileMode.Create);
pdfFile.Write(bytes, 0, bytes.Length);
pdfFile.Close();
Response.Flush();
Response.End();
4. Now set this page as a startup and then press F5. In browser click on Export Report button. It will export report on the disk.
Now browse to path on which Report is Exported. You will see report over there.
4-Export SSRS Report into PDF Format from ASP.NET Application
Similarly, If you want to export report in excel, make following changes in line no18 of above code:
?
1
byte[] bytes = MyReportViewer.ServerReport.Render("EXCEL", null, out mimeType, out encoding, out extension, out streamids, out warnings);