That's because you overwrote the text, so the first select no longer exists. If you want both the count of ratings and the count of comments, try this one instead:
getNumRatings.CommandText =
"SELECT (COUNT(*) FROM Ratings where AlbumID = '" + id + "') AS myCount, " +
"(SELECT COUNT(*) FROM Comments where AlbumID = '" + id + "') AS myComment " +
"FROM Ratings where AlbumID = '" + id + "'";
Jim
Main Topics
Browse All Topics





by: Mk3890Posted on 2008-02-20 at 16:20:58ID: 20943746
Well by setting getNumRatings.CommandText twice you have erased the first query.
getNumRatings.CommandText is just a string that you put your query in the query doesn't run until the reader.Read() command so you have to think of this as one query. You can join your two queries by putting UNION at the end so it should look like this...
getNumRatings.CommandText = "SELECT COUNT(*) AS myCount FROM Ratings where AlbumID = '" + id + "' UNION ";
getNumRatings .CommandText += "SELECT COUNT(*) AS myComment FROM Comments where AlbumID = '" + id + "'";
Notice the += for the second time this joins the two strings rather than erasing the first one with the second one.