Monday, January 03, 2011

Call SQL Server Reporting Services in ASP.NET by 5 easy steps


Hello friends,
Wish you Happy New Year.

Today I am going to share a simple way to use SQL Server Reporting Services locally. I am trying to share simple steps for creating report.
I am using North wind database and try to create a sample report which shows customer information.
Here we go
Step 1:- Create a project in my case I am using web project you can choose either web/window. In the project add a typed dataset and drag drop customers table on XSD. (You can customize schema according to your need) as shown in below fig.


Step2:- Now add a report (.RDLC)

Step 3:- When you add report then on right side a screen(Just like solution explore) will appear with name “ Web Site Data source “ in this you will find the typed dataset which we have added and when you expand the typed dataset object you will find all the table schema as show in fig.



Step 4: Now just drag drop a table from toolbars’ report item. And drag drop column from data source (type data set which shown in step3) to table column which you need to show in report.


Step 5:- Once you done with step4 now next step to call report from “Asp.net”. For this just drag drop Microsoft reporting viewer and do following code.
Below is the code
Dim sqlcon As New SqlConnection(ConfigurationManager.ConnectionStrings("connectionString").ConnectionString.ToString())
sqlcon.Open()
Dim sqlcmd As New SqlCommand
sqlcmd.CommandText = "SELECT * FROM Customers"
sqlcmd.CommandType = CommandType.Text
sqlcmd.Connection = sqlcon
Dim sda As New SqlDataAdapter
Dim ds As New myDs
sda.SelectCommand = sqlcmd
sda.Fill(ds, ds.Customers.TableName)
‘clearing report viewer
Me.ReportViewer1.LocalReport.DataSources.Clear()
‘Create datasource for report
Dim rds As New ReportDataSource()
rds.Name = "myDs_Customers"
rds.Value = ds.Customers
Me.ReportViewer1.LocalReport.DataSources.Add(rds)
‘assigning report path.
Me.ReportViewer1.LocalReport.ReportPath = "myReport.rdlc"
Me.ReportViewer1.LocalReport.Refresh()


Now run the page and you will get report as shown below.


Thanks
Rajat