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

时间:2017-12-09 06:22:45

标签: c# arrays string byte

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

这是我以前用来解决这个问题的代码。我不知道在我的代码中把它放在哪里。请帮忙!!

这是错误的部分:

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 :(得分:-2)

当您尝试编译时,您收到此错误,对吗?

我认为您的问题实际上是您的查询没有引号。

此:

var query = from o in test.Users
where o.Username == textBox1.Text && o.Password == textBox2.text
select o;

应该是:

var query = "from o in test.Users" +
"where o.Username == textBox1.Text && o.Password == textBox2.text" + 
"select o";

query是一个字符串,所以你需要用引号包围=的所有内容。这告诉编译器它是一个字符串文字,而不是更多的代码。