Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
its accepting if username is Sai and SAi ..its treating as same for both username
the same scenario is happening with password
if password is SAI1@
sai1@
its treating as same

What I have tried:

SqlCommand cmd = new SqlCommand("SELECT * FROM VEL_EXISTING_USERDTLS  WHERE USER_NAME = '" + textBox1.Text + "' AND PASSWORD = '" + textBox2.Text + " '", con);

                   cmd.Parameters.AddWithValue("@USER_NAME", textBox1.Text);
                   cmd.Parameters.AddWithValue("@PASSWORD", textBox2.Text);
                   SqlDataReader dr = cmd.ExecuteReader();
                   if (dr.Read())
                   {
                       this.Hide();
                       Form4 f = new Form4();
                       f.Show();
                   }
                   else
                   {
                       MessageBox.Show("invalid login");
                   }
               }
               else
               {
                   MessageBox.Show("USERNAME COULD NOT BE EMPTY");
               }

           }
Posted
Updated 2-May-17 1:03am
Comments
Richard MacCutchan 2-May-17 8:32am    
Everything about that SQL statement is wrong. String concatenation and storing passwords in clear text.

If you are using SQL Server then COLLATION can help in this regard.
Try something like-
C#
SqlCommand cmd = new SqlCommand("SELECT * FROM VEL_EXISTING_USERDTLS  WHERE USER_NAME = '" + textBox1.Text + "' COLLATE SQL_Latin1_General_CP1_CS_AS AND PASSWORD = '" + textBox2.Text + "' COLLATE SQL_Latin1_General_CP1_CS_AS", con);


For further reading, check following link-
Collations | Microsoft Docs[^]

IMPORTANT:
Your query is vulnerable to SQL Injection. That's very critical to know and prevent.
You can use either parameterised query or stored procedure to avoid that.
Please check following links for more information-
SQL Injection Attacks and Some Tips on How to Prevent Them[^]
SQL Injection Attack, its examples and Prevention mechanisms and Techniques in ASP.Net[^]

Hope, it helps :)
 
Share this answer
 
v2
Comments
ZurdoDev 2-May-17 6:54am    
Except do not concatenate user input into your sql query. Use Parameters instead.
Suvendu Shekhar Giri 2-May-17 7:03am    
Thanks for pointing it. I really agree with you.
:'( my bad luck that I earned a negative vote.. your comment came at the same time when I was adding that info to my answer.

Anyway, love to see everybody concerned about the critical issues like SQL Injection and all.

Thanks :)
Why would you check passwords in database if exist? This is not a 'KEY' value. If it is than you have a mistake in your database.

The username is the only thing you can verify if exist. I would advice you to not verify on both.

 SqlCommand cmd = new SqlCommand($"SELECT * FROM VEL_EXISTING_USERDTLS WHERE USER_NAME = '{textBox1.Text}'")
SqlDataReader dr = cmd.ExecuteReader(); 

if(dr.HasRows)
{
  //in use.
}
 
Share this answer
 
You need to append
COLLATE Latin1_General_CS_AS 
in your query

try this

SqlCommand cmd = new SqlCommand("SELECT * FROM VEL_EXISTING_USERDTLS  WHERE USER_NAME = '" + textBox1.Text + "' COLLATE Latin1_General_CS_AS  AND PASSWORD = '" + textBox2.Text + " '" COLLATE Latin1_General_CS_AS , con);


for more info

Collations | Microsoft Docs[^]

SQL SERVER - Collate - Case Sensitive SQL Query Search - Journey to SQL Authority with Pinal Dave[^]

NOTE: Instead of concatenation of a string use parameterized query or stored procedure to prevent SQL Injection


Let us know if you have any query or concern for same
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900