none
Help: Making a login screen for school project and source code wrong!

    Question

  • i'm making a login screen for a school project in C# but i cant get the source code to work. Below is the code. Can you suggest any changes to make that will get it working:

    Note: this is in the button1_click method

     

    string txtUsername;         //Name of txtbox were username is entered

    string txtPassword;         //Name of txtboxwere password is entered

    string Username;

    string Password;

    string Ash;                      //Username

    string hello;                     //Password

     

    if(Username = "Ash" (Password = "hello"))

    {

             //Load user profile if user name and password is correct

             Profile frm = new Profile();

             frm.Show();

    }

    else

    {

            //If failed to login display the error message

            Failed frm =new Failed();

            frm.Show();

    }

    Sunday, April 01, 2007 11:01 AM

Answers

  • You have a couple of fundamental mistakes there:

    I don't really get why you have all the variables that you have...

    You really only need 4 strings [2 to hold the values the user enters and 2 to hold the "correct" values you are going to check against]. txtUsername and txtPassword are redundant in this case.
    The 2 strings you are checking for are the TextBox.Text values [I'm assuming they are stored in Username and Password]

    The main problem here is the checking you are doing:

    to compare strings use the .Equals method:


    if ( (Username.Equals("Ash")) && (Password.Equals("hello"))


    Remember that "=" is the assignment opperator and not the "equals" [==] operator. An easy way to think of this is to always pronounce "=" as "gets"

    ie:

    int x;
    x = 2;  // [x GETS 2]
    x == 2; // [x EQUALS 2]
    Sunday, April 01, 2007 3:43 PM

All replies

  • //In the C#, If statement is that if ( <boolean-expression> ) <embedded-statement> else <embedded-statement>;  "=" Tongue Tiedet a value to a object or variable. 

    if(Username == "Ash"  && Password == "hello")

    {

             //Load user profile if user name and password is correct

             Profile frm = new Profile();

             frm.Show();

    }

    else

    {

            //If failed to login display the error message

            Failed frm =new Failed();

            frm.Show();

    }

    Sunday, April 01, 2007 3:37 PM
  • You have a couple of fundamental mistakes there:

    I don't really get why you have all the variables that you have...

    You really only need 4 strings [2 to hold the values the user enters and 2 to hold the "correct" values you are going to check against]. txtUsername and txtPassword are redundant in this case.
    The 2 strings you are checking for are the TextBox.Text values [I'm assuming they are stored in Username and Password]

    The main problem here is the checking you are doing:

    to compare strings use the .Equals method:


    if ( (Username.Equals("Ash")) && (Password.Equals("hello"))


    Remember that "=" is the assignment opperator and not the "equals" [==] operator. An easy way to think of this is to always pronounce "=" as "gets"

    ie:

    int x;
    x = 2;  // [x GETS 2]
    x == 2; // [x EQUALS 2]
    Sunday, April 01, 2007 3:43 PM