C# Left-Hand Side Of An Assignment Must Be a Varia

2019-01-27 11:35发布

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();
    }
    }

    }
    }
    }

2条回答
地球回转人心会变
2楼-- · 2019-01-27 12:13

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))
查看更多
对你真心纯属浪费
3楼-- · 2019-01-27 12:14

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)
        {
查看更多
登录 后发表回答