locked
Why is this code not functioning properly - Is something missing? RRS feed

  • Question

  • Hope someone can help with this...

     

    this is working but not quite as it should...

     

    this code basically connects to the FTP server and copies the file to the local drive, this works fine...

     

    The problem is if the URL username and password fields are empty you get a pop up message saying that the field cant be empty, however what is happening is after the first error saying no url entered it goes straight to line that starts :

    INet = InternetOpen

     

    I always thought that code is executed line by line, whihc means the way it is is it should go through each txt field making sure there is something in them and if not sets the focus back to the field until there is text in the field, when all fields have text in and all is ok, then it should go to the connection process..

     

     

    Code Snippet

    Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal HINet As Integer) As Integer

    Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Integer, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Integer) As Integer

    Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession As Integer, ByVal sServerName As String, ByVal nServerPort As Integer, ByVal sUsername As String, ByVal sPassword As String, ByVal lService As Integer, ByVal lFlags As Integer, ByVal lContext As Integer) As Integer

    Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (ByVal hFtpSession As Integer, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, ByVal dwFlagsAndAttributes As Integer, ByVal dwFlags As Integer, ByVal dwContext As Integer) As Boolean

    Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hFtpSession As Integer, ByVal lpszLocalFile As String, ByVal lpszRemoteFile As String, ByVal dwFlags As Integer, ByVal dwContext As Integer) As Boolean

     

     

    Code Snippet

    Private Sub btnConnectToFTP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnectToFTP.Click

    Dim msg As String = ""

    Dim INet As Integer

    Dim INetConn As Integer

    Dim RC As Boolean

    If txtFTP_URL.Text.Trim() = "" Then

    msg &= "No URL Entered"

    'MessageBox.Show("No URL Entered", "No URL Entered")

    txtFTP_URL.Focus()

    ElseIf txtFTP_UserName.Text.Trim() = "" Then

    msg &= "No User Name Entered"

    ' MessageBox.Show("No User Name Entered", "No User Name Entered")

    txtFTP_UserName.Focus()

    ElseIf txtFTP_Password.Text.Trim() = "" Then

    msg &= "No Password Entered"

    ' MessageBox.Show("No Password Entered", "No Password Entered")

    txtFTP_Password.Focus()

    End If

    If msg.Length <> 0 Then

    MessageBox.Show(msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

    End If

     

     

    INet = InternetOpen("MyFTP Control", 1, vbNullString, vbNullString, 0)

    INetConn = InternetConnect(INet, txtFTP_URL.Text, 0, txtFTP_UserName.Text, txtFTP_Password.Text, 1, 0, 0)

    RC = FtpGetFile(INetConn, "/test/test1.txt", "c:\FTP\test\test1.txt", True, 1, 0, 0)

     

    If RC Then MessageBox.Show("Transfer succesfull!") Else MessageBox.Show("Transfer Failed", "There was an error in connection")

    InternetCloseHandle(INetConn)

    InternetCloseHandle(INet)

     

    End Sub

     

     

    Sunday, August 12, 2007 6:59 PM

Answers

  • As soon as you leave the if statement it will proceed to the next line outside of it

     

    in this case you can put the last code into an else part of the if statement

    the idea would be to put the end code after the else so that it will proceed with that code if it meets your requirement

     

    Try this

     

    Private Sub btnConnectToFTP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnectToFTP.Click

    Dim msg As String = ""

    Dim INet As Integer

    Dim INetConn As Integer

    Dim RC As Boolean

    If txtFTP_URL.Text.Trim() = "" Then

    msg &= "No URL Entered"

    'MessageBox.Show("No URL Entered", "No URL Entered")

    txtFTP_URL.Focus()

    ElseIf txtFTP_UserName.Text.Trim() = "" Then

    msg &= "No User Name Entered"

    ' MessageBox.Show("No User Name Entered", "No User Name Entered")

    txtFTP_UserName.Focus()

    ElseIf txtFTP_Password.Text.Trim() = "" Then

    msg &= "No Password Entered"

    ' MessageBox.Show("No Password Entered", "No Password Entered")

    txtFTP_Password.Focus()

    End If

     

     

    If msg.Length <> 0 Then

    MessageBox.Show(msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

     

    Else      --------------------------------------------   

     

    INet = InternetOpen("MyFTP Control", 1, vbNullString, vbNullString, 0)

    INetConn = InternetConnect(INet, txtFTP_URL.Text, 0, txtFTP_UserName.Text, txtFTP_Password.Text, 1, 0, 0)

    RC = FtpGetFile(INetConn, "/test/test1.txt", "c:\FTP\test\test1.txt", True, 1, 0, 0)

     

    If RC Then MessageBox.Show("Transfer succesfull!") Else MessageBox.Show("Transfer Failed", "There was an error in connection")

    InternetCloseHandle(INetConn)

    InternetCloseHandle(INet)

     

    End If

     

    End Sub

     

     

    you will have to adjust it as you need it

    i don't know exactly how you want it to flow

    or if you want to stop after an error then you can use      exit sub()    after the messagebox

    Sunday, August 12, 2007 10:13 PM

All replies

  • As soon as you leave the if statement it will proceed to the next line outside of it

     

    in this case you can put the last code into an else part of the if statement

    the idea would be to put the end code after the else so that it will proceed with that code if it meets your requirement

     

    Try this

     

    Private Sub btnConnectToFTP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnectToFTP.Click

    Dim msg As String = ""

    Dim INet As Integer

    Dim INetConn As Integer

    Dim RC As Boolean

    If txtFTP_URL.Text.Trim() = "" Then

    msg &= "No URL Entered"

    'MessageBox.Show("No URL Entered", "No URL Entered")

    txtFTP_URL.Focus()

    ElseIf txtFTP_UserName.Text.Trim() = "" Then

    msg &= "No User Name Entered"

    ' MessageBox.Show("No User Name Entered", "No User Name Entered")

    txtFTP_UserName.Focus()

    ElseIf txtFTP_Password.Text.Trim() = "" Then

    msg &= "No Password Entered"

    ' MessageBox.Show("No Password Entered", "No Password Entered")

    txtFTP_Password.Focus()

    End If

     

     

    If msg.Length <> 0 Then

    MessageBox.Show(msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

     

    Else      --------------------------------------------   

     

    INet = InternetOpen("MyFTP Control", 1, vbNullString, vbNullString, 0)

    INetConn = InternetConnect(INet, txtFTP_URL.Text, 0, txtFTP_UserName.Text, txtFTP_Password.Text, 1, 0, 0)

    RC = FtpGetFile(INetConn, "/test/test1.txt", "c:\FTP\test\test1.txt", True, 1, 0, 0)

     

    If RC Then MessageBox.Show("Transfer succesfull!") Else MessageBox.Show("Transfer Failed", "There was an error in connection")

    InternetCloseHandle(INetConn)

    InternetCloseHandle(INet)

     

    End If

     

    End Sub

     

     

    you will have to adjust it as you need it

    i don't know exactly how you want it to flow

    or if you want to stop after an error then you can use      exit sub()    after the messagebox

    Sunday, August 12, 2007 10:13 PM
  • You have a pretty fundamental misunderstanding how Windows programming works.  I'm not quite sure how to explain it.  MessageBox.Show() stops the code in your Click event handler from running until the user clicks the OK button.  At that point, code execution continues and will resume at the InternetOpen() call.  While the message box is displayed, the user will not have an opportunity to enter text in the required TextBoxes, MessageBox ensure it is "modal" by disabling all of the forms.

    There are two ways to solve your problem.  First is to add "Exit Sub" after the MessageBox.Show() call.  That makes sure that you won't execute the InternetOpen() call.  You would have to give directions to the user and remind her to enter the required username and password, then click the button again.  The second way is to create a separate form that requests entry of the name and password, you'd display that form with the ShowDialog() method.  Keep showing the form until the user enters the what you need or clicks the Cancel button.  You'd know whether the user clicked OK or Cancel by looking at the ShowDialog() method return value.

    While you're at it, you might want to consider using the System.Net.FtpWebRequest class instead of P/Invoke.
    Sunday, August 12, 2007 10:24 PM
    Moderator