Pradip Daingade

Pradip Daingade

  • NA
  • 226
  • 20.6k

Cannot access a disposed object.

Nov 13 2013 7:42 AM
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 { } read complete text file after complete read while loop get error Cannot access a disposed object. Object name: 'System.Net.Sockets.NetworkStream'.

Answers (7)