Site Search:
Sign in | Join | Help

This Blog

Syndication

VB.NET

Notes, Tricks and Tips on VB.NET

February 2009 - Posts

  • Code to keep a form from opening twice in a VB.NET WinForms application

    Code to keep a form from opening twice in a VB.NET WinForms application

     Sub LaunchPrintPickingTickets()
        If frmPrintPickingTickets Is Nothing Then
            Try
                frmPrintPickingTickets = New PrintPickingTickets()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        Else
            If frmPrintPickingTickets.Created = False Then
                frmPrintPickingTickets = New PrintPickingTickets
            End If
        End If
    
    
        ' Always show and activate the WinForm
        frmPrintPickingTickets.Show()
        frmPrintPickingTickets.Activate()
    End Sub

  • Error handling code

    This module is used in quite a few places to handle errors

    Sub globalErrorHandler(ByVal ex As Exception)
        'log the error
        Dim strMsg As String = ""
        strMsg += "Exception: " + vbCrLf
        strMsg += ex.Message + vbCrLf
    
    
        ''display the error
        'If strMsg.Contains("Illegal address") Then
        '    Exit Sub
        'End If
    
    
        strMsg += "GetType: " & ex.GetType().FullName + vbCrLf
        strMsg += "Source: " & ex.Source + vbCrLf
        strMsg += "StackTrace: " & ex.StackTrace + vbCrLf
        strMsg += "TargetSite: " + ex.TargetSite.ToString & vbCrLf
    
    
        If Not ex.InnerException Is Nothing Then
            strMsg += "Inner Exception: " & vbCrLf
            strMsg += ex.InnerException.Message + vbCrLf
            strMsg += "GetType: " & ex.InnerException.GetType().FullName + vbCrLf
            strMsg += "Source: " & ex.InnerException.Source + vbCrLf
            strMsg += "StackTrace: " & ex.InnerException.StackTrace + vbCrLf
            strMsg += "TargetSite: " + ex.InnerException.TargetSite.ToString & vbCrLf
        End If
    
    
        strMsg += appUser.UserID
    
    
        'mail the error
        sendMail(strMsg)
    
    
        globalMessageBox(strMsg)
    End Sub
     
    
    
     
    
    
        Sub globalMessageBox(ByVal strMessage As String)
            MsgBox(strMessage, , "Dynamics GP")
        End Sub
        Public Sub sendMail(ByVal strBody As String)
    
    
            Try
                Dim strEmailServer As String = System.Configuration.ConfigurationManager.AppSettings("emailServer")
                Dim strEmailUser As String = System.Configuration.ConfigurationManager.AppSettings("emailUser")
                'Dim strEmailPassword As String = System.Configuration.ConfigurationManager.AppSettings("emailPassword")
                Dim strEmailRecipient As String = System.Configuration.ConfigurationManager.AppSettings("emailRecipient")
    
    
                Dim mail As New System.Net.Mail.MailMessage(strEmailUser, strEmailRecipient)
                mail.Subject = "NGB Dynamics VS Tools Error"
                mail.Body = strBody
    
    
                Dim serv As New System.Net.Mail.SmtpClient(strEmailServer)
                'serv.Credentials = New System.Net.NetworkCredential(strEmailUser, strEmailPassword)
    
    
                serv.Send(mail)
    
    
            Catch ex As Exception
                globalMessageBox("Failed to send Admin Email")
                'no error handling. we're already in an error loop. If this fails, we're just out of luck
            End Try
    
    
        End Sub
    
    
    
     

    You'll need the email settings in the app.config

     <appSettings>
     <add key="emailServer" value="mail.domain.com" />
     <add key="emailUser" value="emailuser" />
     <add key="emailRecipient" value="emailrecipient" />
     <add key="emailPassword" value="abcd1234" />
    </appSettings>
    
    
    
     

  • The requested database xyz is not defined in configuration

    I have an application that is designed as a class library (a .dll) that is called by a WinForms app. The class library is trying to do data access using the Microsoft Data Access Application Block (which I really love, BTW).

    Anyway, I was getting 'The requested database xyz is not defined in configuration' when I attempted data access. Turns out I had to copy the '<connectionStrings>' section from the app.config file from my class library to the WinForms application .config file. Then, it was able to find the needed connection string.

    Weird.

     

More Posts Next page »