Reputation: 77
Previously tried creating a login page with just username and password. However, since my user table has 3 roles, i would like to create a login page that grants login to the user based on their role.
Eg: Admin to admin page, Staff to staff page etc.
Faced the following error in one of my line while trying to implement this: OleDbException was unhandled, No value given for one or more parameters.
Here is my login code:
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.OleDb;
namespace AcuSapp
{
public partial class Login : Form
{
OleDbConnection LoginLink = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\SB18\\Documents\\Visual Studio 2010\\Projects\\AcuSapp\\AcuSapp\\bin\\debug\\AcuzioSecureStore DatabaseX.accdb");
public Login()
{
InitializeComponent();
//textBox_username.Text = "LittleJohn";
//textBox_password.Text = "HelloJohn";
}
private void button_login_Click(object sender, EventArgs e)
{
string username = textBox_username.Text;
string password = textBox_password.Text;
string role_name = comboBox_role.Text;
//this is to give notification if username and password is lesser than 4 characters
// .length will count the characters in the string
// This is to reduce redundant calls. Less calls = less taxing on the db
if ((username.Length < 4) || (password.Length < 4))
{
MessageBox.Show("Wrong Credentials!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
// Set authentication as false. By default, user is not authenticated yet.
bool isAuthenticated = false;
//Opens the connection to db
LoginLink.Open();
// Sets the SQL command to be executed
// Since it is a variable command, it becomes a new SQL command to be executed in Microsoft access
// + is to join the string together
//Does string comparing to see if username and password match exactly, case sensitive.
//var cmd = new OleDbCommand("SELECT COUNT(*) FROM [User] WHERE username = '" + username + "' AND password = '" + password + "' ", LoginLink);
var cmd = new OleDbCommand("SELECT COUNT(*) FROM [User] WHERE STRCOMP(username, '" + username + "', 0) = 0 AND STRCOMP(password, '" + password + "', 0) = 0 AND STRCOMP(role_name, '" + role_name + "', 0) = 0", LoginLink);
// (int)cmd.ExecuteScalar only reads the first few rows from the db
isAuthenticated = (int)cmd.ExecuteScalar() == 1; //Error on this line.
//Closes connection to db
LoginLink.Close();
// if isAuthenticated is true
if (isAuthenticated)
{
// This will open the next page which is form1
Client hello = new Client(this);
hello.Show();
// Hides the login form
this.Hide();
}
else
{
//Always remember to put the last statement in curly braces
//otherwise it wont show the previous error will show this messsage instead
MessageBox.Show("Wrong Credentials!");
}
}
}
}
}
Upvotes: 0
Views: 3969
Reputation: 1721
Your connection string looks a little off. You have two data sources defined with one of those being assigned a provider which isn't a valid data source:
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\SB18\\Documents\\Visual Studio 2010\\Projects\\AcuSapp\\AcuSapp\\bin\\debug\\AcuzioSecureStore DatabaseX.accdb"
You should remove the section:
Data Source=Provider=Microsoft.ACE.OLEDB.12.0;
Try the connection string below may help a little.
OleDbConnection LoginLink = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\SB18\\Documents\\Visual Studio 2010\\Projects\\AcuSapp\\AcuSapp\\bin\\debug\\AcuzioSecureStore DatabaseX.accdb");
Upvotes: 1