Last post Dec 21, 2016 05:17 PM by PatriceSc
Dec 21, 2016 04:38 PM|sn002|LINK
I have a ticketing system where I can send a confirmation email to recipients. If recipients with names that come with a special character like an apostrophe (e.g., O'Brien), it will throw errors below.
Incorrect syntax near 'Brien'. Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon. Database
'the' does not exist. Make sure that the name is entered correctly.
please advice. Thanks.
Dim strMailSQLCmd As String = "EXEC msdb.dbo.sp_send_dbmail " & _
"@profile_name = 'Ticket System', " & _
"@recipients = '" & strTo & "', " & _
"@blind_copy_recipients = 'Webmaster@xyz.com', " & _
"@body = '" & strBody & "', " & _
"@subject = '" & strSubj & "'"
Dim cmdSendMail As New SqlCommand(strMailSQLCmd, conn)
cmdSendMail.ExecuteNonQuery() // ***************Error Here *****************
AddLogEntry("Confirmation E-Mail Sent")
Dec 21, 2016 05:03 PM|codemovement.pk|LINK
while concatenating the name string do this replace ' with ''
Dec 21, 2016 05:17 PM|PatriceSc|LINK
Or use parameters : http://csharp-station.com/Tutorial/AdoDotNet/Lesson06 (in case you are doing it elsewhere already, you can use that as well here, and if not doing it all it is the best
practice as it avoid SQL injections and allows to pass native values without bothering about formating them properly...