Samrikan
Samrikan

Reputation: 63

Variable always returning true

I have this in my button click event:

protected void btn_login_Click(object sender, EventArgs e)
{
    authentication auth = new authentication();

    bool emailExists = auth.checkEmail(System.Convert.ToString(txt_email.Text));

    if (emailExists == true)
    {
        btn_create.Text = "Email doesnt exist";
    }
    else
    {
        btn_create.Text = "Email exists";
    }

}

It sends the email to my checkEmail method which is in my authentication class:

public bool checkEmail(string email)
{
    bool emailExists = false;

    usersTableAdapters.UsersTableAdapter user = new usersTableAdapters.UsersTableAdapter();

    users.UsersDataTable userDataTable = user.checkEmail(email);

    if (userDataTable.Rows.Count == 0)
    {
        emailExists = false;
    }
    else
    {
        emailExists = true;
    }

    return emailExists;

}

The checkEmail query is "SELECT COUNT(email) AS email FROM People WHERE (email = ?)"

However when I debug, it always falls through the if(emailExists == true) statement even when the email already exists in my DB, does anyone know why?

Upvotes: 0

Views: 74

Answers (3)

Samrikan
Samrikan

Reputation: 63

I figured out I would have to check the index of the row which in this case is 0 and then retrieve and store the value from the email column into an int

DataRow row = userDataTable.Rows[0];
int rowValue = System.Convert.ToInt16(row["email"]);

Upvotes: 0

SeanKuehnel
SeanKuehnel

Reputation: 41

can't add code markup in comments, so reposting how I would rewrite the checkEmail method: (assuming the how the UsersTableAdapter type is setup)

public bool checkEmail(string email)
{
    usersTableAdapters.UsersTableAdapter user = new usersTableAdapters.UsersTableAdapter();

    users.UsersDataTable userDataTable = user.checkEmail(email);

    return userDataTable.Rows[0][0] > 0;

}

Upvotes: 0

SeanKuehnel
SeanKuehnel

Reputation: 41

your query you entered will always have 1 result. If you want to continue using that query, you should check the first column ("email") of the first result and check to see if that is == 0 or not.

Upvotes: 3

Related Questions