Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
In login form how to check the given password is in database or not.if it is in database only it has to login else it has to show some text message like "password does not exist".and how to apply case sensitive for that password.Using ASP .Net and C#
Posted
Comments
pguimalan 9-Jan-12 23:50pm    
question:

what specific rdbms you are using?
Kim Togo 10-Jan-12 2:02am    
Use Supriya Srivastav and Jaganathan B solutions and you have a working method! :-)

Run the following query in your code,

"Select Id from UserMast where User_Name COLLATE Latin1_General_CS_AS= '" + txt_uname.Text.Trim() + "' and Passwd COLLATE Latin1_General_CS_AS= '" + txt_Pwd.Text.Trim() + "' "


Where UserMast is the table having fields User_Name and Passwd.And txt_uname is the textbox for User Name and txt_Pwd is for Password,If this query returns some id not null then the input detail will be valid else invalid.This query is for Sql Server DB.
 
Share this answer
 
v2
Comments
Kim Togo 10-Jan-12 2:01am    
Yes correct! :-)
The only problem here is SQL injection.
Use SqlCommand.Parameters to solved the problem.
Hi,

Create a method called IsValidUser as like below one.

C#
private bool IsValidUser(string userName, string passWord)

{
bool loginSuccessful = false;
 string sql = "SELECT* FROM  Login WHERE Username=@UserName AND [Password]=@Password'";

SqlCommand sqlCommand= new SqlCommand(sql, con);
sqlCommand.Parameters.Add(new SqlParameter("Name", userName));
sqlCommand.Parameters.Add(new SqlParameter("[Password]", passWord));
SqlDataReader rdr = mySQL.ExecuteReader();

if (rdr.HasrRows()) 
   loginSuccessful = true;

return loginSuccessful ;
}


And use that method in Button Click event as like the below one,

C#
private void loginbtn_Click(object sender, EventArgs e)
 {
    if( IsValidUser(nametxtbx.Text.Trim(),passtxtbx.Text.Trim()))
    {
       //Redirect to .....
    }
    else 
    {
        // say Invalid Username or Password , please try again.
        nametxtbx.Text = "";
        passtxtbx.Text = "";
        nametxtbx.Focus();
    }
           
 }
 
Share this answer
 
Comments
Kim Togo 10-Jan-12 1:57am    
Will not work correct.
The WHERE statement of [Password] ignores upper and lower case.
Hi,


Write a button click event on the login button which calls a stored procedure in database.Pass the userid and password to the procedure.
C#
BtnLogin_Click()
{

//Get the username and password from the text boxes


//call a function which connects to database procedure and store the return value in boolean variable

//if the return value is true
then open next page
//else
display custom error

}

In the procedure pass the uname, pwd as paramters:
pseudo code
Storeprocedure(uname varchar,pwd varchar)
{
check whether the record exists for that uname and pwd.
(something like select count(*) into intcount from users where upper(username)=uname and password=pwd)
if intcount>0 return true.
else return false.
}


Hope this helps.
 
Share this answer
 
v3
using Datareader to read a content of database(password) and compare to your given password.

In below coding is vb.net. Try this .......
I hope its helpful for u.....
VB
cmd = New OleDbCommand("select empcode,password from tablename where empcode = '" + TextBox1.Text + "'AND password = '" + TextBox2.Text + "'", con)
                dr = cmd.ExecuteReader
                Try
                    If dr.HasRows Then
                        dr.Read()
                        If TextBox1.Text = dr.Item("empcode").ToString And TextBox2.Text = dr.Item("password").ToString Then
                          .........
                          .........
                        Else
                            MsgBox("USERNAME AND PASSWORD ARE CASE SENSITIVE. ")
                        End If
                    End If
                    dr.Close()
 
Share this answer
 
Comments
Kim Togo 10-Jan-12 1:59am    
There is no need for a DataReader her.
A SELECT statement with one column will do it.
ckulasekaran 10-Jan-12 4:10am    
To check username and password as case sensitive means how to select a single column ?

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