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.
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.
Here is my query:
SELECT DISTINCT ResponseFormParent.*
FROM ResponseFormParent
INNER JOIN ResponseForm
ON ResponseFormParent.ResponseFormParentId = ResponseForm.ResponseFormParentId
WHERE ResponseForm.RegistrationDeadline >= '5/1/2009'
ResponseFormParent has 3 ints, 1 datetime, and 1 nvarchar(50). ResponseForm has 4 ints, 1 datetime, 1 bit, and 1 nvarchar(255).
What is causing the problem? The error doesn't make sense.
UPDATE: code to call query
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;
}