HasRows always true

  • Thread starter Thread starter gjtired
  • Start date Start date
G

gjtired

Hi, I can't figure out why my HasRows code is always true. Below is a
snippet of my code.

sqlConnection1.Open();
//Check that quiz name is not a duplicate during the Create(0)task
string cmdCheckName="SELECT Count(Quiz_Name) FROM Quiz WHERE Quiz_Name
= '"+valName+"' ";

SqlCommand cmdCheck = new SqlCommand(cmdCheckName, sqlConnection1);
SqlDataReader drCheckName =
cmdCheck.ExecuteReader(CommandBehavior.CloseConnection);


if (drCheckName.HasRows)
{
lblErrorName.Text = "Name already exists";
}
else
{
 
SELECT COUNT(Col) will always return one row in the result set. This is why
you see HasRow is always true.

You might want to change the SQL String to

//Check that quiz name is not a duplicate during the Create(0)task
string cmdCheckName="SELECT Quiz_Name FROM Quiz WHERE Quiz_Name
= '"+valName+"' ";

so that it'll return 0 rows if quizname is not duplicate.
 
Back
Top