4 Replies - 1713 Views - Last Post: 15 February 2012 - 08:58 AM Rate Topic: -----

#1 Zaraki=Death  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-February 12

Index Out Of Range Exception

Posted 15 February 2012 - 05:09 AM

Hello

Need some help with coding please!!!!!!!!!!

I am creating an Airline application with 90 seats. Information needs to be inserted into the database and then read it into the application so the button will display red and the enabled property will be disabled (enabled = false) so the button can’t be clicked again.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Deployment;
using System.Diagnostics;

namespace Airline
{
    public partial class Form1 : Form
    {
        SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Airline;Integrated Security=True");
        SqlCommand com = new SqlCommand();
        DataTable dTable = new DataTable();


        public Form1()
        {
            InitializeComponent();            
        }

        private void btnBooking_Click(object sender, EventArgs e)
        {
            com.Connection = conn;
            com.CommandText = "INSERT INTO AirlineTable Values ('" + txbName.Text + "' , '" + txbSurname.Text + "' , '" + txbId.Text + "' , '" + txbWeight.Text + "', '" + txbSeat.Text + "')";
            conn.Open();
            com.ExecuteNonQuery();
            SqlDataAdapter dataA = new SqlDataAdapter("SELECT * FROM AirlineTable", conn);
            DataTable dt = new DataTable();
            dataA.Fill(dt);
            dataGridView1.DataSource = dt;
            conn.Close();
            txbName.Clear();
            txbSurname.Clear();
            txbId.Clear();
            txbWeight.Clear();
            MessageBox.Show("Booking Made Succesfully");

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string seat = "";
            DataTable newDataTable = new DataTable();
            SqlDataAdapter NewDataA = new SqlDataAdapter("SELECT * FROM AirlineTable", conn);
            NewDataA.Fill(newDataTable);
            dataGridView1.DataSource = newDataTable;
            for (int i = 0; i < 91; i++)
            {
                if (dataGridView1.SelectedRows[i].Cells[4].Value.ToString() != "")               
                {
                    seat = "btn" + dataGridView1.SelectedRows[i].Cells[4].Value.ToString();

                    Button ZZ = new Button();
                                    
                    btn1.BackColor = Color.Red;                    
                }
            }



        }

