Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8463741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:32:23+00:00 2026-06-10T14:32:23+00:00

Using the following program I’m trying to insert all the filenames listed in a

  • 0

Using the following program I’m trying to insert all the filenames listed in a folder on ftp server into a SQL table.

While Writing to table I’m getting an error

c# cannot access a disposed object. object name
‘system.net.sockets.networkstream’

reader.ReadToEnd() Writing the whole stream where I want one by one filename.

namespace Examples.System.Net
{
    public class WebRequestGetExample
    {
        public static void Main()
        {

            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://myftpaddress.com/0708/");
            request.Method = WebRequestMethods.Ftp.ListDirectory;


            request.Credentials = new NetworkCredential("vish10", "MyPasswd");

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            Console.WriteLine(reader.ReadToEnd());

            SqlConnection sqlConnection1 = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DMSTG;Data Source=.");
            SqlCommand cmd = new SqlCommand();

            cmd.CommandText = @"Insert into FTPfileList0708Folder
                                           values('" + reader.ReadToEnd().ToString() + "')";
            cmd.Connection = sqlConnection1;

            sqlConnection1.Open();

            cmd.ExecuteNonQuery();
            sqlConnection1.Close();

            Console.WriteLine("Directory List Complete, status {0}", response.StatusDescription);

            reader.Close();
            response.Close();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:32:25+00:00Added an answer on June 10, 2026 at 2:32 pm

    You’ve already read the complete response here:

    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    
    Stream responseStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(responseStream);
    Console.WriteLine(reader.ReadToEnd());
    

    Once you’ve read it, you can’t read it again.

    Just change to

    string text = reader.ReadToEnd();
    

    … and then add appropriate using statements to handle resources properly:

    string text;
    using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
    {
        using (Stream responseStream = response.GetResponseStream())
        {
            // No need to dispose of the StreamReader as we're already
            // disposing of the stream. It wouldn't do any harm though.
            // Is the text definitely encoded in UTF-8 though?
            text = new StreamReader(responseStream).ReadToEnd();
        }
    }
    

    You should have similar using statements for all of your SQL-related resources.
    That solves the immediate problem – but it’s not clear what that has to do with the specific business of reading filenames. That will depend on what format the filenames are in within the text. From what I remember, FTP is actually very poorly specified when it comes to this sort of thing.

    I would recommend two significant changes:

    • Split your code up into logical methods:
      • Handling the FTP request itself
      • Parsing the data into a suitable format
      • Inserting the data into the database
    • Try to find a higher-level FTP library which will know how to do the parsing for you
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Am using the following program for insert the value in sql server 2005 using
I'm trying to start Microsoft word using QProcess as following: QString program = WINWORD.EXE;
I am trying to reverse a C style string using the following simple program.
I am trying to run the following program using boost::thread. #include <boost/thread.hpp> #include <iostream>
In my following program I'm currently using unordered_map just because I wanted O(1) search/insert
i'm trying to run the following program using on my VM using VirtualBox (i'm
I'm trying to compile the following program using Qt Creator 2.0.1: void f() {
I am using following code to handle all the unhandled exceptions in the program.
Am using the following program for generate the Token,based on the time(current time).It was
Take the following program: #include <cstdlib> using std::rand; #include <iostream> using std::cout; int main()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.