0
votes

I'm a bit dumbfounded here. Most of the tutorials I've seen around create a report using the wizard or a built-in (or ready-made) dataset. I build my dataset using queries in runtime so I'm not too sure how to adopt the ones I've seen so far.

Usually, when I create a report, it's a one record report and fields in the RDLC file can easily be populated using parameters. The one I'm trying to do below however works similarly to making a SELECT query. So here's what I'm trying to do, I'm trying to make a report that shows up like so:

Status: Approved
PID  |  Name   | Address
1    |  Name 1 | Address 1
2    |  Name 2 | Address 2
===========================
Status: Denied
PID  |  Name   | Address
3    |  Name 3 | Address 3
4    |  Name 4 | Address 4

I have several questions with this:

  1. My RDLC reports are stored in a "\Reports" folder inside the folder with the .vb/.resx files. When I refer to it's using Application.StartupPath & "\Reports\myReport.rdlc", it can't find it there (obviously). Is there a way for me to embed the report files to the program (ie: use a relative folder name, where should I place the RDLC folder/file)? I don't think I should transfer the RDLC files to the Debug folder just to make this work (hence the full directory listing).

  2. What's a good approach to take in trying to create a report using the code above? I was thinking that if I were to loop along my dataset and pass the values from there to the RDLC file as parameters, it would populate the report (as with my one-record report before) but that doesn't seem to be the case.

  3. Refreshing the report viewer also gives me

    A data source instance has not been supplied for the data source 'Dataset1'.

    I created a dummy dataset in the RDLC file just to be able to use a Tablix as was suggested in several threads I read around. I'd really prefer if I could just use the dataset I made from scratch rather than use the wizard for that.

    Private Sub btnGenerateReport_Click(sender As Object, e As EventArgs) Handles btnGenerateReport.Click
        Dim query As String
    
        query = BuildQuery()
        SQLControl = New SQLControl
    
        Try
            If Not query = String.Empty Then
                SQLControl.QueryParams(query)
                If SQLControl.SQLDS.Tables(0).Rows.Count > 0 Then
                    FetchData()
    
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Report Maker")
        End Try
    End Sub
    
    Private Sub FetchData()
        Dim dataSource As ReportDataSource
        Dim PID As Integer
        Dim Name As String
        Dim ApplicationStatus As String
        Dim Address As String
    
        rvPreview.Reset()
        rvPreview.LocalReport.ReportPath = "C:\Users\xxx\Documents\Visual Studio 2015\Projects\My Project 1\My Project 1\Reports\myReport.rdlc"
        rvPreview.LocalReport.DataSources.Clear()
        dataSource = New ReportDataSource()
        _rparams = New List(Of ReportParameter)
    
        With SQLControl.SQLDS.Tables(0)
            For x As Integer = 0 To .Rows.Count - 1
                PID = .Rows(x).Item("PID")
                Name = .Rows(x).Item("LName") & ", " & .Rows(x).Item("FName")
                ApplicationStatus = .Rows(x).Item("ApplicationStatus")
                Address = .Rows(x).Item("StreetAddress") & ", " & .Rows(x).Item("City")
    
                _rparams.Add(New ReportParameter("PID", PID))
                _rparams.Add(New ReportParameter("Name", Name))
                _rparams.Add(New ReportParameter("ApplicationStatus", ApplicationStatus))
                _rparams.Add(New ReportParameter("Address", Address))
    
                For Each param As ReportParameter In _rparams
                    rvPreview.LocalReport.SetParameters(_rparams)
                Next
            Next x
    
            rvPreview.RefreshReport()
        End With
    End Sub
    
1

1 Answers

0
votes

At the begining I have to mention that I use RDLC reports with C#, but the solution should be very similar in vb.net.

  1. I usualy prepare every report as separate project (so I have separate DLL for every report) and use a separate class in this project to handle everything I need to do with report. I add Every RDLC file just under the project (main report file and subreports files). In my case I can read every RDLC file like a stream just using project namespace and RDLC filename as in this sample code below

    Assembly assembly = Assembly.GetExecutingAssembly();
    Stream stream = assembly.GetManifestResourceStream("[Some project namespace].[Some report filename].rdlc"); 
    

    Then I use a code like this to connect that stream with report viewer (the stream in this code is the same object as above but this code is in another class which has access to report viewer object)

    ReportViewerControl.LocalReport.LoadReportDefinition(new StreamReader(stream));
    
  2. You can provide a DataSet for your report in very simple way. You can use object of the System.Data.DataTable class for table (I call this object as yourTableObject in code below) and System.Data.DataRow class for rows in that table. When you put all your data in the table then you can provide it to the report using code like this

    //I use DataSet1 as dataset name because you use this name in your report definition
    ReportDataSource rds = new ReportDataSource("DataSet1", yourTableObject);
    ReportViewerControl.LocalReport.DataSources.Add(rds);
    

    Ofcourse provided table should have the same fields like the dataset in your report definition.

  3. You have to provide a valid DataSet to the report. Solution is above in point 2.