title: PostgreSQL "Column "foo" does not exist"
body: <p>I'm trying to make a search method for my program. In search page, there are 8 variables a user can search in. For example, If user types only into "Name:" this method will Select according to the name's. If user decided to type into "Id:" too, this method will Select according to both. I have 8 variables in the original.   </p>
  
  <p>Well... Whenever I try to search, I got "Column 'sth' does not exists" message. If I only type on name, it says column 'Name' does not exist. Same for Id, except it says cloumn 'Id' does not exist.</p>
  
  <p>I can't figure out what is the problem. It's probably about the prepared statements. My null search works by the way.</p>
  
  <pre><code>    public ObservableCollection&lt;Foo&gt; Search(Foo foo)
      {
          ObservableCollection&lt;Foo&gt; results = new ObservableCollection&lt;Foo&gt;();
  
          IdParam = new NpgsqlParameter("@Id", NpgsqlTypes.NpgsqlDbType.Bigint);
          NameParam = new NpgsqlParameter("@Name", NpgsqlTypes.NpgsqlDbType.Varchar, 50);
  
          string sql;
  
          //Null search
          if (intern == null) 
              sql = "select * from \"Foos\"";
          else
          {    
              sql = "select * from \"Foos\" ";
              int parameterCount = 0;
  
              //This part goes too long, So I just put 2 sample of it. I have 8 parameters in the  
              //original
              if (foo.Id &gt; 0)
              {
                  if (parameterCount == 0)
                      sql += "WHERE \"Id\"=";
                  if (parameterCount &gt; 0)
                      sql += "AND \"Id\"=";
                  IdParam.Value = foo.Id;
                      sql += "@Id ";
                  parameterCount++;
              }
  
              if (foo.Name != "" &amp;&amp; foo.Name != null)
              {
                  if (parameterCount == 0)
                      sql += "WHERE \"Name\"=";
                  if (parameterCount &gt; 0)
                      sql += "AND \"Name\"=";
                  NameParam.Value = foo.Name;
                      sql += "@Name ";
                  parameterCount++;
              }
  
          }
          try
          {
              NpgsqlCommand cmd = new NpgsqlCommand(sql, dbConnection);
              NpgsqlDataReader dr = cmd.ExecuteReader();  //This line gives the error
              Intern result = new Intern();
              while (dr.Read())
              {
                  result.Id = (long)dr.GetInt64(dr.GetOrdinal("Id"));
                  result.Name = dr.GetString(dr.GetOrdinal("Name"));
  
                  results.Add(result);
                  result = new Intern();
              }
              return results;
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.ToString());
              return null;
          }
      }
  </code></pre>
  
  <p>When I don't use prepared statements, it works!</p>
  
  <pre><code>           if (intern.Id &gt; 0)
              {
                  if (parameterCount == 0)
                      sql += "WHERE \"Id\"=";
                  if (parameterCount &gt; 0)
                      sql += "AND \"Id\"=";
  
                  //IdParam.Value = intern.Id;
                  //sql += "@Id ";
  
                  sql += intern.Id + " ";  //When I do this, it works.
                  parameterCount++;
              }
  </code></pre>
  
  <p>What should I do to solve this problem? </p>