Click here to Skip to main content
15,891,936 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm facing some problems in my login system. If I put the correct information it says login successfully even if I input some wrong info it says "Your username or password is incorrect. Please try again." Now the problem is if doesn't put anything on the username and password field and hit the Login Button... it also Logins successfully... I'm not getting how to fix this weird error. Please Someone help me out from this.


C#
private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                //Connecting Database form SqlDbConnect.cs Class
                con = new SqlDbConnection();
                con.SqlQuery("select * from LookDB.dbo.Users where UserName='" + this.username_txt.Text + "' and Password='" + this.password_txt.Text + "' ;");
                SqlDataReader Reader;
                Reader = con.cmd.ExecuteReader();
                int count = 0;
                while (Reader.Read())
                {
                    count = count + 1;
                }
                if (count == 1)
                {
                    MessageBox.Show("Successfully Login. Press OK to Continue", "Login Successful", MessageBoxButtons.OK, MessageBoxIcon.None);
                    this.Hide();
                    MainWindow newForm = new MainWindow();
                    newForm.ShowDialog();
                }
                else if (count > 1)
                {
                    MessageBox.Show("Access Denied!");
                }
                else
                    MessageBox.Show("Your username or password is incorrect. Please try again.", "Warning", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning);
            }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
        }
Posted
Comments
[no name] 16-May-14 18:37pm    
So check and see if username_txt.Text or password_txt.Text are empty before you do anything and if they are, simply return.
mufeed k 16-May-14 21:53pm    
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection con;
SqlDataAdapter da;
SqlCommand cmd;
DataTable dt;

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
con = new SqlConnection("your coonection string");
cmd=new SqlCommand ();
cmd.Connection = con;
con.Open();
cmd.CommandText = "select query";
da = new SqlDataAdapter();
da.SelectCommand = cmd;
dt = new DataTable();
da.Fill (dt);
if (dt.Rows.Count > 0)
{
MessageBox.Show("Login succesfull");
}
else
{
MessageBox.Show("Invalid username or password");
}




}
}
}

do proper validation on your button click

like

if txtusername.text.trim="" or txtpassword.text.trim="" then
msgbox("Enter yousername and password")

else
your login check code
end if
 
Share this answer
 
Your code seems to be correct. Please ensure that there is no record in your database with blank username and password.

Please use RequiredFieldValidator on textboxes to ensure that form doesn't submit with no input.
 
Share this answer
 
Comments
[no name] 17-May-14 10:17am    
Thanks dude, and yes there was a record in my database with blank username and password. LoL..

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