title: Save datatable value in .txt file c#
body: <p>Here is the table:
  <a href="https://i.stack.imgur.com/dADoI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dADoI.png" alt="enter image description here" /></a></p>
  <p>I want to fetch all the column and row of this table and save in a .txt file.</p>
  <p>The text file name must be SubscriberID.txt(example:281063.txt or 281064.txt) and save in a Folder Name manoj.</p>
  <p>In 281063.txt file the data save in SubscriberID:60;npercentscore:60;nevaleid:7091,courseID:1,...
  LIKE THIS.</p>
  <pre><code>Here i take all the data to a datatable:
  string query = &quot;SELECT * FROM DATAFETCH&quot;;
          SqlCommand cmd = new SqlCommand(query, conn);
  
          DataTable dt = new DataTable();
          dt.Load(cmd.ExecuteReader());
  </code></pre>
  <p>How can i achieve my target using c# asp .net??
  any suggestion??</p>
  <hr />
  <p>You already have half of what you need. i too was looking for a solution to this problem and this seemed to have worked for me.</p>
  <p>after you close the connection i would add this</p>
  <pre><code>Like So
  </code></pre>
  <pre><code>StreamWriter sw = new StreamWriter(attachmentLocation + dr[&quot;SubscID&quot;].ToString()&quot; + x + &quot;.txt&quot;); //create the file
                          
                          foreach (DataRow dr in ResultsTable.Rows)
                          {
                              
                              string line = &quot;ACCTNUMBER:&quot; +dr[&quot;Account&quot;].ToString() +&quot;\n&quot;;
                              //and so on
                              sw.WriteLine(line); //write data
                             // x++;
                              
                          }
  
                          sw.Close();
  </code></pre>
  <pre><code>
  And the reason i did this is because when i added the write file within the loop it would create and replace the file everytime. 
  </code></pre>