        private void btn1_MouseHover(object sender, EventArgs e)
        {
            ToolTip tt = new ToolTip();
            tt.Show("Meiring", this.btn1, 10000);
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            if (btn1.Enabled == true)
            {
                txbSeat.Text = btn1.Text;
                btn1.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            if (btn2.Enabled == true)
            {
                txbSeat.Text = btn2.Text;
                btn2.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            if (btn3.Enabled == true)
            {
                txbSeat.Text = btn3.Text;
                btn3.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            if (btn4.Enabled == true)
            {
                txbSeat.Text = btn4.Text;
                btn4.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            if (btn5.Enabled == true)
            {
                txbSeat.Text = btn5.Text;
                btn5.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            if (btn6.Enabled == true)
            {
                txbSeat.Text = btn6.Text;
                btn6.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            if (btn7.Enabled == true)
            {
                txbSeat.Text = btn7.Text;
                btn7.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            if (btn8.Enabled == true)
            {
                txbSeat.Text = btn8.Text;
                btn8.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn9_Click(object sender, EventArgs e)
        {
            if (btn9.Enabled == true)
            {
                txbSeat.Text = btn9.Text;
                btn9.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn10_Click(object sender, EventArgs e)
        {
            if (btn10.Enabled == true)
            {
                txbSeat.Text = btn10.Text;
                btn10.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn11_Click(object sender, EventArgs e)
        {
            if (btn11.Enabled == true)
            {
                txbSeat.Text = btn11.Text;
                btn11.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn12_Click(object sender, EventArgs e)
        {
            if (btn12.Enabled == true)
            {
                txbSeat.Text = btn12.Text;
                btn12.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn13_Click(object sender, EventArgs e)
        {
            if (btn13.Enabled == true)
            {
                txbSeat.Text = btn13.Text;
                btn13.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn14_Click(object sender, EventArgs e)
        {
            if (btn14.Enabled == true)
            {
                txbSeat.Text = btn14.Text;
                btn14.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn15_Click(object sender, EventArgs e)
        {
            if (btn15.Enabled == true)
            {
                txbSeat.Text = btn15.Text;
                btn15.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn16_Click(object sender, EventArgs e)
        {
            if (btn16.Enabled == true)
            {
                txbSeat.Text = btn16.Text;
                btn16.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn17_Click(object sender, EventArgs e)
        {
            if (btn17.Enabled == true)
            {
                txbSeat.Text = btn17.Text;
                btn17.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn18_Click(object sender, EventArgs e)
        {
            if (btn18.Enabled == true)
            {
                txbSeat.Text = btn18.Text;
                btn18.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn19_Click(object sender, EventArgs e)
        {
            if (btn19.Enabled == true)
            {
                txbSeat.Text = btn19.Text;
                btn19.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn20_Click(object sender, EventArgs e)
        {
            if (btn20.Enabled == true)
            {
                txbSeat.Text = btn20.Text;
                btn20.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn21_Click(object sender, EventArgs e)
        {
            if (btn21.Enabled == true)
            {
                txbSeat.Text = btn21.Text;
                btn21.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn22_Click(object sender, EventArgs e)
        {
            if (btn22.Enabled == true)
            {
                txbSeat.Text = btn22.Text;
                btn22.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn23_Click(object sender, EventArgs e)
        {
            if (btn23.Enabled == true)
            {
                txbSeat.Text = btn23.Text;
                btn23.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn24_Click(object sender, EventArgs e)
        {
            if (btn24.Enabled == true)
            {
                txbSeat.Text = btn24.Text;
                btn24.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn25_Click(object sender, EventArgs e)
        {
            if (btn25.Enabled == true)
            {
                txbSeat.Text = btn25.Text;
                btn25.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn26_Click(object sender, EventArgs e)
        {
            if (btn26.Enabled == true)
            {
                txbSeat.Text = btn26.Text;
                btn26.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn27_Click(object sender, EventArgs e)
        {
            if (btn27.Enabled == true)
            {
                txbSeat.Text = btn27.Text;
                btn27.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn28_Click(object sender, EventArgs e)
        {
            if (btn28.Enabled == true)
            {
                txbSeat.Text = btn28.Text;
                btn28.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn29_Click(object sender, EventArgs e)
        {
            if (btn29.Enabled == true)
            {
                txbSeat.Text = btn29.Text;
                btn29.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn30_Click(object sender, EventArgs e)
        {
            if (btn30.Enabled == true)
            {
                txbSeat.Text = btn30.Text;
                btn30.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn31_Click(object sender, EventArgs e)
        {
            if (btn31.Enabled == true)
            {
                txbSeat.Text = btn31.Text;
                btn31.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn32_Click(object sender, EventArgs e)
        {
            if (btn32.Enabled == true)
            {
                txbSeat.Text = btn32.Text;
                btn32.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn33_Click(object sender, EventArgs e)
        {
            if (btn33.Enabled == true)
            {
                txbSeat.Text = btn33.Text;
                btn33.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn34_Click(object sender, EventArgs e)
        {
            if (btn34.Enabled == true)
            {
                txbSeat.Text = btn34.Text;
                btn34.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn35_Click(object sender, EventArgs e)
        {
            if (btn35.Enabled == true)
            {
                txbSeat.Text = btn35.Text;
                btn35.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn36_Click(object sender, EventArgs e)
        {
            if (btn36.Enabled == true)
            {
                txbSeat.Text = btn36.Text;
                btn36.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn37_Click(object sender, EventArgs e)
        {
            if (btn37.Enabled == true)
            {
                txbSeat.Text = btn37.Text;
                btn37.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn38_Click(object sender, EventArgs e)
        {
            if (btn38.Enabled == true)
            {
                txbSeat.Text = btn38.Text;
                btn38.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn39_Click(object sender, EventArgs e)
        {
            if (btn39.Enabled == true)
            {
                txbSeat.Text = btn39.Text;
                btn39.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn40_Click(object sender, EventArgs e)
        {
            if (btn40.Enabled == true)
            {
                txbSeat.Text = btn40.Text;
                btn40.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn41_Click(object sender, EventArgs e)
        {
            if (btn41.Enabled == true)
            {
                txbSeat.Text = btn41.Text;
                btn41.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn42_Click(object sender, EventArgs e)
        {
            if (btn42.Enabled == true)
            {
                txbSeat.Text = btn42.Text;
                btn42.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn43_Click(object sender, EventArgs e)
        {
            if (btn43.Enabled == true)
            {
                txbSeat.Text = btn43.Text;
                btn43.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn44_Click(object sender, EventArgs e)
        {
            if (btn44.Enabled == true)
            {
                txbSeat.Text = btn44.Text;
                btn44.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn45_Click(object sender, EventArgs e)
        {
            if (btn45.Enabled == true)
            {
                txbSeat.Text = btn45.Text;
                btn45.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn46_Click(object sender, EventArgs e)
        {
            if (btn46.Enabled == true)
            {
                txbSeat.Text = btn46.Text;
                btn46.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn47_Click(object sender, EventArgs e)
        {
            if (btn47.Enabled == true)
            {
                txbSeat.Text = btn47.Text;
                btn47.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void btn48_Click(object sender, EventArgs e)
        {
            if (btn48.Enabled == true)
            {
                txbSeat.Text = btn48.Text;
                btn48.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (dataGridView1.SelectedCells[4].Value.ToString() != "")
            {
                btn2.BackColor = Color.Red;
            }


        }
    }
}




Take a look at my coding and please tell me where I went wrong. I will attach a picture to show you how the interface looks as well.
And if I try to run the application this is the Error that I get:

Index was out if range. Must be non-negative and less than the size of the collection.
Parameter name: index.

Attached image(s)

  • Attached Image
  • Attached Image
  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Index Out Of Range Exception

#2 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: Index Out Of Range Exception

Posted 15 February 2012 - 05:31 AM

First, rename your controls to something meaningful.

Debugging walkthrough:

Quote

System.ArgumentOutOfRangeException: Index was out of range...at Airline.Form1.Form1_Load(Object sender, EventArgs e) in I:\New folder\Airline\Airline\Form1.cs:line 57

So at line 57 you are trying to reference an index in a collection, but that index does not exist. Let's look at line 57.

Quote

if (dataGridView1.SelectedRows[i].Cells[4].Value.ToString() != "")

This code is in the FormLoad event handler (this code should be in a method that is called by the event handler). I would put a breakpoint at this code. The variable "i" is probably greater than the number of SelectedRows. Since this is the FormLoad event, I doubt there is more than 1 selected row.

This post has been edited by Robin19: 15 February 2012 - 05:46 AM

Was This Post Helpful? 2
  • +
  • -

#3 Zaraki=Death  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-February 12

Re: Index Out Of Range Exception

Posted 15 February 2012 - 06:05 AM

Thanks for the reply. :D

Trying the code know, hope I do it correct.
Isn’t there an easier way to let the button display red if it is booked and I first only wanted to disable the button but it needs to show a popup box with the client name and surname when you hover over the button and it works but if the button is disabled the popup box doesn’t work and it still needs to display.

Thanks in advance....
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Index Out Of Range Exception

Posted 15 February 2012 - 06:28 AM

Your logic is out of wack...

        private void btn1_Click(object sender, EventArgs e)
        {
            if (btn1.Enabled == true)
            {
                txbSeat.Text = btn1.Text;
                btn1.BackColor = Color.Red;
                //btn1.Enabled = false;
            }
        }



If btn1 is disabled, then the Click event won't fire anyway. There is no reason to make the if (btn1.Enabled == true) check because the Click event won't fire if it's not enabled.

You could take away every single one of those Click event handlers, and make one.

private void Button_Click(object sender, EventArgs e)
{
     Button btn = sender as Button;
     txbSeat.Text = btn.Text;
     btn.BackColor = Color.Red;
     //btn.Enabled = false;
}



You could then select every "seat" button on the form, go to the Properties window, click on the Events tab(lightning bolt), and click the drop-down for the Click event. You will see Button_Click listed. Click on it, and every button will have the same Click event.

As for showing a ToolTip for a disabled button, you could try this out...

http://stackoverflow...disabled-button
Was This Post Helpful? 1
  • +
  • -

#5 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Index Out Of Range Exception

Posted 15 February 2012 - 08:58 AM

Line 55 reads "for (int i = 0; i < 91; i++)", so your index is going from 0 to 90. That's 91 spots. And you have how many seats?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1