Binding Data to the ReportViewer Control Dynamically in ASP.NET 2.0 - Binding a text file (with comma separated values or CSV) to a ReportViewer control in ASP.NET 2.0
(Page 4 of 6 )
In this section, I would like to deal with ASP.NET 2.0 reporting using text files. The text files will contain comma separated values (csv) of data.
Add a text file to your project (called SalesData.csv) and add few rows based on the following figure.

Add a new web form (called TextBasedReport.aspx) to your project, drag a ReportViewer control from the toolbox and modify the code as follows:
ImportsMicrosoft.Reporting.WebForms
ImportsSystem.Data
ImportsSystem.Text
ImportsMicrosoft.VisualBasic.FileIO
PartialClass TextbasedReport
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
'Set the processing mode for the ReportViewer to Local
ReportViewer1.ProcessingMode = ProcessingMode.Local
Dim rep As LocalReport = ReportViewer1.LocalReport
rep.ReportPath = "SampleReport.rdlc"
Dim ds As DataSet = GetSalesData()
'Create a report data source for the sales order data
Dim dsSalesOrder As New ReportDataSource()
dsSalesOrder.Name = "SalesData_Data"
dsSalesOrder.Value = ds.Tables("Data")
rep.DataSources.Add(dsSalesOrder)
End Sub
Private Function GetSalesData()
Dim ds As New DataSet("SalesData")
Dim dt As New DataTable("Data")
dt.Columns.Add("SalesPersonID")
dt.Columns.Add("FirstName")
dt.Columns.Add("SalesQuota")
dt.Columns.Add("SalesYTD")
dt.Columns.Add("SalesLastYear")
Dim fileName As String =
HttpContext.Current.Request.MapPath("SalesData.csv")
Using parser As New TextFieldParser(fileName)
parser.Delimiters = New String() {","}
parser.HasFieldsEnclosedInQuotes = False
Dim fields As String()
fields = parser.ReadFields()
Do While fields IsNot Nothing
Dim dr As DataRow = dt.NewRow
dr("SalesPersonID") = fields(0)
dr("FirstName") = fields(1)
dr("SalesQuota") = fields(2)
dr("SalesYTD") = fields(3)
dr("SalesLastYear") = fields(4)
dt.Rows.Add(dr)
fields = parser.ReadFields()
Loop
End Using
ds.Tables.Add(dt)
Return ds
End Function
EndClass
Next: Binding a customized ObjectDataSource (or business logic) to a ReportViewer control in ASP.NET 2.0 >>
More ASP.NET Articles
More By Jagadish Chaterjee