Gary
Gary

Reputation: 1774

SQL Server query is not returning any rows

I am trying to read a single column from a single row. After executing the query no rows are returned but there are fields.

When I look at the reader HasRows is false but the field count is 1.

string sql = "select userid from aspnet_Membership where loweredemail = '@email'";

SqlCommand sqlCommand = new SqlCommand(sql, THOGConnection);

string emailAddress = (string)account["ree_thogemail"];
emailAddress = emailAddress.ToLower();
sqlCommand.Parameters.AddWithValue("@email", emailAddress);

SqlDataReader reader = sqlCommand.ExecuteReader();

while(reader.Read())
{
    userId = (Guid)reader["userid"];
}  

If I select all columns in the row then the field count is 21 but there are still no rows.

Why am I not returning any rows?

Thanks, Gary

Upvotes: 0

Views: 1802

Answers (2)

Morpheus
Morpheus

Reputation: 1634

There is syntax error in your sql query:

string sql = "select userid from aspnet_Membership where loweredemail = '@email'";

should be like the following

string sql = "select userid from aspnet_Membership where loweredemail = @email";

Upvotes: 0

Reza Aghaei
Reza Aghaei

Reputation: 125197

You don't need to use ' around @email. Use :

string sql = "select userid from aspnet_Membership where loweredemail = @email"

Actually it returns no value, because there is no record with loweredemail = '@email' while there is record with for example loweredemail = '[email protected]'.
When using parameters, single quotes are not needed.

Upvotes: 2

Related Questions