Darren Holmes

Darren Holmes

  • NA
  • 1
  • 661

MYSQL Login Issue

Aug 20 2016 2:08 PM


down votefavorite

 made a login page that checks the database for the username and password but it's just allowing any username and password it's not rejecting them not sure why I'm new to this, below is the form, I've entered data into the database with user_name and password so it's deffo not that must be something in the code but when I close out of the program it also says password incorrect which is strange it only shows that once I've actually clicked close on the program tho it's weird

namespace LoginApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
PassTextBox.PasswordChar = '•';
}
private void button1_Click(object sender, EventArgs e)
{
try
{
string MyConnection = "datasource=localhost;port=3306;username=user;password=pass";
MySqlConnection MyConn = new MySqlConnection(MyConnection);
MySqlCommand MyCommand = new MySqlCommand("select * from etool.login where user_name='" + this.UserTextBox.Text + "' and password='" + this.PassTextBox.Text + "' ;", MyConn);
MySqlDataReader MyReader;
MyConn.Open();
MyReader = MyCommand.ExecuteReader();
int count = 0;
while (MyReader.Read())
{
Console.WriteLine(MyReader[count]);
count++;
}
MessageBox.Show("Username and password is correct");
this.Hide();
Form2 f2 = new Form2();
f2.ShowDialog();
if (count == 1)
{
}
else if (count > 1)
{
MessageBox.Show("Duplicate Username and passwor.\nAccess denied.");
}
else
{
MessageBox.Show("Username and password is incorrect.\nPleas try again.");
}
MyConn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}

Answers (1)