Unable to programmatically set the value for Microsoft.SqlServer.Dts.Tasks.Send MailTask.SendMailMessageSourceType
I am writing a VB script for Script Task that will find out if there is any bad/unrecognized data. If found, it will programmatically concatenate those values into in one string and put it on the e-mail content and send out the e-mail alert. Everything looks find in the code until I did the following line in the code... set .Properties( "MessageSourceType").SetValue(thSendMailTask, "Variable") Then, it gives me the error below: Object of type 'System.String' cannot be converted to type 'Microsoft.SqlServer.Dts.Tasks.SendMailTask.SendMailMessageSourceType'. at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)... UnrecognizedOrgNames is the variable that contains string value to be put in the e-mail content. I am basically trying to programmatically set MessageSourceType to "Variable" that takes the value from UnrecognizedOrgNames variable. There seems to be data type conflict, but I don’t know what to do with it. Here is the entire script per your review. What am I doing wrong here? Please help. Thank you! Imports System Imports System.Data Imports System.Math Imports System.Xml Imports System.Data.OleDb Imports Microsoft.SqlServer.Dts.Runtime Imports Microsoft.SqlServer.Dts.Tasks.SendMailTask Public Class ScriptMain Public Sub Main() Dim oleDA As New OleDb.OleDbDataAdapter Dim dt As New DataTable Dim col As DataColumn Dim row As DataRow Dim sMsg As String Dim sMsg2 As String Dim pkg As New Package Dim smtpCM As ConnectionManager smtpCM = pkg.Connections.Add("SMTP") smtpCM.Name = "AHSS email" smtpCM.ConnectionString = "smtp.ahss.org" Dim exe As Executable = pkg.Executables.Add("STOCK:SendMailTask") Dim thSendMailTask As TaskHost = CType(exe, TaskHost) oleDA.Fill(dt, Dts.Variables("UnrecognizedOrg").Value) sMsg = "" sMsg2 = "" For Each row In dt.Rows For Each col In dt.Columns sMsg = sMsg & col.ColumnName & _ row(col.Ordinal).ToString & vbCrLf Next sMsg2 = sMsg & ", " & sMsg2 Dts.Variables("UnrecognizedOrgNames").Value = sMsg2 sMsg = "" Next Dts.TaskResult = Dts.Results.Success With thSendMailTask .Properties("SmtpConnection").SetValue(thSendMailTask, "AHSS email") .Properties("ToLine").SetValue(thSendMailTask, "monthep.hongsyok@ahss.org") .Properties("FromLine").SetValue(thSendMailTask, "sqlservice@ahss.org") .Properties("Subject").SetValue(thSendMailTask, "Unrecognized hospital name(s) found in HPM file") .Properties("MessageSourceType").SetValue(thSendMailTask, "Variable") .Properties("MessageSource").SetValue(thSendMailTask, Dts.Variables("UnrecognizedOrgNames").Value) End With Dim valResults As DTSExecResult = pkg.Validate(pkg.Connections, pkg.Variables, Nothing, Nothing) If sMsg2 <> "" Then pkg.Execute() End If End Sub End Class
April 29th, 2011 3:34pm

The problem is that that property isn't a string. The error is telling you that the string "Variable" can't be converted to the type Microsoft.SqlServer.Dts.Tasks.SendMailTask.SendMailMessageSourceType, which is an enumeration. Enumerations happen to be integer-backed abstractions - each "value" is actually a number. The trick is to find out which number represents each value, which is fairly straightforward, since you're in code. Using this should work: .Properties("MessageSourceType").SetValue(thSendMailTask, Cint(Microsoft.SqlServer.Dts.Tasks.SendMailTask.SendMailMessageSourceType.Variable)) Talk to me now on
Free Windows Admin Tool Kit Click here and download it now
April 29th, 2011 5:02pm

Todd, Thank you very much for your advise! :) Truly,
May 16th, 2011 5:39pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics