Wednesday, November 23, 2011

Dynamically create a Report in ReportViewer

Here im going to tell you about how to create a report using a report viewer by coding.

First you should create a typed DataSet with the structure of your Table(or how you need the table to be).
Now the next step is to create a report

Website/Project -> Add New Item -> Report
Give a name for it.
After this Select the Dataset from the New menu displayed in the rdlc file
The dataset will be diplayed in the report data window.
You must have to provide a Name for the dataset. In DataSource you will get the Typed DataSet you created.
Select the DataTable you created in the Dataset in the AvailableDatasets section and click OK.
Now, You will get the information in the left pane window.

Above you can see the Dataset added to the Table. Here the DataSet name given by me is DataSetSmilu. You should remember this name when we Bind the data from the program....

Now you can right click on the report create a table in the Report page and add the Fileds shown above like this.


Now you have the report file to be attached with the Report Viewer. You can add images, header etc to this page...

Now create a new WebForm.
From the ToolBox add a Ajax ScriptManager into the page
Now from the Reports Section add a Report Viewer into the page.
Select the ToolTip of the ReportViewer and Select the .rdlc file that you have created.

Now whenever you add a Report.rdlc file an ObjectDataSource will be added to the page. We dont need that objectdatasource for the working here...
So You can delete the ObjectDataSource.
Now you should go to Source of the Page and remove the ObjectDataSource Link on the LocalReport section of Reportviewer


        <rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana"
            Font-Size="8pt" InteractiveDeviceInfos="(Collection)"
            WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt" Width="651px">
            <LocalReport ReportPath="rerportsmilu.rdlc">
                <DataSources>
                   <rsweb:ReportDataSource />
                </DataSources>
            </LocalReport>
         </rsweb:ReportViewer>

After editing the form should look like this.

Now to our coding part.
Goto your codebehind file. ie the .cs file of the page and bind the data to the dataset.


using System.Data.SqlClient;
using Microsoft.Reporting.WebForms;
using System.Web.Configuration;

//code in page load or some methods..

 smiluDS tDs = new smiluDS();
            SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["NationalBankConnectionString"].ConnectionString);
            SqlDataAdapter da = new SqlDataAdapter("Select * from Employee", con);
            da.Fill(tDs, tDs.Tables[0].TableName);


            ReportDataSource rDs = new ReportDataSource("DataSetSmilu", tDs.Tables[0]);
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(rDs);
            ReportViewer1.LocalReport.Refresh();
//code complete

In the above code the We are using a SqlDataAdapter to get the data.
Now since we are using a TypedDataSet you should create an instance of the TypedDataSet itself.. Here it is smiluDS. Now  a ReportDataSource should be created for the reportviewer. In the report viewer we should specify the DataSet which we have created in the RDLC file and data to be Binded into it..

Try to run it.. you will get the d





Monday, November 21, 2011

VB to C# Application Path in WindowsService

This method in VB.NET can be written like the below reflection method. The below method gets you the location of your application
My.Application.Info.DirectoryPath


System.Reflection.Assembly.GetExecutingAssembly().Location;