Search code examples
c#sqlif-statementtype-conversionsqldatatypes

I can't Convert SQL Select Query Result to Bit


After the user has registered; the user's membership is saved as "passive" by default. I do this in the following way: I have a line called "active" in my user table and the data type of this field is bit and default value of this field is 0. What I want to do is: If the user has not activated his account, I want him to get warning but I got System.IConvertible error. My login.aspx.cs is as follows:

DataRow drlogin = function.GetDataRow("SELECT isactive FROM user WHERE email = '" + TxtEMail.Text + "'");
if(Convert.ToInt32(drlogin) == 0)
{
    string message = "<script>alert('You can't login because your account is not active!');</script>";
}
else
{
    // Login operations
}

Solution

  • This line is toxic, robots will hack your website as soon as you publish it:

    DataRow drlogin = function.GetDataRow("SELECT isactive FROM user WHERE email = '" + TxtEMail.Text + "'");
    

    Use parameters and not string concatenation!

    You cannot convert DataRow to Int32, besides it is a bit column that should be converted to bool. So it should be like this:

    if(!Convert.ToBoolean(drlogin[0]))
    

    or

    if(!Convert.ToBoolean(drlogin["isactive"]))