namespace Project
{
    public partial class Chapter_Login : Form
    {
        public Chapter_Login()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            StreamReader Chapter = new StreamReader("Chapters.txt");
            Chapter.ReadLine();
            string[] lines = System.IO.File.ReadAllLines("Chapters.txt");
            for(int i =0; i < lines.Length-1;i++)
            {
            foreach (string line in lines)
            {

                int ID = line.IndexOf("-");
                int Pass = line.IndexOf("*");
                string Id = txtID.Text;

                if (line.Substring(0,ID) == lines[i])
                {
                    if (line.Substring(Pass + 1) == txtPassword.Text)
                    {
                        Chapter_Home Home = new Chapter_Home();
                        Home.ShowDialog();
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("Incorrent ID or Password");
                    }
                }
            }

        }

This is what i have so far and i get an exception agruement can not be less the zero
im trying to read a txt box and compare the cotents of a txt file to see if it is in there.
the program is a windows form and user are registering for a conference so they login in and i compare it to the txt file but i cant figure out how to do so. Any help will be greatly appreciated thank you

Recommended Answers

All 11 Replies

what is your text file's format? can you post a part of it?

String.IndexOf Documentation

Return Value: The zero-based index position of value if that string is found, or -1 if it is not. If value is String.Empty, the return value is 0.

line.Substring(0,ID): If value is not found "ID" = -1. You can't do the following: line.Substring(0, -1). It throws "ArgumentOutOfRangeException".

String.Substring Documentation

Exceptions:

ArgumentOutOfRangeException:

  • startIndex plus length indicates a position not within this instance.

-or-

  • startIndex or length is less than zero.

Fix: Ensure "ID" is >= 0 before trying to use it in your substring statement.

*Note: It is bad practice to use both "ID" and "Id" for variable names. It will lead to confusion and wasted time debugging.

Member ID:
-mlaronfl
*061292
<
-Nicole Larose
+fl

txt file - means user ID and * password

So i stepped into it and that is why ID = -1 how change get it to recognizethe ( - ) or the ( * )?

The error isn't occuring on the line that contains "-". It is occuring when a line does NOT contain a "-". You didn't post enough of the text file. You need to post at least 2 - 3 complete records. You don't have to post the actual data, you can make up data as long as it is in the same format.

Why are you reading the file twice (using two different methods)? And why are you using two for loops?

Something like the following may help:

string[] lines = System.IO.File.ReadAllLines("Chapters.txt");

string userId  = string.Empty;
string userPassword = string.Empty;

foreach (string line in lines)
{
    string trimmedLine = line.Trim();

    if (trimmedLine.StartsWith("-"))
    {
        userId = trimmedLine.Substring(1);
    }//if
    else if (trimmedLine.StartsWith("*"))
    { 
        userPassword = trimmedLine.Substring(1);
    }//else if

}//foreach

To answer your original question:

            foreach (string line in lines)
            {
                string trimmedLine = line.Trim();

                if (trimmedLine.StartsWith("-"))
                {
                    int ID = line.IndexOf("-");

                    if (line.Substring(0,ID) == lines[i])
                    {

                        if (trimmedLine.StartsWith("*"))
                        {
                            int Pass = line.IndexOf("*");

                            if (line.Substring(Pass + 1) == txtPassword.Text)
                            {
                                Chapter_Home Home = new Chapter_Home();
                                Home.ShowDialog();
                                this.Close();
                            }//if
                            else
                            {
                                MessageBox.Show("Incorrent ID or Password");
                            }//else
                        }//if
                    }//if
                }//if
            }//foreach

*Note: The code in this post only prevents the issue that you previously mentioned--ArgumentOutOfRangeException.

I recommend you use an XML file (or a database) instead of a text file. You are using characters that could possibly be in the username or password ("-", "*").

I agree with cgeier that your structure is not optimal, but let's assume that it is good enough for what you are trying to do. So the structure of the text file seems to be that for each entry there are three lines, one that starts with a "-" and that represents the user's name, a second that starts with "*" and that represents the password, and then you would have a "<" that is a separator.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            makeTexfile();
            Boolean ok = checkUser("name2","password2");
            if (ok) {
                Console.WriteLine("Name password pair is valid");
            }
            Console.ReadKey();
        }
        private static bool checkUser(string name,string password)
        {
            string[] lines = System.IO.File.ReadAllLines(@"USER.TXT");
            int lineIndex = 0;
            foreach (string line in lines)
            {
                if (line == "-"+name )
                {
                    return (lines[lineIndex + 1] == "*" + password);                      
                }
                lineIndex++;
            }
            return false;
        }
        private static void makeTexfile()
        {
            string[] lines = { "-name1", "*password1", ">", "-name2", "*password2", ">", "-name2", "*password2", ">" };
            System.IO.File.WriteAllLines(@"USER.TXT", lines);
        }
    }
}

Be advised that this code has been tested and runs. That does not mean that the suggested code is garanteed to work on any other user text file, especially if its structure is not the one I assumed. If the entries come from a user entry form then they should not contain any trailing blanks.

Anyway, if you have any more questions, then do not hesitate to ask.

Good luck.

I know that it would be easier to do a database but the user needs txt files.I will try this and tell you if works thank you again

Thanks it works i believe it was because i was reading it twice. So i'm new obviously with programming and this site why does it say i need to post five post to unlock full website, and how do i rank up on this site?

how do i rank up on this site?

Just upvoted your previous post if that makes you more happy.
I'm here to learn, ask questions and help other members if I can. After some time, your ranking will go up or down, wether you like it or not. Click your avatar or mine to find out more.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.