Answered by:
Cannot access a disposed object. Object name: 'System.Net.Sockets.NetworkStream'.

Question
-
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 { }
Complete reading text file while throws exception Cannot access a disposed object.
Object name: 'System.Net.Sockets.NetworkStream'.
Answers
-
solve this issue using this
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(); }
- Marked as answer by Pradip26 Friday, November 15, 2013 11:53 AM
All replies
-
-
-
-
-
I have a high difficulty understand waht this code is even supposed to do.You seem to have mixed Input and Output way too close and there is no Commentary whatsoever.
And I guess you don't see through either. If you do not udnerstand your code, you and nobody else can debug it.
You should reoranise this entire Block so it clearly Follows the IPO-Model:
First all the Input stuff, store it in a temporary Varriable.
Optionally some processing (you seem to just read at one place and write to anotehr, so no processign seems to take plase).
Then all the Output Stuff, where you write what is temp. Example://this is the temproary storage varriable //It is in scope for both input and output blocks byte[] Temp; //Input phase using(/*Whatever disposable you need for input*/){ //Do your input stuff
Temp = //Store the inforamtion you gathered in Temp } //Output phase using(/*Whatever disposable you need for output*/){ //Stire whatever is stored in temp }Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.
-
solve this issue using this
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(); }
- Marked as answer by Pradip26 Friday, November 15, 2013 11:53 AM
-
That code executes the exactly same query 4+ times (once per index in value).
And you keep opening and closing the connection pointlessly in between, causing quite a bit of overhead.
I still doubt that you yourself even understand what you try to do.
Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.