title: C# connecting to a postgres database
body: <p>This is my first time connecting to a database, but i'm having some problems</p>
  
  <pre><code>using Npgsql;
  
  namespace DBPrj
   {
  class Program
  {
      static void Main(string[] args)
      {
          bool boolfound=false;
          NpgsqlConnection conn = new NpgsqlConnection("Server=&lt;ip&gt;; Port=5432; User Id=Admin; Password=postgres.1; Database=Test1"); //&lt;ip&gt; is an actual ip address
          conn.Open();
  
          NpgsqlCommand cmd = new NpgsqlCommand();
          NpgsqlDataReader dr= cmd.ExecuteReader(); //I get InvalidOperationException : The connection is not open.
          if (dr.Read())
          {
              boolfound=true;
              Console.WriteLine("connection established");
          }
          if(boolfound==false)
          {
              Console.WriteLine("Data does not exist");
          }
          dr.Close();
          conn.Close();
  
  
  
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>What could be the problem? Is the NpgsqlConnection string written correctly? Could the database be protected from remote access?</p>
  
  <p>How could I fix this problem?</p>
  
  <p>Thanks in advance!</p>