I have created a program in ASP.Net and VB.Net that allows users to loging and download and upload files to and from an FTP site.

I have so far got it to the point where the user can download files and when they go to the download page, a listbox displays a list of the files that reside on the FTP site.

When I highlight a file and click download, the file downloads as required but the list is then duplicated by double, so you see each file listed twice.

Every time you then click on the download or delete button, the duplicates again and again.

Can anybody advise on why this is happening???

My code is below:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="OutDirectory.aspx.vb" Inherits="FTPUploadSite.OutDirectory" %>

<%@ Import Namespace="FTPUploadSite" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    </head>
<body style="height: 769px; width: 1630px; background-color: #99CCFF; margin-top: 0px;" 
    bgcolor="#99ffcc">
    <form id="form1" runat="server">
    <div style="text-align: center; font-family: Arial; font-size: xx-large; margin-top: 0px; height: 1072px;">
    
        <br />
    
        FTP Out Directory<br />
        <br />
        <br />
        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="ftp://10.9.16.80/dwinn/In" 
            style="font-size: small; font-weight: 700; color: #000000;">View FTP Files</asp:HyperLink>
        <br />
        <br />
        <asp:ListBox ID="ListBox1" runat="server" Height="513px" Width="1111px" 
            BackColor="#99CCFF"></asp:ListBox>
        <br />
        <asp:Button ID="Button5" runat="server" Text="Download File" />
        
        &nbsp;<asp:Button ID="Button6" runat="server" Text="Delete File" />
&nbsp;&nbsp;<br />
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Back to FTP Directories" 
            Width="167px" />
