运算符'=='不能应用于'byte []'和'string'类型的操作数

时间:2017-12-08 03:31:02

标签: c# visual-studio

我正在制作一个包含登录页面的程序,编码时出现此错误:运算符==无法应用于byte[]string类型的操作数

我不知道该去哪里或如何处理它。这是我的错误部分:

private void button1_Click(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(textBox1.Text))
    {
        MessageBox.Show("Please Enter your username.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        textBox1.Focus();
        return;
    }
    try
    {
        using (DataEntities test = new DataEntities())
        {
            var query = from o in test.Users
                        where o.Username == textBox1.Text && o.Password == textBox2.Text
                        select o;
            if(query.SingleOrDefault() != null)
            {
                MessageBox.Show("You have been successfully logged in.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //Add your code process login here
            }
            else
            {
                MessageBox.Show("Your username or password is incorrect.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

提前致谢!

1 个答案:

答案 0 :(得分:1)

如果您的用户名和密码字段是字节数组,则可以在进行比较之前将它们转换为字符串:

Encoding.ASCII.GetString(o.Username)== textBox1.Text