运算符'=='不能应用于类型为'byte []'和'string'的操作数 [英] Operator '==' cannot be applied to operands of type 'byte[]' and 'string'

查看:212
本文介绍了运算符'=='不能应用于类型为'byte []'和'string'的操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用登录页面制作程序,对它进行编码时出现此错误:运算符==无法应用于类型byte[]string

I'm making a program with a login page and this error came up when coding it: Operator == cannot be applied to operands of type byte[] and string

我不确定该在哪里使用或如何使用它.这是我的错误部分:

Im not sure where to go with it or how to handle it. This is my section with the error:

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

提前谢谢!

推荐答案

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

if your username and password fields are byte arrays you could convert them to string before doing the comparison:

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

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

这篇关于运算符'=='不能应用于类型为'byte []'和'string'的操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