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

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

PrivateDeclareFunction InternetCloseHandleLib"wininet.dll" (ByVal HINetAsInteger)AsInteger

PrivateDeclareFunction InternetOpenLib"wininet.dll"Alias"InternetOpenA" (ByVal sAgentAsString,ByVal lAccessTypeAsInteger,ByVal sProxyNameAsString,ByVal sProxyBypassAsString,ByVal lFlagsAsInteger)AsInteger

PrivateDeclareFunction InternetConnectLib"wininet.dll"Alias"InternetConnectA" (ByVal hInternetSessionAsInteger,ByVal sServerNameAsString,ByVal nServerPortAsInteger,ByVal sUsernameAsString,ByVal sPasswordAsString,ByVal lServiceAsInteger,ByVal lFlagsAsInteger,ByVal lContextAsInteger)AsInteger

PrivateDeclareFunction FtpGetFileLib"wininet.dll"Alias"FtpGetFileA" (ByVal hFtpSessionAsInteger,ByVal lpszRemoteFileAsString,ByVal lpszNewFileAsString,ByVal fFailIfExistsAsBoolean,ByVal dwFlagsAndAttributesAsInteger,ByVal dwFlagsAsInteger,ByVal dwContextAsInteger)AsBoolean

PrivateDeclareFunction FtpPutFileLib"wininet.dll"Alias"FtpPutFileA" (ByVal hFtpSessionAsInteger,ByVal lpszLocalFileAsString,ByVal lpszRemoteFileAsString,ByVal dwFlagsAsInteger,ByVal dwContextAsInteger)AsBoolean

Code Snippet

PrivateSub btnConnectToFTP_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles btnConnectToFTP.Click

Dim msgAsString =""

Dim INetAsInteger

Dim INetConnAsInteger

Dim RCAsBoolean

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()

EndIf

If msg.Length <> 0Then

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

EndIf

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 RCThen MessageBox.Show("Transfer succesfull!")Else MessageBox.Show("Transfer Failed","There was an error in connection")

InternetCloseHandle(INetConn)

InternetCloseHandle(INet)

EndSub

[14138 byte] By [VBT] at [2008-1-8]
# 1

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

js06 at 2007-10-2 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 2
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.

nobugz at 2007-10-2 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...