分配的C#左侧必须是变量,属性或索引器
我真的找不到能回答这种特定情况的帖子.再加上也许我真的很累.无论如何,我正在为WinForms进行登录身份验证.我有一个名为DBFunctions.cs的类,其中包含数据库连接信息等.我在C#中遇到此分配的左侧必须是变量,属性或索引器"的错误.请在下面找到我当前的代码.预先感谢.
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).
您将有意输入
if (VerifyUser(tbxUsername.Text, tbxPassword.Text) == true)
(而不是= true
)
但是,实际上,将布尔值与恒定布尔值进行比较是多余的操作.
But really, comparing a boolean value with a constant boolean value is a redundant operation.
您应该使用:
if (VerifyUser(tbxUsername.Text, tbxPassword.Text))