Here is some VB code that is part of a major system of apps that I have been asked to maintain,  one of the more fun things that I get to do is rewrite some code here and there,  I have done a little bit of that here, I finally have it functioning properly and would like to see if I am still on an upward stride in my VB learning.Module Module1    Private Function SQL_Connection_String() As String        Dim Connection As String#If DEBUG Then        Connection = {TestConnectionString}#ElseIf CONFIG = Release Then        Connection = {ProductionConnectionString}#End If        Return Connection    End Function    Private Function test_Email() As String        Return {TestersEmail}    End Function    Private sLetterText As String    Sub Main()        Dim sSource As String        Dim sLog As String        Dim sEvent As String        Dim sMachine As String        sSource = OLMJSubscriberNotification        sLog = Application        sEvent = Error        sMachine = .        If Not EventLog.SourceExists(sSource) Then            EventLog.CreateEventSource(sSource, sLog)        End If        Dim ErrorLog As New EventLog(sLog, sMachine, sSource)        Dim SentLog As New EventLog(sLog, sMachine, sSource)        ReadSQL(SentLog, ErrorLog)    End Sub    Public Sub ReadSQL(ByRef ErrorLog As EventLog, ByRef SentLog As EventLog)        Dim sText As New String()        Dim sRecip As New String()        Dim mailSent As Integer        Dim dr As SqlDataReader        Dim sRecipList As New String()        Dim cnn As New SqlConnection(SQL_Connection_String())        Const SQLstr = SELECT UserName, SubscriptionEnd, Email,UserID, AgencyName FROM Users  & _            WHERE(DateDiff(Day, GETDATE(), SubscriptionEnd) <= 10)  & _            And DateDiff(Day, GETDATE(), SubscriptionEnd) >= 0  & _            AND (DATEDIFF(day,GETDATE(), NotificationSent) < -10 OR NotificationSent IS NULL)        Dim objCommand As New SqlCommand(SQLstr, cnn)        Try            ReadLetterText(ErrorLog)            cnn.Open()            dr = objCommand.ExecuteReader            While dr.Read                sRecip = dr(2).ToString                sRecipList = sRecipList & sRecip &  -  & dr(4).ToString & vbCrLf                SendMail(sLetterText, sRecip, ErrorLog, True)                UpdateNoticeSent(dr(3), ErrorLog)                mailSent += 1            End While            If mailSent > 0 Then                sRecipList = The following  & mailSent.ToString &  subscribers received expiration mail  & Now.ToString & : & vbCrLf & vbCrLf & sRecipList#If DEBUG Then                SendMail(sRecipList, test_Email, ErrorLog, False)#Else                SendMail(sRecipList, {ErrorContactEmails} , ErrorLog, False)#End If            End If            SentLog.WriteEntry(Notices Sent:  & mailSent.ToString &    & Now.ToString)        Catch err As System.Exception            ErrorLog.WriteEntry(System.Reflection.MethodBase.GetCurrentMethod().Name().ToString &   & err.ToString)#If DEBUG Then            SendErrorMail(System.Reflection.MethodBase.GetCurrentMethod().Name().ToString &   & err.ToString, test_Email, ErrorLog)#Else            SendErrorMail(System.Reflection.MethodBase.GetCurrentMethod().Name().ToString &   & err.ToString, helpdesk@Address.com, ErrorLog)#End If        Finally            objCommand.Dispose()            cnn.Dispose()        End Try    End Sub    Private Sub SendErrorMail(ByVal sText As String, ByVal sRecip As String, ByRef ErrorLog As EventLog)        Try            Dim Mailmsg As New Mail.MailMessage            Dim obj As New Mail.SmtpClient({SMTP.Client.Fake})            obj.Port = 25#If DEBUG Then            Mailmsg.To.Add(test_Email)#Else            Mailmsg.To.Add(New MailAddress(Developer@Address.com))            Mailmsg.CC.Add(ProjectManager@Address.com)            Mailmsg.To.Add(sRecip)#End If            Mailmsg.Subject = OLMJ Subscriber Notification Error            Mailmsg.From = New MailAddress(webdev@Address.com, UJS Online Money Judgment)            Mailmsg.Priority = MailPriority.High            Mailmsg.Body = sText            Mailmsg.IsBodyHtml = False            obj.Send(Mailmsg)        Catch err As System.Exception            ErrorLog.WriteEntry(System.Reflection.MethodBase.GetCurrentMethod().Name().ToString &   & err.ToString)        End Try    End Sub    Private Sub SendMail(ByVal sText As String, ByVal sRecip As String, ByRef ErrorLog As EventLog, ByVal sendForm As Boolean)        Try            Dim Mailmsg As New Mail.MailMessage            Dim obj As New Mail.SmtpClient(SMTP.Client.fake)            obj.Port = 25            Mailmsg.To.Add(sRecip)            Mailmsg.From = New MailAddress(webdev@Address.com, UJS Online Money Judgment)            Mailmsg.Subject = Expiration Notice            Mailmsg.Body = sText            Mailmsg.Priority = MailPriority.Normal            Mailmsg.IsBodyHtml = True            If sendForm Then                Dim subAgreeForm As New Attachment(Subscriber_Agreement_And_Request_Form.pdf)                Mailmsg.Attachments.Add(subAgreeForm)            End If            obj.Send(Mailmsg)        Catch err As System.Exception            ErrorLog.WriteEntry(System.Reflection.MethodBase.GetCurrentMethod().Name().ToString &   & err.ToString)#If DEBUG Then            SendErrorMail(System.Reflection.MethodBase.GetCurrentMethod().Name().ToString &   & err.ToString, test_Email, ErrorLog)#Else            SendErrorMail(System.Reflection.MethodBase.GetCurrentMethod().Name().ToString &   & err.ToString, helpdesk@Address.com, ErrorLog)#End If        End Try    End Sub    Private Sub UpdateNoticeSent(ByVal userID As Integer, ByRef ErrorLog As EventLog)        Dim rowsAffected As Integer        Const SQLstr = _            UPDATE Users SET NotificationSent = @NotiSent where UserID = @UserID        Using UpdateConn As New SqlConnection(SQL_Connection_String)            Using objCommand As New SqlCommand(SQLstr, UpdateConn)                Try                    objCommand.CommandType = CommandType.Text                    objCommand.Parameters.Add(@UserID, SqlDbType.Int)                    objCommand.Parameters(@UserID).Value = userID                    objCommand.Parameters.Add(@NotiSent, SqlDbType.DateTime)                    objCommand.Parameters(@NotiSent).Value = Now                    UpdateConn.Open()                    rowsAffected = objCommand.ExecuteNonQuery()                Catch err As System.Exception                    ErrorLog.WriteEntry(System.Reflection.MethodBase.GetCurrentMethod().Name().ToString &   & err.ToString)#If DEBUG Then                    SendErrorMail(System.Reflection.MethodBase.GetCurrentMethod().Name().ToString &   & err.ToString, test_Email, ErrorLog)#Else            SendErrorMail(System.Reflection.MethodBase.GetCurrentMethod().Name().ToString &   & err.ToString, helpdesk@Address.com, ErrorLog)#End If                Finally                    objCommand.Dispose()                    UpdateConn.Close()                End Try            End Using        End Using    End Sub    Private Sub ReadLetterText(ByRef ErrorLog As EventLog)        Dim sFilePath As String        Dim sTempLine As String        Dim sLetterBody As New String()        Try            sFilePath = System.AppDomain.CurrentDomain.BaseDirectory()            sFilePath = sFilePath & SubExp.txt            Dim myStreamReader As New IO.StreamReader(sFilePath)            Do Until myStreamReader.Peek() = -1                sTempLine = myStreamReader.ReadLine()                sLetterBody = sLetterBody & vbCrLf & sTempLine            Loop            sLetterText = sLetterBody        Catch err As System.Exception            ErrorLog.WriteEntry(System.Reflection.MethodBase.GetCurrentMethod().Name().ToString &   & err.ToString)            SendErrorMail(System.Reflection.MethodBase.GetCurrentMethod().Name().ToString &   & err.ToString, helpdesk@Address.com, ErrorLog)        End Try    End SubEnd Modulelet me know what you think.
Notify user of upcoming expiration of subscription
vb.net;email
This query uses DATEDIFF() inefficiently:Const SQLstr = SELECT UserName, SubscriptionEnd, Email,UserID, AgencyName FROM Users  & _    WHERE(DateDiff(Day, GETDATE(), SubscriptionEnd) <= 10)  & _    And DateDiff(Day, GETDATE(), SubscriptionEnd) >= 0  & _    AND (DATEDIFF(day,GETDATE(), NotificationSent) < -10 OR NotificationSent IS NULL)To accomplish that, the server would have to run DATEDIFF() on every single row in the Users table. Instead, you should calculate the cutoff dates just once:SELECT UserName, SubscriptionEnd, Email,UserID, AgencyName    FROM Users    WHERE        SubscriptionEnd <= CAST(DATEADD(Day, 10, GETDATE()) AS DATE)        AND SubscriptionEnd >= CAST(GETDATE() AS DATE)        AND (NotificationSent IS NULL             OR NotificationSent < CAST(DATEADD(Day, -10, GETDATE()) AS DATE));(The casts truncate the results of DATEADD() to midnight.)