none
Login Page C# Var Password Exception UnHandled RRS feed

  • Question

  • Hey I was coding a login forum and then it said Exception Unhandled

    System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

    I'm using c#

    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.Threading;
    using System.IO;
    using System.Net;
    using static System.Windows.Forms.VisualStyles.VisualStyleElement;
    namespace Deluxe_V2_Rewrite
    {
        public partial class Form3 : MaterialSkin.Controls.MaterialForm
        {
            static string url = "https://www.dropbox.com/s/qa8cliq4eo9gsg3/deluxewhitelist.txt.txt?dl=0Raw=1";
            static List<string> userdatabase = new List<string>();
            static int LoggedIn = 0;
            static int Loaded = 0;
            private string Username;
            private string Password;
            public Form3()
            {
                InitializeComponent();
                DisableAll();
                LoadedTimer.Start();
                ThreadPool.QueueUserWorkItem(LoadUsers);
                Thread t1 = new Thread(LoadUsers);
                t1.Start();
            }
            private void Form3_Load(object sender, EventArgs e)
            {
            }
            private void LoadUsers(object state)
            {
                // Downloads each line from url and adds it to da list - userdatabase
                try
                {
                    var client = new WebClient();
                    using (var stream = client.OpenRead(url))
                    using (var reader = new StreamReader(stream))
                    {
                        string line;
                        while ((line = reader.ReadLine()) != null)
                        {
                            userdatabase.Add(line);
                        }
                    }
                    Loaded = 1;
                }

                catch
                {
                    MessageBox.Show("random error force closing");
                    this.Close();
                }
            }
            private void EnableAll()
            {
                foreach (Control ctrl in this.Controls)
                {
                    ctrl.Enabled = true;
                }
            }
            private void DisableAll()
            {
                foreach (Control ctrl in this.Controls)
                {
                    ctrl.Enabled = false;
                }
            }
            private void MaterialFlatButton1_Click(object sender, EventArgs e)
            {
                if (LoggedIn == 1)
                {
                    // if clicked another time
                    LoggedIn = 0;
                }
                foreach (string user in userdatabase)
                {
                    var Username = user.Split('|')[0];
                    var Password = user.Split('|')[1];
                    { }
                    if (Username == txtuser.Text && Password == txtpass.Text)
                    {
                        LoggedIn = 1;
                        MessageBox.Show("Logged in, Welcome!");
                        Form1 form1 = new Form1();
                        form1.Show();
                        this.Hide();
                    }
                }
                if (LoggedIn == 0)
                {
                    MessageBox.Show("invaild user or pass, or something else happend");
                }
            }

            private void LoadedTimer_Tick(object sender, EventArgs e)
            {
                if (Loaded == 1)
                {
                    LoadedTimer.Stop();
                    EnableAll();
                }
            }
        }
    }
               

    Everything is green and works, but when I hit start and then I put in a correct username and password nothing happened

    Then if I hit login again itll say that exception error

    I'm using dropbox to store my .txt

    Friday, January 19, 2018 9:10 PM

All replies

  • There are some problems with your question.

    1. You didn't tell us which line gets the exception.
    2. There's no "Start" button here.
    3. There's no "Login" button here.
    4. Your code cannot be run as is, because we are missing the other files.

    If I had to take a wild guess, I'd guess that there's a line in your user database that does not contain a "|", so that the expression "user.Split('|')[1]" fails.  Do you possibly have a blank line at the end?

    If that's not it, then you'll need to give us some more information.


    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Friday, January 19, 2018 9:43 PM
  •  var Password = user.Split('|')[1];

    That gets the exception every time

    I meant the login was submit

            private void MaterialFlatButton1_Click(object sender, EventArgs e)
            {
                if (LoggedIn == 1)
                {
                    // if clicked another time

                    LoggedIn = 0;
                }

    Saturday, January 20, 2018 2:13 AM
  • Hi john carver12,

    According to your error message and related, I would suggest that you could check if the value of variable named user does not have '|' in it. I would suggest that you could modify your code like this:

     string[] temp = user.Split('|');
    if(temp.Count()>1)
    { 
       var password =  user.Split('|')[1];
    }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 22, 2018 3:18 AM
    Moderator