title: SELECT DISTINCT not working in .NET application, but works in SQL Mgmt Studio
body: <p>I have a web app where I am calling a SELECT DISTINCT query on two joined tables. If I try to run the query from the web page, I get the following error: "The text, ntext, or image data type cannot be selected as DISTINCT". When I run it from within SQL Management Studio, the query runs fine - no error. Even more interestingly, there are no text, ntext, or image data types in either of the tables.</p>
  
  <p>It should be noted that I'm stepping through the code, and right before the query is executed, I'm copying the query from the "watch" window into Mgmt Studio, and it runs, when I step through and let .NET run it, the error is thrown. I'm using .NET 2, and the System.Data.SqlClient namespace.</p>
  
  <p>Here is my query:</p>
  
  <pre><code>SELECT DISTINCT ResponseFormParent.* 
  FROM ResponseFormParent 
  INNER JOIN ResponseForm 
    ON ResponseFormParent.ResponseFormParentId = ResponseForm.ResponseFormParentId 
  WHERE ResponseForm.RegistrationDeadline &gt;= '5/1/2009'
  </code></pre>
  
  <p>ResponseFormParent has 3 ints, 1 datetime, and 1 nvarchar(50).
  ResponseForm has 4 ints, 1 datetime, 1 bit, and 1 nvarchar(255).</p>
  
  <p>What is causing the problem? The error doesn't make sense.</p>
  
  <hr>
  
  <p>UPDATE: code to call query</p>
  
  <pre><code>public DataSet ExecuteQuery(string sql)
  {
   DataSet ds = null;
   try
   {
    using (SqlConnection dbconn = new SqlConnection(connectionString))
    using (SqlCommand dbcmd = new SqlCommand(sql, dbconn))
    {
     SqlDataAdapter dbadapter = new SqlDataAdapter(dbcmd);
     ds = new DataSet();
     dbadapter.Fill(ds);
    }
   }
   catch (Exception ex)
   {
    Utility.LogError(sql, ex);
   }
   return ds;
  }
  </code></pre>