Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
2.2k views
in .NET FTP by (380 points)
When users put in their user information I want to be able to verify that it is correct by checking to see if they are connected. If they are not connected I want it to open a message box and skip to the end of the routine. If it is connected I want it open another message box confirming it and continue to download the file. If for some reason the file does not exist or you lose connection it want it to open another message box that indicates that. When I use the code below and the user information is incorrect, it goes to the last exception and does not open the message box pertaining to the incorrect user information. Can someone please help!!!!

P.S. I am using the free version.
Try
            FtpConnection1.ServerAddress = (M_Server.Text)
            FtpConnection1.UserName = (M_UserName.Text)
            FtpConnection1.Password = (M_Password.Text)
            FtpConnection1.Connect()
            If FtpConnection1.IsConnected = False Then
                MsgBox("Please check your user name and password.", MsgBoxStyle.Critical, "Connection Error!")
                GoTo notconnected
            Else : MsgBox("You are connected.", MsgBoxStyle.Critical, "Connected")
            End If
            FtpConnection1.ChangeWorkingDirectory("/var/tmp")
            Dim file_1 As String = M_UserName.Text & ".txt"
            Dim path As String = "C:\test\"
            FtpConnection1.DownloadFile(path + file_1, file_1)
            FtpConnection1.Close()
        Catch ex As Exception
            MsgBox("Please check your network connection and make" & vbNewLine & "sure you have saved your file to the L1 printer.", MsgBoxStyle.Critical, "Connection/File Error!")
        End Try
notconnected:
    End Sub

1 Answer

0 votes
by (51.6k points)
This is really a language issue. Exceptions are caught by the nearest (relevant) catch statement. In this case, an exception is thrown in FtpConnectiopn1.Connect() if it cannot connect and this exception is caught by the catch statement. What you need to do is to put a try-catch block around each statement whose error you want to distinguish from others.

Something like this:
Try
  FtpConnection1.ServerAddress = (M_Server.Text)
  FtpConnection1.UserName = (M_UserName.Text)
  FtpConnection1.Password = (M_Password.Text)
  Try
    FtpConnection1.Connect()
  Catch ex As Exception
    MsgBox("Please check your user name and password.", MsgBoxStyle.Critical, "Connection Error!")
    GoTo notconnected
  End Try
  MsgBox("You are connected.", MsgBoxStyle.Critical, "Connected")
  Try
    FtpConnection1.ChangeWorkingDirectory("/var/tmp")
    Dim file_1 As String = M_UserName.Text & ".txt"
    Dim path As String = "C:\test\"
    FtpConnection1.DownloadFile(path + file_1, file_1)
    FtpConnection1.Close()
  Catch ex As Exception
    MsgBox("Please check your network connection and make" & vbNewLine & "sure you have saved your file to the L1 printer.", MsgBoxStyle.Critical, "Connection/File Error!")
  End Try

notconnected:
    End Sub


- Hans (EDT)

Categories

...