0
        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);
                }
            }


            bc.DestinationTableName = "CSVTest";
            bc.BatchSize = dt.Rows.Count;
            con.Open();
            bc.WriteToServer(dt);

            //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
        {
            //Closing Bulk Copy
            bc.Close();
            //Closing Sql Connection
            con.Close();
            //Dispose method internally calls Close..So you dont need to call the close explicitly.
            sr.Dispose();
        }

read file from ftp sever complete read file while loop thorws error Cannot access a disposed object. Object name: 'System.Net.Sockets.NetworkStream'.

4

1 Answer 1

1

You need to check the FtpWebResponse StatusCode before proceeding.some times request can fail.

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

check this -> response.StatusCode

Sign up to request clarification or add additional context in comments.

3 Comments

get error Error Non-invocable member 'System.Net.FtpWebResponse.StatusCode' cannot be used like a method.
@Pradip: can you show me your peice of code how you are accessing StatusCode i think you are accessesing as : response.StatusCode()?
error : Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.