Click here to Skip to main content
15,885,194 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
While connecting to ftp sever and try to read the contents of a text file get this error
Cannot access a disposed object.
Object name: 'System.Net.Sockets.NetworkStream'.


[edit] Code added from comment
C#
try {
   string strConn = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString.ToString();
   SqlConnection con = new SqlConnection(strConn);

   FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftpserver");

   request.Credentials = new NetworkCredential("username", "pass");

   FtpWebResponse response = (FtpWebResponse)request.GetResponse();
   Stream responsestream = response.GetResponseStream();
   StreamReader sr = new StreamReader(responsestream);

   string line = sr.ReadLine();
   string[] value = line.Split(',');
   DataTable dt = new DataTable();
   DataRow row;
   foreach (string dc in value) {
      dt.Columns.Add(new DataColumn(dc));
   }

   while (!sr.EndOfStream) {
      value = sr.ReadLine().Split(',');
      if (value.Length == dt.Columns.Count) {
         row = dt.NewRow();
         row.ItemArray = value;
         dt.Rows.Add(row);
      }
   }

   SqlBulkCopy bc = new SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock);
   bc.DestinationTableName = "CSVTest";
   bc.BatchSize = dt.Rows.Count;
   con.Open();
   bc.WriteToServer(dt);
   bc.Close();
   con.Close();
   sr.Close();
   sr.Dispose();
   //File.Open(str1, FileMode.Open, FileAccess.Read, FileShare.None);

   using (var writer = new StreamWriter(responsestream)) {
      writer.Write("");
   }
}
catch (ObjectDisposedException a) {
   Console.WriteLine("Caught: {0}", a.Message);
}
finally {
}

[/edit]
Posted
Updated 13-Nov-13 1:41am
v3
Comments
Kornfeld Eliyahu Peter 13-Nov-13 7:11am    
To clarify. You problem that you do not know why the object is disposed when you try to access it, or you knowingly try to access a disposed object?
Kornfeld Eliyahu Peter 13-Nov-13 7:28am    
First. You posted a solution, instead of comment. Remove it, I added it's content to the original question.
Second. Share your code, may someone spot the problem...
(Add the code to the original question)
thatraja 13-Nov-13 7:38am    
Include this in your question
Ron Beyer 13-Nov-13 8:06am    
Where are you telling the FtpWebRequest what file you want to read on the server???
pradip2609 13-Nov-13 8:10am    
read the text file on server

StreamReader will dispose the underlying Stream when disposed.
Move the dispose and close of sr until after the write to the response stream.

Change
C#
sr.Close();
sr.Dispose();
//File.Open(str1, FileMode.Open, FileAccess.Read, FileShare.None);

using (var writer = new StreamWriter(responsestream)) {
   writer.Write("");
}

to
C#
//File.Open(str1, FileMode.Open, FileAccess.Read, FileShare.None);

using (var writer = new StreamWriter(responsestream)) {
   writer.Write("");
}
sr.Close();
sr.Dispose();


Or, better yet, wrap everything up in using statements.

Help this helps,
Fredrik
 
Share this answer
 
Comments
pradip2609 13-Nov-13 8:11am    
while (!sr.EndOfStream)
{
value = sr.ReadLine().Split(',');
if (value.Length == dt.Columns.Count)
{
row = dt.NewRow();
row.ItemArray = value;
dt.Rows.Add(row);
}
}
after complete read it's get error Cannot access a disposed object.
Object name: 'System.Net.Sockets.NetworkStream'.
Fredrik Bornander 13-Nov-13 9:23am    
What line throws that?
Please see this example on how to read a file from an FTP, you are missing quite a bit of stuff required, its more than giving the FtpWebRequest a path, you actually have to tell it you want to download a file.

http://stackoverflow.com/questions/2781654/ftpwebrequest-download-file[^]

(The question has a good example with an explanation of the path as the first answer)
 
Share this answer
 
Comments
pradip2609 13-Nov-13 8:45am    
Same Error
pradip2609 14-Nov-13 8:03am    
Please Help Me..........
Ron Beyer 14-Nov-13 9:15am    
How can we help if you don't tell us what the modified code is?
pradip2609 14-Nov-13 10:13am    
what can i do to solve this error
Ron Beyer 14-Nov-13 10:52am    
Either you can post the code with the modifications I said in my solution above or we cannot help you at all.
solve this
issue


C#
foreach (string dc in value)
          {
          cmd.CommandText = "INSERT INTO Test(ID,FirstName,LastName,BirthDate) VALUES ('" + value[0] + "','" + value[1] + "','" + value[2] + "','" + value[3] + "')";
          cmd.Connection = con;
          con.Open();
          cmd.ExecuteNonQuery();
          con.Close();
           }
 
Share this answer
 
i cam modify code like this
string strConn = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString.ToString();
SqlConnection con = new SqlConnection(strConn);

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftpserver");

request.Credentials = new NetworkCredential("username", "pass");

FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responsestream = response.GetResponseStream();
StreamReader sr = new StreamReader(responsestream);
SqlBulkCopy bc = new SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock);
try
{
string line = sr.ReadLine();
string[] value = line.Split(',');
DataTable dt = new DataTable();
DataRow row;
foreach (string dc in value)
{
dt.Columns.Add(new DataColumn(dc));
}

while (!sr.EndOfStream)
{
value = sr.ReadLine().Split(',');
if (value.Length == dt.Columns.Count)
{
row = dt.NewRow();
row.ItemArray = value;
dt.Rows.Add(row);
}
}
 
Share this answer
 
Comments
CHill60 5-Feb-15 12:58pm    
Well done on being able to modify some code. However you're a bit late - the question was answered over a year ago

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900