I'm new to C#, and trying to make user login window. Currently I'm trying to search from txt file and it works fine if I enter correct username. But the issue is when wrong username is entered- it stucks in loop.
Actually the way I'm reading the string from textbox is wrong, Its not allowing me to enter new string before again comparing it from file if I enter wrong username. It keeps comparing the old value or null value Can anyone guide how this is done?
public void userpass()
{
int us = 0; //for user pass
string readText2 = File.ReadAllText(pathuser);
using (StreamReader sr2 = new StreamReader(pathuser))
{
string usernam = username.Text;
string line;
string[] lines = new String[500];
while ((line = sr2.ReadLine()) != null)
{
lines[us] = line;
if (lines[us] == usernam)
{
check = 1;
MessageBox.Show(usernam);
Form2 f2 = new Form2();
this.Hide();
break;
}
us++;
}
if (lines[us] != usernam && usernam != null)
{
this.DialogResult = DialogResult.None;
DialogResult result = new DialogResult();
result = MessageBox.Show("Invalid Username or Password?",
"Retry",
MessageBoxButtons.OK);
if (result == DialogResult.OK)
{
username.Clear();
}
}
}
StreamReader
and how exactly it´s working, but it looks like the condition for you loop isn´t correct. Since when the right username is found you break the loop and otherwise it´ll run endless your while-condition probaply can´t ever become false. – Tim Schmidt