Question: I have a website that is required to read data from an external txt file..I had put that file in the App\_data folder and read the data from that file using this code

the txt file contain 3 texts.. which are: kevin123 INFO102lec INFO102k

```
   protected void logInButton_Click(object sender, EventArgs e)
{
    string usernameListString = File.ReadAllText(Server.MapPath("~") + "/App_Data/usernameFile.txt");

    string[] userAray = usernameListString.Split(' ');
    bool usernameExists = false;
    for (int i = 0; i < userAray.Length; i++)
    {
        if (usernameTextBox.Text == userAray[i])
        {
            welcomeLabel.Text = "Welcome" + userAray[i];
        }

        if (usernameTextBox.Text != userAray[i])
        {
            welcomeLabel.Text = "unknown user";
        }

        usernameExists = true;

    }

```

I want to write a `for` loop that goes through each item of `userArray`. At the end of the loop, set `usernameExists` to true only if the username entered in the username text box is present in the array.
I coded something which when I type 1 of those name in the textbox?? it give an welcome label if the name doesn exist?? and vice versa!

IS anything wrong with my code above?

Answer: 
This would work:

```
bool usernameExists = false;
for (int  i = 0; i < userAray.Length; i++)
{
    if (userAray[i] == "kevin123")
    {
        usernameExists = true;
        break; // stop checking more values
    }
}

```

Or more simply:

```
bool usernameExists = userAray.Contains("kevin123");

```

Or if you want to check whether the array contains any of several values:

```
string[] userNamesToSearchFor = new[] { "kevin123", "INFO102lec", "INFO102k" };
bool usernameExists = userAray.Intersect(userNamesToSearchFor).Any();

```

---

Regarding your update, the problem is that you're trying to update your labels as you're looping through the array. You need to first determine if the username exists in the array (`usernameExists`) and *then* set the labels indicating the result, for example:

```
for (int  i = 0; i < userAray.Length; i++)
{
    if (userAray[i] == usernameTextBox.Text)
    {
        usernameExists = true;
        break; // stop checking more values
    }
}

if (usernameExists)
{
    welcomeLabel.Text = "Welcome " + usernameTextBox.Text;
}
else 
{
    welcomeLabel.Text = "unknown user";
}

```
try this

```
protected void logInButton_Click(object sender, EventArgs e)
{
string usernameListString = File.ReadAllText(Server.MapPath("~") 
      + "/App_Data/usernameFile.txt");

string[] userAray = usernameListString.Split(' ');
bool usernameExists = false;
for (int  i = 0; i < userAray.Length; i++)
{
    if (userAray[i]==usernameTextBox.Text)
    {
        welcomeLabel.Text = "Welcome" + userAray[i];
        usernameExists = true;
    }
    else
    {
        welcomeLabel.Text = "unknown user";
    }        
}

```