title: Write SQL info to a TXT file
body: <p>Im trying to read a row of information from a DB and write that out to a txt file. I have most of it figured out, but I get the following error "A field initializer cannot reference the non-static field, method, or property 'reader_writer.filewriter.filePath'" and I dont know why. Can someone please explain my problem?</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.IO;
  using System.Data.SqlClient;
  using System.Data.Common;
  
  namespace reader_writer
  {
  public class filewriter
  {
  
      //public string filePath = "";
      bool fileExists = false;
      string filePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
      string dbFile = filePath + @"\sqlfile.txt";
  
      public void Main(string[] args)
      {
  
  
          fileExists = File.Exists(dbFile);
  
          if (fileExists)
          {
              writeFileFromDB();
          }
  
          else
          {
              File.Create(dbFile);
              writeFileFromDB();
          }
  
      }
  
  
      public void writeFileFromDB()
      {
          //create connection
          SqlCommand comm = new SqlCommand();
          comm.Connection = new SqlConnection(@"MY DB CONNECTION STRING");
          String sql = @"SELECT ROW1, ROW2
                             FROM Export.TABLENAME";
  
          comm.CommandText = sql;
          comm.Connection.Open();
  
          SqlDataReader sqlReader = comm.ExecuteReader();
  
          while (sqlReader.Read())
          {
              StreamWriter writer = File.CreateText(dbFile);
              writer.WriteLine(sqlReader["ROW1"] + "\t" + sqlReader["ROW2"]);
              writer.Close();
          }
  
          sqlReader.Close();
          comm.Connection.Close();
      }
  }
  </code></pre>
  
  <p>}</p>