0
votes

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();

            }
        }
    }
3
Put a breakpoint in there and debug. See what happens when you get to the end of the file.krillgar
You need to learn how to use the debugger to step through the code line by line and inspect the state as it executes. The documentation is a good place to start.mason
Im not familiar with 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
I did use debugger to step through the code, Actually the way I'm reading the string from textbox is wrong, Its not allowing me to enter new string if I enter wrong username.Mubi
@FatTony even if I make the condition false, it still doesn't allow me to enter new string before again comparing it from file. It keeps comparing the old value or null valueMubi

3 Answers

0
votes
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++;
}

The problem lies in your while loop. Imagine line = "incorrectUsername" on the first iteration of your file. You're assigning "incorrectUserName" to line[0] then line[1] then line[2] etc... and it never stops. You just keep assigning an invalid username into an array then comparing the entered username against that value. What you want to do is ditch the "lines" array, and simply compare the username against the line:

if (line.Equals(username))
{
   //Go to new form
}
0
votes

In this simple case you don't need to use StreamReader, this is a simpler approach:

string myUserName = "Admin";

    string[] lines = File.ReadAllLines(usersTextFile, Encoding.UTF8);

    bool found = false;

    if (lines != null)
    {
        foreach (var l in lines)
        {
            if (string.IsNullOrEmpty(l))
                continue;

            if (l.ToLower() == myUserName.ToLower())
            {
                found = true;
                break;
            }
        }
    }

    if (found)
        MessageBox.Show("Welcome " + myUserName + "!", "Protected", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
    else
        MessageBox.Show("User not found!", "Protected", MessageBoxButtons.OK, MessageBoxIcon.Error);
0
votes

My complete code is:

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

namespace Employees_1
{
    public partial class Form2 : Form
    {

        string pathuser = @"//192.168.1.10/Shared-Public/testfile.txt";
        int check = 0;

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            //  string username = username.Text;

        }

        private void button1_Click(object sender, EventArgs e)
        {

            //string usernam = username.Text;
            // MessageBox.Show(usernam);


            while (check != 1)
            {
                userpass();


            }
            this.Close();


        }

        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)
                    {

                        //DialogResult result = MessageBox.Show("Invalid Username or Password", "Error", MessageBoxButtons.OK);
                        // MessageBox.Show("Invalid Username or Password");
                        username.Clear();


                    }
                }
            }


         /*   string[] lines = File.ReadAllLines(pathuser, Encoding.UTF8);

            bool found = false;
            string usernam = username.Text;
            if (lines != null)
            {
                foreach (var l in lines)
                {
                    if (string.IsNullOrEmpty(l))
                        continue;

                    if (l.ToLower() == usernam.ToLower())
                    {
                        found = true;
                        break;
                    }
                }
            }

            if (found)
                MessageBox.Show("Welcome " + usernam + "!", "Protected", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            else
                MessageBox.Show("User not found!", "Protected", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }
            */

    }
}