Link to home
Create AccountLog in
Avatar of WINN2012
WINN2012Flag for United States of America

asked on

Vb 2010 .Net Report

Does anyone have an example of a report using Report Viewer that includes 1 or more sub-reports?

I have been all over the web and can't seem to find one that explains in detail how to implement with VB 2010. .Net.

Tons of examples using Crystal Reports with 2010 .Net

Any help would be appreciated.

Thanks
MW
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Avatar of WINN2012

ASKER

That worked perfect. My problem now is I dragged textboxes onto my report. Report looks great until I want more than 1 record. I read online that you have to use the table to display more than 1 record?  What if I have a report that has fields all over it and the grid wont work?

Thanks
MW
As you read, if you want to show a list of records then you have to use details view.
I basically have a form I am recreating and need to print 1 out for each record.
MW
I basically have a form I am recreating and need to print 1 out for each record.
MW
I can live with printing 1 record at a time.
I have a main report with 3 sub reports, each using its own subreportprocessing handler but for some reason all 3 subreports display the data from the first sub report.
My main report only has 1 dataset listed. Each of the handlers have dataset1 in the reportdatasource code
Thanks
MW
So you need to specify separate datasets for each handler.
i am using the following:
With a handler for each sub report:

Private Sub IssueTrackerWeeklyRpt_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim Connection As New SqlClient.SqlConnectionStringBuilder
        Dim myConnection As SqlConnection

        Connection.DataSource = "WWWWWWW"
        Connection.InitialCatalog = "FES Issue Tracking"
        Connection.UserID = "user"
        Connection.Password = "password"
        Connection.ConnectTimeout = 5

        Try
            myConnection = New SqlConnection(Connection.ConnectionString)
            'you need to provide password for sql server
            myConnection.Open()

            With Me.ReportViewer1.LocalReport

                ' Report path
                .ReportPath = Application.StartupPath & "\Reports\IssueTrackerWeekly.rdlc"
                .DataSources.Clear()

            End With
            Dim SQL As String = ""
            ' ----------------------------------------------------
            ' Datasource for the main report Not Assigned and Not Closed
            ' ----------------------------------------------------
            SQL = "SELECT Issues.ID, Issues.Title, Issues.OpenedDate, Status.Status, Contacts.FullName AS AssignedTo," & _
                  " Contacts_1.FullName AS OpenedBy, Priority.Priority, Issues.AssignedTo AS AssignedToID" & _
                  " FROM Issues" & _
                  " INNER JOIN Status ON Issues.Status = Status.ID" & _
                  " INNER JOIN Contacts AS Contacts_1 ON Issues.OpenedBy = Contacts_1.ID" & _
                  " INNER JOIN Priority ON Issues.Priority = Priority.PriorityID" & _
                  " INNER JOIN Contacts ON Issues.AssignedTo = Contacts.ID" & _
                  " WHERE Contacts.FullName = 'Not Assigned' AND Status.Status <> 'Closed'" & _
                  " ORDER BY Issues.ID DESC"

            Using da As New SqlDataAdapter(SQL, myConnection)

                Using ds As New DataSet
                    da.Fill(ds, "issues")

                    ' You must use the same name as defined in the report
                    ' Data Source Definition
                    Dim rptDataSource As New ReportDataSource("DataSet1", ds.Tables("issues"))
                    Me.ReportViewer1.LocalReport.DataSources.Add(rptDataSource)
                    AddHandler ReportViewer1.LocalReport.SubreportProcessing, AddressOf IssueTrackerWeekly2
                    AddHandler ReportViewer1.LocalReport.SubreportProcessing, AddressOf IssueTrackerWeekly3
                    AddHandler ReportViewer1.LocalReport.SubreportProcessing, AddressOf IssueTrackerWeekly4
                End Using
            End Using

            ' Refresh the report
            ReportViewer1.RefreshReport()

        Catch ex As Exception
            MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        Me.ReportViewer1.RefreshReport()
    End Sub

    Sub IssueTrackerWeekly2(ByVal sender As Object, ByVal e As SubreportProcessingEventArgs)
        ' #### OPENED THIS WEEK ####
        Try

            Call findFirstLastDayOfWeek()

            Dim SQL As String = ""
            SQL = "SELECT Issues.ID, Issues.Title, Issues.OpenedDate, Status.Status, Contacts.FullName AS OpenedBy," & _
                  " Priority.Priority, Contacts_1.FullName AS AssignedTo" & _
                  " FROM Issues" & _
                  " INNER JOIN Status ON Issues.Status = Status.ID" & _
                  " INNER JOIN Contacts ON Issues.OpenedBy = Contacts.ID" & _
                  " INNER JOIN Priority ON Issues.Priority = Priority.PriorityID" & _
                  " INNER JOIN Contacts AS Contacts_1 ON Issues.AssignedTo = Contacts_1.ID" & _
                  " WHERE OpenedDate >= '" & weekStartDate & "' AND OpenedDate <= '" & weekEndDate & "'" & _
                  " ORDER BY Issues.ID DESC"

            Using da As New SqlDataAdapter(SQL, myConnection)
                Using ds As New DataSet
                    da.Fill(ds, "DataSet2")
                    Dim rptDataSource As New ReportDataSource("DataSet2", ds.Tables("Issues"))
                    e.DataSources.Add(rptDataSource)
                End Using
            End Using

        Catch ex As Exception
            MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub
I only see IssueTrackerWeekly2 in your code. Do you have different Where condition in code of IssueTrackerWeekly3 and IssueTrackerWeekly4?
Yes I have different handlers for 3 and 4. Just didnt cut and paste them.

MW
I just noticed that you are assigning different event handlers to same event.

Check example here

http://msdn.microsoft.com/en-us/library/microsoft.reporting.winforms.localreport.subreportprocessing(v=vs.80).aspx
Hmmm
I'm not quite following that code and how to implement it in my code.

MW
There is only one event handler for all subreports. You need to use e.Parameters collection in that event to determine what datasource you need to supply to the subreport which is currently being processed. I have not done it myself so not sure about specifics.
In the example you provided its the line:
Dataset.ReadXml ("C:\MyReports\OrderData.xml")
That has my confused.
To date I haven't done anything with xml.
I have been usinf sql statements , da and ds to populate my datasets.
MW
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Woot,

That makes sense, worked like a champ.

TYVM

MW