unable to read a value of a variable

  • Thread starter Thread starter msdnpublic1234
  • Start date Start date
M

msdnpublic1234

Guest
Hi,

The requirement is to iterate through each element of list and generate corresponding ssrs report for the parameter specified and email to different distribution lists for each of the 3 iterations.This code is inside of script task in ssis.I am stuck at a point where it throws exception on the sendemail() because the variable DistrList is being read as a string instead of value.I want DistrList to refer anyone of RecipientsA/B/C depending on the prompt.key.I am stuck at the line var DistrList = String.Format("Recipients{0}",prompt.Key); which gives me string RecipientsA and is not a valid email parameter.The EmailRecipientsA is a variable of type string jdoe@abc.com. Pls help me fix this.

public void Main()
{

var directory = (string)Dts.Variables["$Package::Folder"].Value;

var RecipientsA = (string)Dts.Variables["$Package::EmailRecipientsA"].Value;

var RecipientsB = (string)Dts.Variables["$Package::EmailRecipientsB"].Value;
var RecipientsC = (string)Dts.Variables["$Package::EmailRecipientsC"].Value;

var smtpServer = (string)Dts.Variables["$Package::SmtpServer"].Value;
// Shows a List of KeyValuePairs.
var list = new List<KeyValuePair<string, int>>();
list.Add(new KeyValuePair<string, int>("A", 1));
list.Add(new KeyValuePair<string, int>("B", 2));
list.Add(new KeyValuePair<string, int>("C", 3));
foreach (var prompt in list)
{
var mhtmlFilename = String.Format(directory+"/Report"+prompt.Key+".html");
try
{
var parameters = new ParameterValue[2];
parameters[0] = new ParameterValue();
parameters[0].Name = "View";
parameters[0].Value = "Simple";

parameters[1] = new ParameterValue();
parameters[1].Name = "City";
parameters[1].Value = prompt.Key;

var success = RenderReport(parameters, "HTML", htmlFilename);
var html = DecodeMHTML(htmlFilename);

if (success)
{

var body = String.Format("{0}{1}", link, html);
var subject = "Newsletter" + prompt.Key;
var DistrList = String.Format("Recipients{0}",prompt.Key);

SendMail(smtpServer, DistrList.ToString(), body,subject);
Dts.TaskResult = (int)ScriptResults.Success;
}
else
{

Dts.TaskResult = (int)ScriptResults.Failure;
}

}
catch (Exception exp)
{
FireInformation("Exception: " + exp);
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
}

private bool RenderReport(ParameterValue[] parameters, string format, string filename)
{
//piece of code here that runs report execution service and renders report
}

private void SendMail(string smtpServer, string email, string body,string sub)
{
var message = new MailMessage();
var smtp = new SmtpClient(smtpServer);
var addresses = to.Split(';');
foreach (var address in addresses)
{
message.To.Add(address);
}

message.Subject = sub;
message.From = new MailAddress("abc@1234xyz.com");
message.IsBodyHtml = true;
message.Body = body;
smtp.Send(message);
}

Continue reading...
 
Back
Top