ExecuteNonQuery will always return -1 for SELECT statement (http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx). If you only want to check if any row with such conditions exists, it will be better if you use following
query (it will aslo have better efficency on db side):
SELECT COUNT(1) FROM PICLIKES WHERE LikedBy = @LikedBy AND picURL = @picURL
Now you can use ExecuteScalar to achieve your goal:
//At least one row exists
if (Convert.ToInt32(selectCommand.ExecuteScalar()) != 0)
LikeButton.Enabled = false;
//No rows exists
else
LikeButton.Enabled = true;
Please indicate "Mark as Answer" if a post has answered the question.
Yet another developer blog <-- visit my blog
tpeczek
Contributor
2112 Points
260 Posts
Re: Need to check for null return on ExecuteNonQuery()
Jan 10, 2012 11:47 AM|LINK
ExecuteNonQuery will always return -1 for SELECT statement (http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx). If you only want to check if any row with such conditions exists, it will be better if you use following query (it will aslo have better efficency on db side):
Now you can use ExecuteScalar to achieve your goal:
//At least one row exists if (Convert.ToInt32(selectCommand.ExecuteScalar()) != 0) LikeButton.Enabled = false; //No rows exists else LikeButton.Enabled = true;Yet another developer blog <-- visit my blog