1 Previous Next 

Report Viewer control and dataset


1.- I created a Sql Server 2005 database in the App_Code of my Web App. In it, I build two tables named ‘Encabs´ (‘Headers’ in

English I suppose) and ‘ Detalle’

The ‘Encabs’ table has colums that will be used in text boxes of the Header and the Bottom of the Report, like :

- Folio, date, user, total, sending warehouse, receiving warehouse, Total amount expressed in alphabetical letters etc..

The ‘Detalle’ table has colums that will be included in a table control (of the Report Items Toolbar) that will show the details of the

Report, like Article Code, Description, Unit Cost, Series numbers, quantity and (I don´t know how to say in english :

Pedimento Aduanal)

. These will be used just to create a xsd schema. Really they won´t be used directly in the program. It´s just that when you create

the xsd file, it´s mandatory (I think) to use them for configuration purposes. In fact, the program will use a couple of tables

created at runtime individualized with the 3 first characters of the User code that connected to the Web App. In this way I solved the

problema that could occur if two or more users use the Report at the same time. (Remember ?). For example : ‘Encabs_ken’ or

‘Encabs_mig’ and “Detalle_ken” or “Detalle_usr”

2.- Then I created the xsd file ( I named it ‘Traspasos.xsd’. My Web App is about traspassing merchandise between warehouses

In the company. Some of them very far from Mexico City, like Los Cabos B. Calif.). I´m pretty sure you know how to do it.

clip_image002

3- You create the Report Definition Fle (.rdlc) . I´m using a Report Viewer in Local Mode. We don´t need a huge level of impression.

clip_image004

4.- Using code, I fill the two mentioned tables with the data I need. (At Runtime)

5.- Using Code also, in the Web page that contains the Report Viewer Control, I managed to replace the data sources for it.

I saw the code in a Web Portal but sadly, I don´t remember the URL, so I send you my own code, which I developed upon what

I saw in that Portal. (In fact, the code was in C#)

Imports Microsoft.Reporting.WebForms

Imports System.Data

Imports System.Data.SqlClient

Partial Class Reporte

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim strXsd As String, strXsDet As String

strXsd = CType(Session("strxsd"), String)

strXsDet = CType(Session("strxsdet"), String)

RV1.ProcessingMode = ProcessingMode.Local

Dim LocalReport = RV1.LocalReport

LocalReport.reportPath = "C:\WebSite_Traspasos\Report.rdlc"

Dim Encabs_Set As New DataSet

Dim Detalle_Set As New DataSet

Dim Cn As New SqlConnection

Cn.ConnectionString = "Data Source=MALTAMIRANO\SQLEXPRESS;Initial Catalog=C:\WEBSITE_TRASPASOS\APP_DATA\TRASP_WORK.MDF;Integrated Security=True"

Cn.Open()

Dim Comando As New SqlCommand("SELECT * FROM " + strXsd, Cn)

Dim Adapter As New SqlDataAdapter

Adapter.SelectCommand = Comando

Adapter.Fill(Encabs_Set, strXsd)

Dim ds As New ReportDataSource

ds.Name = "Traspasos_Encabs"

ds.Value = Encabs_Set.Tables(strXsd)

LocalReport.datasources.add(ds)

'

Dim Comanda As New SqlCommand("SELECT * FROM " + strXsDet, Cn)

Dim Adaptador As New SqlDataAdapter

Adaptador.SelectCommand = Comanda

Adaptador.Fill(Detalle_Set, strXsDet)

Dim det As New ReportDataSource

det.Name = "Traspasos_Detalle"

det.Value = Detalle_Set.Tables(strXsDet)

LocalReport.datasources.add(det)

Cn.Close()

End Sub

Explanations :

StrXsd is the variable which holds the name of the table with the information for the ‘Encabs’ table

StrXdet has the infomation for the ‘Detalle’ table.

RV1 is the Instance of the Report Viewer Control. In the next lines of code, the program creates a New couple of Data Sets and

Somehow manages to set the replace the original datasources of the Report for the newly created. I´m very aware I don´t need to

Explain anything to you. Besides, I did almost identcally to what I saw. Someday I´ll analize it deeper.

I think the code :

Dim det As New ReportDataSource

det.Name = "Traspasos_Detalle" ‘ You can see these names in the xsd schema

det.Value = Detalle_Set.Tables(strXsDet)

LocalReport.datasources.add(det)

Is what really makes the miracle.


Submitted by Miguel Altamirano




1 Previous Next