I couldn't really find posts that answered this specific scenario. Plus maybe I am just really tired. Anyway, I am working on a login authentication for WinForms. I have a class named DBFunctions.cs which hold database connection info etc. I am stuck with this "The left-hand side of an assignment must be a variable, property or indexer" error in C#. Please find my current code below. Thanks in advance.
namespace emsdashboard
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
//Contains the SQL string and other information to process
//user login.
public object VerifyUser(string userId, string password)
{
DBFunctions dbInfo = new DBFunctions();
bool status = false;
string verifyUserQry = "SELECT * FROM Employee WHERE UserName = '" + userId + "' AND Password = '" + password + "'";
DataTable dt = default(DataTable);
dt = dbInfo.OpenDTConnection(verifyUserQry);
if (dt.Rows.Count == 1)
{
status = true;
}
return status;
}
//When the login button is clicked. Check to see if the user
//entered a username and/or password. Also verify the username
//and the password are correct, else display an error message.
private void btnLogin_Click(object sender, EventArgs e)
{
if(tbxUsername.Text=="" || tbxPassword.Text=="")
{
MessageBox.Show("Username and Password cannot be blank", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
if (VerifyUser(tbxUsername.Text, tbxPassword.Text) = true)
{
this.Hide();
}
}
}
}
}
Nice easy one, you're confusing =
(the assignment operator) with ==
(the comparison operator).
You would be meaning to enter
if (VerifyUser(tbxUsername.Text, tbxPassword.Text) == true)
(rather than = true
)
But really, comparing a boolean value with a constant boolean value is a redundant operation.
You should just use:
if (VerifyUser(tbxUsername.Text, tbxPassword.Text))
I was trying to convert an object to bool. I declared status of type bool and returned a bool so, I needed to change public object to public bool. Code follows:
Original:
public object VerifyUser(string userId, string password)
{
DBFunctions dbInfo = new DBFunctions();
bool status = false;
string verifyUserQry = "SELECT * FROM Employee WHERE UserName = '" + userId + "' AND Password = '" + password + "'";
DataTable dt = default(DataTable);
dt = dbInfo.OpenDTConnection(verifyUserQry);
if (dt.Rows.Count == 1)
{
status = true;
}
return status;
}
Corrected
public bool VerifyUser(string userId, string password)
{
DBFunctions dbInfo = new DBFunctions();
bool status = false;
string verifyUserQry = "SELECT * FROM Employee WHERE UserName = '" + userId + "' AND Password = '" + password + "'";
DataTable dt = default(DataTable);
dt = dbInfo.OpenDTConnection(verifyUserQry);
if (dt.Rows.Count == 1)
{
来源:https://stackoverflow.com/questions/41627046/c-sharp-left-hand-side-of-an-assignment-must-be-a-variable-property-or-indexer