First, load the needed dlls:
In Visual Studio, click on TOOLS > NUGET PACKAGE MANAGER > PACKAGE MANAGER CONSOLE
Then type this in the window:
Install-Package Microsoft.ReportingServices.ReportViewerControl.Winforms
This will install the dlls, and you'll get a 'SQL Server' section in the toolbox.
The code below is all you'll need:
Private Sub GenerateInvoice(InvoiceNum As String)
Try
Dim rv As New Microsoft.Reporting.WinForms.ReportViewer
'the report name is 'test', it is in the root folder.
'if it were in the 'TWO' folder, it would be '/TWO/Test'
rv.ServerReport.ReportPath = "/Test"
rv.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Remote
' Pass a parameter to the report
Dim myParam As New ReportParameter("InvoiceNum", InvoiceNum)
rv.ServerReport.SetParameters(myParam)
rv.RefreshReport()
'write the report to disk.
'this will write to the application directory... which I would not do in production
Dim content() As Byte
content = rv.ServerReport.Render("pdf")
'use filestream to save byte array to a file
Dim o_fs As New IO.FileStream(InvoiceNum & ".pdf", IO.FileMode.Create)
o_fs.Write(content, 0, content.Length)
o_fs.Close()
o_fs = Nothing
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub