I am making a login form in C# visual studio from my local MySQL database. However everytime i enter the informations it tells me that the username and/or password is incorrect which i know is false. here is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Dark_Heresy
{
public partial class Form1 : Form
{
private MySqlConnection connection = new MySqlConnection();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btn_Login_Click(object sender, EventArgs e)
{
try
{
String MyConnection = "datasource = localhost; port = 3306; username = root; password = Mypass;";
MySqlConnection MyConn = new MySqlConnection(MyConnection);
MySqlCommand SelectCommand = new MySqlCommand("SELECT * FROM dark_heresy.users WHERE users_='"+ this.TextUserName.Text + "' and password_='"+ this.TextPassword.Text + "';", MyConn);
MySqlDataReader MyReader;
MyConn.Open();
MyReader = SelectCommand.ExecuteReader();
int count = 0;
while (MyReader.Read())
{
count = count++;
}
if (count == 1)
{
MessageBox.Show("Connection Successful");
}
else if (count > 1)
{
MessageBox.Show("Duplication of Username and Password... Access Denied");
}
else
MessageBox.Show("Incorrect Username and/or Password");
MyConn.Close();
}
catch (Exception exp)
{
MessageBox.Show("Error: \r\n" + exp);
}
}
}
}
In MySQL workbench it work, it shows me the "user" with its "password" when make this query:
SELECT * FROM dark_heresy.users WHERE users_='admin' and password_='adminpass';
The connection works fine, it has been tested, but always get the result "Incorrect Username and/or Password".
COUNTin your query andExecuteScalarto execute it.EXISTS.