-4

I'm using Visual Studio 2015 Professional and I'm new to that version. So my question will be a simple one.................But I really do need help with this code. This is a login form code without a database. But each time I coded it I can't use else statement and it keep log in even I type a wrong password. So how do I fix this? Here is the code and the Screen shots

So this is the Code

Private Void BtnSI_Click(Object Sender, EventArgs e)

{


String Username = "Test";

String Password = "123";


if

(TxtUN.Text == Username && TxtPW.Text == Password);
MessageBox.Show("Login Successful","Login",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);

}

else

{

MessageBox.Show("Please Check your Username and Password","Login",MessageBoxButtons.OK,MessageBoxIcon.Error);

}
Samvel Petrosov
  • 7,580
  • 2
  • 22
  • 46
Kasun
  • 1
  • 3

1 Answers1

1

You should replace ; from this line by {

if (TxtUN.Text == Username && TxtPW.Text == Password);

And also compare strings with Equals function in this way:

TxtUN.Text.Equals(Username) && TxtPW.Text.Equals(Password)

So that this line should be this:

if(TxtUN.Text.Equals(Username) && TxtPW.Text.Equals(Password)){

Information about difference between == and Equals for string you can find for example here

Community
  • 1
  • 1
Samvel Petrosov
  • 7,580
  • 2
  • 22
  • 46
  • 2
    Worth mentioning that string's `operator ==` calls `string.Equals` so unless you need to worry about culture or case insensitivity using `==` is just fine – pinkfloydx33 May 13 '17 at 12:38
  • Thank you very much. It works .Also can I know how should I do the coding to go from a second page to first page? I mean from a page to previous page? Without using any database? – Kasun May 13 '17 at 12:42
  • @Kasun Please specify what technology(WinForms,WPF or something else) are you using and show the pages code. But I advice you to search for that in the internet and try to do by yourself. And only then create here question if you fail. – Samvel Petrosov May 13 '17 at 12:46