Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Index was outside the bounds of array at trimmed text

I have created a Windows Form App which is connected to a database and users enter specific values in it. The problem I'm facing right now is that I created a search, which works like charm. But, users will always search by Name and Surname, and I wanted to trim this search to be more accuarate. It works, but when I enter only Name or only Surname, I'd like to show a massageBox to say that they didn't enter properly. So I wrote this, but if I type only one word, it crashes and shows this error: Index was outside the bounds of array. Thanks !

private void button1_Click(object sender, EventArgs e)
{
    if (textBox1.Text.Length != 0)
    {
        var numePrenume = textBox1.Text.Trim().Split(' ');
        var nume = numePrenume[0];
        var prenume = numePrenume[1];

    if (nume.Length > 0 || prenume.Length > 0)
    {
        var connString = @"Data Source=C:\Users\Andrei\Documents\Visual Studio 2010\Projects\Stellwag\Stellwag\Angajati.sdf";
        using (var conn = new SqlCeConnection(connString))
        {
        }
    }

    //some code
}
like image 742
Ezekiel Avatar asked Feb 10 '23 06:02

Ezekiel


2 Answers

Check your array length will fix your issue :

 if (textBox1.Text.Length != 0)
            {

                var numePrenume = textBox1.Text.Trim().Split(' ');
                if (numePrenume.Length >= 1)
                {
                    var nume = numePrenume[0];
                    var prenume = numePrenume[1];

                    if (nume.Length > 0 || prenume.Length > 0)
                    {
                        var connString = @"Data Source=C:\Users\Andrei\Documents\Visual Studio 2010\Projects\Stellwag\Stellwag\Angajati.sdf";
                        using (var conn = new SqlCeConnection(connString))
                        {
                        }
                    }
                }
                else
                    // some code here
            }
like image 97
Moez Rebai Avatar answered Feb 12 '23 11:02

Moez Rebai


You'll need to check the length of the array returned by Split(). In addition it is a good idea to add StringSplitOptions.RemoveEmptyEntries to avoid getting more than two parts if the user types multiple spaces between the name parts.

Consider this example:

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

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var name = "   name  ";

            var nameParts = name.Trim().Split(new []{' '}, StringSplitOptions.RemoveEmptyEntries);

            if (nameParts.Length < 2)
            {
                Console.WriteLine("You've only entered one name");
            }
            else
            {
                Console.WriteLine("First part: {0}", nameParts[0]);
                Console.WriteLine("Second part: {0}", nameParts[1]);
            }
        }
    }
}
like image 31
Micke Avatar answered Feb 12 '23 09:02

Micke