title: Exception of type 'System.OutOfMemoryException' was thrown while reading data from text file
body: <p>I m trying to read a text file data with "|" separated, I m using below code. I m able to read the data from text file after reading when trying to put datatable data into dataview I m getting Exception of type <strong>'System.OutOfMemoryException'</strong> was thrown, </p>
  
  <p>Can any one suggest me how to avoid this exception.</p>
  
  <pre><code>string filepath = System.Configuration.ConfigurationManager.AppSettings["data"];  
  
  if (filepath != "")
              {
                  DataTable dt = new DataTable("file");
                  string[] columns1 = null;
                  var lines = File.ReadAllLines(filepath);
                  int Count = lines.Length;
                  //here taking columns and adding to table
                   if (lines.Count() &gt; 0)
                  {
                      columns1 = lines[0].Split(new char[] { '|' });
                      foreach (var column in columns1)
                          dt.Columns.Add(column);
                  }                   
                   for (int i = 1; i &lt; lines.Count(); i++)
                  {
                      DataRow dr = dt.NewRow();
                      string[] values = lines[i].Split(new char[] { '|'  });
                      for (int j = 0; j &lt; values.Count() &amp;&amp; j &lt; columns1.Count(); j++)
                      {
                        dr[j] = values[j];
                      }
  
                  }
                   dt.Rows.Add(dr);   
  
                      DataView View = new DataView(dt);
                      //Here I m getting "Exception of type 'System.OutOfMemoryException' was thrown."
                      DataTable MD = View.ToTable("MD", false, "ID", "Description")
                      DataTable MM = View.ToTable("MM", false, "RecordNumber", "Item description")
                  if (MD.Rows.Count &gt; 0)
                  {
                      InsertData(MD);
                  }
                  if (MM.Rows.Count &gt; 0)
                  {
                      InsertData1(MM);
                  }
              }
  </code></pre>
  
  <p><strong>StackTrace :-</strong></p>
  
  <pre><code>    at System.Collections.Generic.List`1.set_Capacity(Int32 value)
      at System.Collections.Generic.List`1.EnsureCapacity(Int32 min)
      at System.Collections.Generic.List`1.Add(T item)
      at System.Data.DataView.ToTable(String tableName, Boolean distinct, String[] columnNames)
  </code></pre>