When I attempt to run the application and insert stored username and password or random characters an error pops up instead of the intended validation message. Code Below:
private void btnLogin_Click(object sender, EventArgs e)
{
//Connection Login form to SQL Database login table to get username and password
SqlConnection sqlcon = new SqlConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\mbewe\Documents\Visual Studio 2019\VS Projects\ZRA Transport Requisition\LoginDB.mdb");
string query = "Select * from Logininfo where Username = '" + tbUsername.Text.Trim() + "'and Password= '" + tbPassword.Text.Trim() + "'";
SqlDataAdapter sda = new SqlDataAdapter(query, sqlcon);
DataTable dtbl = new DataTable();
sda.Fill(dtbl);
if (dtbl.Rows.Count == 1)
{
//Setting which form to open once username and password have been confirmed
frmRD objFrmRD = new frmRD();
this.Hide();
objFrmRD.Show();
}
else
{
//Setting message to be shown when username and password do not exist in the database
MessageBox.Show("Check Your Username and Password");
}
}
As others said, you need to use OleDbConnection.
I modify the code you provided, which can verify the username and password successfully.
private void button1_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\School.mdb");
con.Open();
string query = "Select * from Logininfo where Username = '" + tbUsername.Text.Trim() + "'and Password= '" + tbPassword.Text.Trim() + "'";
OleDbDataAdapter sda = new OleDbDataAdapter(query, con);
DataTable dtbl = new DataTable();
sda.Fill(dtbl);
if (dtbl.Rows.Count == 1)
{
MessageBox.Show("success");
//Setting which form to open once username and password have been confirmed
Form2 form = new Form2();
this.Hide();
form.Show();
}
else
{
//Setting message to be shown when username and password do not exist in the database
MessageBox.Show("Check Your Username and Password");
}
}
Tested result:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.