Hello all,

I have a small problem that I would like to ask for help on. I appreciate anyone's reply.

I have to convert a program of mine over to C#, however, I am new at C#. I know a good amount of VB.NET, but the C# language is a little foreign to me.

My vb code is:

Private Sub PassOKButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PassOKButton.Click
        If _userTextBox.Text = My.Settings.Username AndAlso _passTextBox.Text.GetHashCode() = My.Settings.PasswordHashcode Then
            MessageBox.Show("Access Granted!")
            MessageBox.Show("You must login everytime to access the Admin Panel!")
            AdminPanel.Label3.Visible = False
            AdminPanel.Show()
            Me.Close()

        Else
            MessageBox.Show("Wrong Username or Password")
        End If

and I have tried to redo it and I am getting stuck. My C# code is:

private void _loginbtn_Click(object sender, EventArgs e)
        {
            if (_usernametxt == Properties.Settings.Default.adminusername && _passwordtxt.Text.GetHashCode() == Properties.Settings.Default.adminpass)
            {
                MessageBox.Show("Wrong Username and/or Password.", "Admin Login",
                  MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                this.Close();
            }
            else
            {
                MessageBox.Show("Wrong Username or Password");

            }

        }
    }

}

but I am throwing errors. Everytime I try to fix one another couple errors appear.

Does anyone know what I am doing wrong.

Thank you in advanced

daveofgv

Recommended Answers

All 11 Replies

If _userTextBox.Text in the VB.NET code seems like a TextBox to me. In the C#.NET code it looks like a private string field of a the class, not a TextBox field. Then it should be _usernametxt.Text instead of _usernametxt .

Thanks for the reply.

It is saying:

Operator '&&' cannot be applied to operands of type 'string' and 'int'

And

Invalid expression term '&&'

It would also be handy if you posted the error's you are experiencing in this thread. From the looks of it I agree with ddanbe on what it seems like.

I would also recommend to stop prefixing your control names with an underscore.

call .ToString() on the .GetHashCode():

private void simpleButton2_Click(object sender, EventArgs e)
    {
      TextBox textBoxUserName = null, textBoxPassword = null;
      if ((textBoxUserName.Text == Properties.Settings.Default.adminusername) && (textBoxPassword.Text.GetHashCode().ToString() == Properties.Settings.Default.adminpass))
      {
      }
    }

&& is the conditional AND operator in C#. It only works with boolean values. GetHashCode() returns an int, you then compare it with a string(adminpass). You should compare it with adminpass.GetHashCode() ,also an int.
C# is very picky about types.

Thank you for you help.

I now have no errors and my program can debug.....

One question though?????

For some reason it dosn't matter if I type the correct username and password that is in my settings.settings or a made up one it still lets me pass through without hitting the "else" messagebox

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Promo_SSP
{
    public partial class adminpass : Form
    {
        public adminpass()
        {
            InitializeComponent();
        }

        private void cancelbtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void loginbtn_Click(object sender, EventArgs e)
        {
     

  
    {
      if  (usernametxt.Text == Promo_SSP.Properties.Settings.Default.adminusername && passwordtxt.Text.ToString() == Promo_SSP.Properties.Settings.Default.adminpass) {
          MessageBox.Show("Access Granted!");
          MessageBox.Show("You must login everytime to access the Admin Panel!");
          adminpanel adminfrm = new adminpanel();
          adminfrm.Show(); 
          this.Close();
     }
     else {
         MessageBox.Show("Wrong Username or Password");
      }
  }



        }

        private void passwordtxt_TextChanged(object sender, EventArgs e)
        {

        }
    }

    
}

I change all the object names without the underscore. (thanks for the advise on that)

daveofgv

Zip up your project and post it here. That doesn't make any sense and i'm guessing you have more code than you pasted here because the code you pasted should not allow that to happen.

Please bear in mind that I just started this project so there are some empty forms.

The code I have been ref to is on adminpass.cs.

Please let me know....

daveofgv

i dont know what about you or the others but any string i put in the admin or in the password grant me access to the admin panel.

what you do is every time that you update a char in the text box it update its attribute and threfore you will always have access granted message.

and thats way you never get the "Wrong Username or Password" message.

disable the update of the attribute in the settings file by disable the databinding of the controls.

ps: databinding works two ways to set and to get, of course you can disable the set by deleting it from the "Settings.Designer.cs" file
but its you choise

Thank you IdanS......

I set the application databindings text to none. This is a bad mistake on my part since I don't do it in vb.net.......

Thank you for your time and your last post sparked one of my 2 brain cells saying that I messed up.....

Owner Error....lol

Thanks IdanS

daveofgv

Thank you IdanS......

I set the application databindings text to none. This is a bad mistake on my part since I don't do it in vb.net.......

Thank you for your time and your last post sparked one of my 2 brain cells saying that I messed up.....

Owner Error....lol

Thanks IdanS

daveofgv

you very welcome

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.