&nbsp;<br />
        <br />
        <asp:Button ID="Button2" runat="server" Text="Exit the system" Width="165px" />
        
        <script runat="server" >
            
            Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

                Dim ftp As FtpWebRequest = DirectCast(WebRequest.Create("ftp://xx.xx.x.xx/In/"), FtpWebRequest)
                ftp.Method = WebRequestMethods.Ftp.ListDirectory
                Dim ftpFiles As New ArrayList()
                ftp.Credentials = New NetworkCredential("user", "password")
                Dim Response As FtpWebResponse = ftp.GetResponse()
                Dim responseStream As Stream = Response.GetResponseStream()
                Dim reader = New StreamReader(responseStream)
                While Not (reader.EndOfStream)
                    ftpFiles.Add(reader.ReadLine())
                    'ListBox1.Items.Add(ftpFiles.ToString())
                End While
                                     
                For Each file In ftpFiles
                    ListBox1.Items.Add(file)
                Next
                
                reader.Close()
                responseStream.Close()
                Response.Close()
                
            End Sub

            Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

                Response.Redirect("NextPage.aspx")
                
            End Sub
            
            Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click

                Response.Redirect("NextPage.aspx")
                
            End Sub
            
            Protected Sub Button6_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button6.Click

                Dim ftp As FtpWebRequest = DirectCast(FtpWebRequest.Create("ftp://xx.xx.x.xx/In/" & ListBox1.Text), FtpWebRequest)
                
                If ListBox1.Text = "" Then
                    MsgBox("A file needs to be selected...!")
                End If
                
                If ListBox1.Text <> "" Then
                    Try
                        ftp.Credentials = New System.Net.NetworkCredential("user", "password")
                        
                        ftp.Method = WebRequestMethods.Ftp.DeleteFile
                        
                        Dim ftpResponse As FtpWebResponse = CType(ftp.GetResponse(), FtpWebResponse)
                        
                        ftpResponse = ftp.GetResponse()
                        
                        MsgBox(ftpResponse.StatusCode, ftpResponse.StatusDescription)
                        
                        ftpResponse.Close()
                        
                    Catch ex As Exception

                    End Try
                End If
                
            End Sub
            
            Protected Sub Button5_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button5.Click

                Dim ftp As FtpWebRequest = DirectCast(FtpWebRequest.Create("ftp://xx.xx.x.xx/In/" & ListBox1.Text), FtpWebRequest)
                
                If ListBox1.Text = "" Then
                    MsgBox("A file needs to be selected...!")
                End If
                
                If ListBox1.Text <> "" Then
                    Try
                        ftp.Credentials = New System.Net.NetworkCredential("user", "password")
                        
                        Dim ftpResponse As FtpWebResponse = ftp.GetResponse()
                        
                        Dim ftpStream As Stream = ftpResponse.GetResponseStream()
                        
                        Dim ftpFile As String = ListBox1.Text
                        
                        Dim ftpFileOutputStream As New FileStream("C:\" & ListBox1.Text, FileMode.Create)
                        
                        Dim ftpContentLength As Long = ftpResponse.ContentLength()
                        
                        Dim ftpBufferSize As Integer = 2048
                        
                        Dim ftpRead As Integer
                        
                        Dim ftpBuffer(ftpBufferSize) As Byte
                        
                        ftpRead = ftpStream.Read(ftpBuffer, 0, ftpBufferSize)
                        
                        While (ftpRead > 0)
                            ftpFileOutputStream.Write(ftpBuffer, 0, ftpRead)
                            ftpRead = ftpStream.Read(ftpBuffer, 0, ftpBufferSize)
                        End While
                        
                        ftpResponse.Close()
                        ftpStream.Close()
                        ftpFileOutputStream.Close()
                        
                    Catch ex As Exception

                    End Try
                End If
                                
            End Sub
            
        </script>

    &nbsp;&nbsp;</div>
    </form>
</body>
</html>

Any help and advice will be great.

Thanks,

Dan

Hi,
Clear out your listbox before you start adding the files to it

ListBox1.Items.Clear()
For Each file In ftpFiles
   ListBox1.Items.Add(file)
Next

This empties the list of the previous inputs so when you reload on the page refresh the previous items aren't doubled up. ListBox1 holds its contents through a page post back.

Hope that helps,

Thank you very much hericles, that worked!!!

I have another problem now though, when I select an item click download or delete, I get popup saying that no record has been selected.

So far I am using If ListBox1.Text = "" and If ListBox1.Text <> "".

What is the best way to say 'if an item has been selected in the listbox' and 'if an item has not been selected in the listbox'?

Thanks,

Dan

Hi,
I think you need to use ListBox1.SelectedItem.Text.
I assume you're getting stuck here:

If ListBox1.Text = "" Then
MsgBox("A file needs to be selected...!")
End If

ListBox1.Text isn't the best option. ListBox1.SelectedItem.Text matches the actual value selected from the list.

Hi,

Thank you very much for your quick response.

I have made the change that you suggested, but am now getting the following error message:

Object reference not set to an instance of an object.

Its pointing to the line:

If ListBox1.SelectedItem.Text = "" Then

Thanks,

Dan

You need to use == there as you're not setting ListBox1 to be "", you're checking the value.
Sorry I should have noticed that before.

Hi,

If I have:

If ListBox1.SelectedItem.Text == "" Then

I get a green underline under the 2nd '='

Sorry to be a pain...I appreciate all of your help.

Thanks,

Dan

My bad, I just did a test project cause I haven't used VB.Net for a while (mainly use C#). It works with just one = which means something else is going on to cause your first error.
Object reference not set to an instance of an object normally means the object has not been created yet but I can't see how that is the case in your code as the ListBox1 is defined in the page.

This happen due to ViewState feature of asp.net. Use IsPostBack boolean page property to ensure that the list is populated once. Read about ViewState.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
         'Your code here
    End If                
End Sub

One last thought on the topic. The SelectedItem object will give that error if you haven't actually selected an item before clicking on the buttons. So if you are clicking on Button5 or Button6 before clicking on an item the program will crash with that error.
Either have one item selected as default when the page loads or include a check to see if ListBox1.SelectedItem is null when checking the SelectedItem.Text

Hi Hericles,

Would it be .Text though? because technically, it is a value, not text isnt it?

How could I include a check?

Thanks,

Dan

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.