Click here to Skip to main content
15,924,935 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i just need to compare my text box values with db values and according to that i need to throw a message saying successful if its correct, if its wrong it will return unsuccessful message.

C#
SqlConnection cn = new SqlConnection(global::EnQApp.Properties.Settings.Default.Database1ConnectionString);

            try {
                cn.Open();
                using (SqlCommand command = new SqlCommand("SELECT * FROM Login", cn))
                {
                    //
                    // Invoke ExecuteReader method.
                    //
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        string name = reader.GetString(0);  // Name string
                        string pass = reader.GetString(1); // Password string

                        string txtname = UName.Text;
                        string txtpword = PWord.Text;

                        if (txtname == name && txtpword == pass)
                        {
                            MessageBox.Show("Password Accepted");
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show("Password Not Valid");
                        }
      
                    }

            }
            catch (Exception ex) { }
            finally { }
        }
Posted
Updated 7-Dec-14 23:04pm
v2
Comments
Richard MacCutchan 8-Dec-14 5:03am    
So what is your question?

And if you are storing passwords in your database without hashing them then you are leaving yourself wide open to hacker attacks. Learn how to store passwords securely.
Hemas Ilearn 8-Dec-14 5:06am    
i'm just reading login name password and i want compare those with my textbox values?
actually this is just a basic program?

You should only read the one record from database for the given user user name. Then compare the retrieved password to the one in text box:
C#
using (SqlCommand command = new SqlCommand("SELECT pass FROM Login WHERE uname = @uname", cn))
{
	command.Parameters.AddWithValue("@uname", UName.Text);
	string pass = (string)command.ExecuteScalar();

	if (PWord.Text == pass)
	{
		MessageBox.Show("Password Accepted");
		this.Close();
	}
	else
	{
		MessageBox.Show("Password Not Valid");
	}	
}

This is IMO the easiest you can do right now. Please note that this doesn't handle the situation if the user is not in the database at all. And you shouldn't store password in plain text as already pointed out in the comments.
 
Share this answer
 
Please, don't do that!
Firstly, it's spectacularly inefficient - why retrieve everybody and then look at their names and passwords? SQL has a WHERE clause that lets you say "I'm only interested in these rows".

Secondly, never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^] - the link includes the code.
 
Share this answer
 
 
Share this answer
 

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