This is a migrated thread and some comments may be shown as answers.

Passing parameters to programmatic report build to send via mail

3 Answers 101 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Tesis52
Top achievements
Rank 1
Tesis52 asked on 06 Jul 2017, 06:43 AM

Hello all reporting gurus.

I've been using Telerik reporting in Silverlight for 5 years, acctually with no serious issue.

Now we need to send a report via Mail w/o passing thru printing it to pdf and manually attaching it to a mail. So I've googled a little and found a way thru RepoerRender. The problem is that I'm NOT able to pass any parameter (all I get is a page with just fixed fields and DataSource is not used anytime (I have a breakpoint in report's rptPraticaPreventivo_NeedDataSource event, and it's never hit), so I guess I'm doing it the wrong way.

Can you pls help me in passing parameters the right way?

code I'm using is right after (both in VB and C#). Thank you in advance!

 

======================================VisualBasic=======================================================

    Public Sub SendByMail(nomeRpt As String, parList As String, codTesti As String, adresses As String, sender As String, subj As String, body As String, mailPwd As String)

        Dim rptSrc As New InstanceReportSource()
        Dim exRpt = Assembly.GetExecutingAssembly
        Dim tcw As Type = Type.GetType("UGO_Rpt." + nomeRpt)
        Dim rp As New rptPraticaPreventivo
        Dim pars() As String = parList.Split(";")
        For Each par In pars
            Dim idVal() = par.Split("=")
            Dim id As String = idVal(0)
            Dim val As String = idVal(1)
            rptSrc.Parameters.Add(id, val)
        Next
        rptSrc.ReportDocument = rp
        Dim rpt = TryCast(rp, Telerik.Reporting.Report)

        Dim reportProcessor As New ReportProcessor()
        Dim result As RenderingResult = reportProcessor.RenderReport("PDF", rpt, Nothing)

        Dim ms As New MemoryStream(result.DocumentBytes)
        ms.Position = 0

        Dim attachment As New Attachment(ms, rpt.Name + ".pdf")
        Dim msg As New MailMessage(sender, adresses, subj, body)
        msg.ReplyToList.Add(sender)
        msg.Attachments.Add(attachment)
        Dim SmtpServer As New System.Net.Mail.SmtpClient()
        Dim netCred As New NetworkCredential
        netCred.UserName = "security@mydomain.com"
        netCred.Password = mailPwd
        SmtpServer.Credentials = netCred
        SmtpServer.Port = 587 'Porta standard SMTP/TSL
        SmtpServer.Host = "name.server.com"
        SmtpServer.SendMailAsync(msg)
    End Sub

========================================C#===========================================================

public void SendByMail(string nomeRpt, string parList, string codTesti, string adresses, string sender, string subj, string body, string mailPwd)
{
InstanceReportSource rptSrc = new InstanceReportSource();
dynamic exRpt = Assembly.GetExecutingAssembly;
Type tcw = Type.GetType("UGO_Rpt." + nomeRpt);
rptPraticaPreventivo rp = new rptPraticaPreventivo();
string[] pars = parList.Split(";");
foreach (void par_loopVariable in pars) {
par = par_loopVariable;
[] idVal = par.Split("=");
string id = idVal(0);
string val = idVal(1);
rptSrc.Parameters.Add(id, val);
}
rptSrc.ReportDocument = rp;
dynamic rpt = rp as Telerik.Reporting.Report;

ReportProcessor reportProcessor = new ReportProcessor();
RenderingResult result = reportProcessor.RenderReport("PDF", rpt, null);

MemoryStream ms = new MemoryStream(result.DocumentBytes);
ms.Position = 0;

Attachment attachment = new Attachment(ms, rpt.Name + ".pdf");
MailMessage msg = new MailMessage(sender, adresses, subj, body);
msg.ReplyToList.Add(sender);
msg.Attachments.Add(attachment);
System.Net.Mail.SmtpClient SmtpServer = new System.Net.Mail.SmtpClient();
NetworkCredential netCred = new NetworkCredential();
netCred.UserName = "security@mydomain.com";
netCred.Password = mailPwd;
SmtpServer.Credentials = netCred;
SmtpServer.Port = 587;
//Porta standard SMTP/TSL
SmtpServer.Host = "name.server.com";
SmtpServer.SendMailAsync(msg);
}

3 Answers, 1 is accepted

Sort by
0
Tesis52
Top achievements
Rank 1
answered on 06 Jul 2017, 07:11 AM

Ok, I guess I found the way.

Instead of applying parameters to ReportSource (actually what is the meaning of the ReportSource parameter object?) I've to set report ReportParameter's value.

Another issue arose (it seems that having subreports something goes wrong, as rendering claims that "cambio" object is not part of the environment and report preview works just fine, but this will be the subject of another post. Any comment welcome

0
Katia
Telerik team
answered on 06 Jul 2017, 12:47 PM
Hello Tesis52,

ReportSource.Parameters collection is mapped by key to report parameters and it is used to update the values of report parameters when setting the ReportSource for the report viewers or ReportProcessor.

The reason ReportSource.Parameters did not update the report parameters was because ReportSource object was not actually passed to ReportProcessor. In the provided code, report instance is passed to ReportProcessor, this approach is obsolete since Q2 2012 - How to migrate your project to utilize the new ReportSource objects.

The correct approach would be to pass ReportSource object to ReportProcessor:
Dim rptSrc As New InstanceReportSource()  
Dim rp As New rptPraticaPreventivo
rptSrc.ReportDocument = rp
Dim result As RenderingResult = reportProcessor.RenderReport("PDF", rptSrc, Nothing)
InstanceReportSource rptSrc = new InstanceReportSource();
rptPraticaPreventivo rp = new rptPraticaPreventivo();
rptSrc.ReportDocument = rp;
RenderingResult result = reportProcessor.RenderReport("PDF", rptSrc, null);

I am not certain about the second issue, would it be possible to attach a screen shot of the error message you receive? 


Regards,
Katia
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Saverio
Top achievements
Rank 1
answered on 06 Jul 2017, 02:15 PM

Thank you so much, Katia.

I got your point about parameters.

Just 2 words to explain the second issue. In the report I'm talking about there's a SqlSource embedded in the report and at the same time in the resx file. The 1st one is outdated and miss a DB fiels, namely cambio, referred in the report. For some reason, the report service when called by a Silverlight app use the resx version, while if called by the wcf service used to send the report via mail, the embedded (outdated) version. I synced the two versions and now it runs flawlesly.

Thank you anyway for your ready reply. Have a nice day.

Saverio

Tags
General Discussions
Asked by
Tesis52
Top achievements
Rank 1
Answers by
Tesis52
Top achievements
Rank 1
Katia
Telerik team
Saverio
Top achievements
Rank 1
Share this question
or