2

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

Thanks in advance!

1
  • What is the type for o.Password field? Commented Dec 8, 2017 at 3:37

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Coming from more of a Java background here: Is it not necessary to use the .equals() method instead of the == operator?
@Ryan_L: "Is it not necessary to use the .equals() method" -- C# is not Java. C# allows operator overloading, and the System.String class does in fact overload the == operator, making it equivalent to calling the Equals() method (C# not being Java, there also is no equals() method).
How do I convert them? Would I use the code provided? And if so, where would I put it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.