Click here to Skip to main content
15,885,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am creating login form which has two text boxes for username and password correspondingly and two buttons which is save and login.Now i can store username and password in database by pressing save button.Now i need code to login.When i press login button,it should check the username,password is correct or not.If it is correct, it should work.Kindly send me the coding.

Thanks & Regards,

Viswanathan.M
Posted
Updated 6-Jul-11 9:04am
v3
Comments
[no name] 6-Jul-11 2:29am    
you are asking or telling us.
Viswanthan.M. 6-Jul-11 2:30am    
asking...

hello Viswanthan,


you have created connection to insert the data. then use that connection to retrieve the data from ur DB store in Data table and check against ur username and password. if it matches then show the next window else show the message that username password is wrong.

this all you can do on login button click event



thanks
sanjeev
 
Share this answer
 
Comments
Viswanthan.M. 6-Jul-11 2:36am    
Hi Mr.Sanjeev,

I know the concept... I have some codings,but it ll not work. So kindly send me the coding please.
walterhevedeich 6-Jul-11 2:37am    
Update your question and post your code, including the error details and line where it errored. That way, we may be able to help you to pinpoint what the issue is.
[no name] 6-Jul-11 2:37am    
give me ur insert button code i'll convert it for login
Viswanthan.M. 6-Jul-11 2:41am    
private void btnSave_Click(object sender, EventArgs e)
{
if ((txtUserName.Text != "") & (txtPassword.Text != ""))
{
DialogResult dr = MessageBox.Show("Do you Want to be a User?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (dr==DialogResult.Yes)
{
con.Open();
SqlCommand cmd = new SqlCommand("insert into login values ('" + txtUserName.Text + "','" + txtPassword.Text + "')", con);
cmd.ExecuteNonQuery();
con.Close();
}
}
else
{
MessageBox.Show("Give Full Details","Warning",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}
......
this is my insert button code.
Viswanthan.M. 6-Jul-11 2:49am    
HI sanjeev,

Can you help me?
 
Share this answer
 
Depends on where you are storing your username and password (database, file etc).

You need to write your logic so that it checks against this store and then login only if the password matches.
 
Share this answer
 
Here is a simple code

Try to call all data from the database where username=? and password=?
If the dataset returned is not null make him login else stop there with an error message

C#
 private void button1_Click(object sender, EventArgs e)
{
    try
    {
        if (txtUsername.Text.Trim() == null || txtUsername.Text.Trim() == "")
        {
            MessageBox.Show("Enter an valid username");
            txtUsername.Focus();
            txtUsername.Text = "";
        }
        else if (txtPassword.Text.Trim() == null || txtPassword.Text.Trim() == "")
        {
            MessageBox.Show("Enter an valid password");
            txtPassword.Focus();
            txtPassword.Text = "";
        }
        else
        {
            OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
            oleDbConnection1.Open();
            OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("select *  from masterusertable where userid='" + txtUsername.Text + "' AND passsword='" + txtPassword.Text + "'", oleDbConnection1);
            oleDbCommand1.CommandType = CommandType.Text;
            try
            {
                OleDbDataReader data = oleDbCommand1.ExecuteReader();
                if (data.Read() == true)
                {                         
                    this.Hide();
                    MainMenuForm mainmenuform = new MainMenuForm();
                    mainmenuform.Show();
                }
                else
                {
                    MessageBox.Show("Enter an valid username or password");
                    txtUsername.Text = "";
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show("Exception at Database close any Access Database program and restart");
                Console.WriteLine("Error", exp);
            }           
        }
    }
    catch (Exception eps)
    {
        Console.WriteLine("Error",eps);           
    }
}
 
Share this answer
 
v5
Comments
fjdiewornncalwe 6-Jul-11 14:43pm    
SREENATH.G, please find the key on your keyboard called Caps Lock. Use it please. Writing in all capital letters on this site is considered yelling and impolite. Also, please wrap code dumps in pre tags so that they appear in a nicer fashion. I have fixed these two things in your answer. Cheers.
fjdiewornncalwe 6-Jul-11 14:46pm    
I would suggest that you use the SqlConnection and not the OleDbConnection. The OP is using a SQL Server and that is the "correct" way to do things. Other than that, this solution is reasonable. There is a lot of room for improvement, but it is simple enough that an inexperienced dev like the OP would benefit from it.
SREENATH GANGA 7-Jul-11 5:40am    
sorry sir Iam new in codeproject anyway thank you for advice
A login form should NOT be used to *save* user credentials. Instead, you should use a registration form. So, take the "save" button off your login form. There should be two text boxes (user ID and password), and two buttons (OK and CANCEL).

If the user can't be authenticated, he should be presented with either the appropriate error message, or a chance to register (using a different form, of course). Once registered, he should be re-prompted to login before being granted access to the application.
 
Share this answer
 
v3

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