0

Please can you help me why my command for select dont select any data. I try change select from name to ID and it works but i need that this select work for name select not nly for ID.

Database data:

id      user      password      level
1       Rad3k       .....         0

Script for select:

public bool loginVerification(string name, string password)
{
    test = name;
    bool access = false;
    string checkLogin, checkPassword;
    int level;

    commandS = new SqlCommand("SELECT * FROM dbo.users WHERE user like @name", conn);
    try
    {
        conn.Open();
        commandS.Parameters.Clear();

        // Parameters
        SqlParameter nameParam = new SqlParameter("@name", SqlDbType.VarChar, 30);
        nameParam.Value = name;
        commandS.Parameters.Add(nameParam);
        commandS.Prepare();
        SqlDataReader reader = commandS.ExecuteReader();

        while (reader.Read())
        {
            for (int i = 0; i < reader.FieldCount; i++)
                if (reader.IsDBNull(i))
                    return access = false;

            checkLogin = reader.GetString(1);
            checkPassword = reader.GetString(2);
            level = reader.GetInt32(3);

            if (name == checkLogin && hashPassword.ValidatePassword(password, checkPassword) == true)
                access = true;
        }

        return access;
    }
    finally
    {
        conn.Close();
    }
}

If i use this select so it works:

commandS = new SqlCommand("SELECT * FROM dbo.users WHERE id = 1", conn);

But when i use this so it dont work:

commandS = new SqlCommand("SELECT * FROM dbo.users WHERE user like @name", conn);
I try use this without parameter, but it dont work too.
commandS = new SqlCommand("SELECT * FROM dbo.users WHERE user like 'Rad3k'", conn);
commandS = new SqlCommand("SELECT * FROM dbo.users WHERE user like '%Rad3k%'", conn);
commandS = new SqlCommand("SELECT * FROM dbo.users WHERE user like 'Rad3k%'", conn);
commandS = new SqlCommand("SELECT * FROM dbo.users WHERE user = 'Rad3k'", conn);
// and same with paramter still dont work

Thanks for all people which can help me.

5 Answers 5

1

If you use a plain SQL statement, you would use

SELECT * FROM dbo.users WHERE user like '%Rad3k%'

You need the % to match a substring, without them it would mean a full match (only user 'Rad3k' would be found). Note that you could also use like 'Rad3k%' for a "starts with" match.

From C# with parameters, it would be

commandS = new SqlCommand("SELECT * FROM dbo.users WHERE user like '%' + @name + '%'", conn);
Sign up to request clarification or add additional context in comments.

Comments

1

your command should be like this

SELECT * FROM dbo.users WHERE user like '%" + @name + "%'"

4 Comments

like '%" + @name + "%'" no ?
@Ehsan not at start of like ... ;) Anyway, just a detail !
@RaphaëlAlthaus i initially posted this thing. Then i edited incorrectly. And then i reverted.... Thanks.
Syntax and code is correctly and it works in DB but not from C# still find nothing.
0

You need use like operator when you are filtering against strings:

 SELECT * FROM dbo.users WHERE user like '%" + @name + "%'"

Comments

0

Try to set value to passing parameter using AddWidthValue :

SqlParameter nameParam = new SqlParameter("@name", SqlDbType.VarChar, 30);
commandS.Parameters.AddWidthValue("name", name);`

2 Comments

Problem is that, I try use select with parameter and without parameter and from C# it find nothing. only when i select id it work. I dont knwo why. I try this directly. select * from dbo.user where user like '%Rad3k%' - it find nothing from C# and when i try this so too nothing: commandS = new SqlCommand("SELECT * FROM dbo.users WHERE user like '%' + @name + '%'", conn);
edit your query and try commandS = new SqlCommand("SELECT * FROM dbo.users WHERE user like %@name", conn);
0

Your query should work. You can use like operator without %. If you want an excact match on user you can just use user = @name.

Are you sure that you dont have any additional column in that row that are null? This code dismisses all rows with an null column:

            for (int i = 0; i < reader.FieldCount; i++)
            if (reader.IsDBNull(i))
                return access = false;

Try to comment out these lines and test your code.

4 Comments

i think that this is not problem because now i have in table only one row with name Rad3k as i say. This code only for future for more datas. But i trying commented it.
Yes, but the code above checks for columns with null. So of there are any columns in the 'Rad3k' row that are NULL, it will return false. I tested your code on my demo data and it worked fine.
i commented it, but i have still same problem. I dont know where i make fail
Ok, try to run the query in SQL Server Management Studio and see if the query works there. Also try to debug your code to see where it fails?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.