CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: System.IndexOutOfRangeException error.

  1. #1
    Join Date
    Dec 2015
    Posts
    2

    System.IndexOutOfRangeException error.

    So this is the error I am getting after working on a form that allows the user to insert some values in the table.
    The issue happens when I try to populate the combo boxes on the form upon the opening/Loading of the form.

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Configuration;
    using System.Data;
    using System.Data.OleDb;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace Call_Logger
    {
        public partial class Form2 : Form
        {
            private OleDbConnection con = new OleDbConnection();
            public Form2()
            {
                InitializeComponent();
                con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString();
            }
    
            private void btn_Save_Click(object sender, EventArgs e)
            {
                try
                {
    
                    con.Open();
                    OleDbCommand command = new OleDbCommand();
                    command.Connection = con;
                    command.CommandText = ("insert into CLTRTRN (CallNo, CallLogBy, CallLogDate, CallLogTime, CallType, CallLocation, ProblemDescription1, ProblemDescription2, IncidentNo, Caller, CallFor, AssignedBy, AssignedDate, AssignedTime, ForwardTo, ContactPerson, ForwardDate, ForwardTime, ActionTaken1, ActionTaken2, NextStep1, NextStep2, ClosedBy, CloseDate, CloseTime, Remarks1, Remarks2, Status) values ('" + txtLogBy.Text + "','" + txtLogDate.Text + "','" + txtLogTime.Text + "','" + comboBox3.Text + "','" + comboBox2.Text + "','" + txtProb1.Text + "','" + txtProb2.Text + "','" + txtIncidentNo.Text + "','" + txtCaller.Text + "','" + txtCallFor.Text + "','" + txtAssignedBy.Text + "','" + txtAssignedDate.Text + "','" + txtAssignedTime.Text + "','" + txtFwdTo.Text + "','" + txtContactPerson.Text + "','" + txtFwdDate.Text + "','" + txtFwdTime.Text + "','" + txtAction1.Text + "','" + txtAction2.Text + "','" + txtNextStep1.Text + "','" + txtNextStep2.Text + "','" + txtClosedBy.Text + "','" + txtClosedDate.Text + "','" + txtCloseTime.Text + "','" + txtRemarks1.Text + "','" + txtRemarks2.Text + "','" + comboBox1.Text + "')");
                    command.ExecuteNonQuery();
    
                    MessageBox.Show("Data Saved Successfully");
                    con.Dispose();
                    this.Hide();
                    Dashboard dboard = new Dashboard();
                    dboard.ShowDialog();
                    con.Close();
                }
    
                catch (Exception ex)
                {
                    MessageBox.Show("Error " + ex);
                }
            }
    
            private void Form2_Load(object sender, EventArgs e)
            {
                try
                {
    
                    con.Open();
                    OleDbCommand command = new OleDbCommand();
                    command.Connection = con;
                    command.CommandText = ("select * from Status");
                    command.CommandText = ("select * from Location");
                    command.CommandText = ("select * from CallType");
    
                    OleDbDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        comboBox1.Items.Add(reader["StatusType"].ToString());
                        comboBox2.Items.Add(reader["LocationType"].ToString());
                        comboBox3.Items.Add(reader["CallingType"].ToString());
                    }
                    con.Close();
                }
    
                catch (Exception ex)
                {
                    MessageBox.Show("Error " + ex);
                }
            }
    
            private void groupBox1_Validating(object sender, CancelEventArgs e)
            {
                foreach (Control control in groupBox1.Controls)
                {
                    var lst = new List<string>() { "System.Windows.Forms.TextBox", "System.Windows.Forms.ComboBox" };
                    if (!lst.Contains(control.GetType().ToString(), StringComparer.OrdinalIgnoreCase)) continue;
                    if (string.IsNullOrEmpty(control.Text) && string.IsNullOrEmpty(control.Text))
                   
                  {
                        MessageBox.Show(control.Name + " Can not be empty");
                    }
    
                }
            }
    
            private void groupBox3_Validating(object sender, CancelEventArgs e)
            {
                foreach (Control control in groupBox3.Controls)
                {
                    var lst = new List<string>() { "System.Windows.Forms.TextBox" };
                    if (!lst.Contains(control.GetType().ToString(), StringComparer.OrdinalIgnoreCase)) continue;
                    if (string.IsNullOrEmpty(control.Text) && string.IsNullOrEmpty(control.Text))
                    {
                        MessageBox.Show(control.Name + " Can not be empty");
                    }
    
                }
            }
    
            private void groupBox4_Validating(object sender, CancelEventArgs e)
            {
                foreach (Control control in groupBox4.Controls)
                {
                    var lst = new List<string>() { "System.Windows.Forms.TextBox", "System.Windows.Forms.ComboBox" };
                    if (!lst.Contains(control.GetType().ToString(), StringComparer.OrdinalIgnoreCase)) continue;
                    if (string.IsNullOrEmpty(control.Text) && string.IsNullOrEmpty(control.Text))
                    {
                        MessageBox.Show(control.Name + " Can not be empty");
                    }
    
                }
            }
    
            private void groupBox7_Validating(object sender, CancelEventArgs e)
            {
                foreach (Control control in groupBox7.Controls)
                {
                    var lst = new List<string>() { "System.Windows.Forms.TextBox" };
                    if (!lst.Contains(control.GetType().ToString(), StringComparer.OrdinalIgnoreCase)) continue;
                    if (string.IsNullOrEmpty(control.Text) && string.IsNullOrEmpty(control.Text))
                    {
                        MessageBox.Show(control.Name + " Can not be empty");
                    }
    
                }
            }
    
            private void groupBox8_Validating(object sender, CancelEventArgs e)
            {
                foreach (Control control in groupBox8.Controls)
                {
                    var lst = new List<string>() { "System.Windows.Forms.TextBox", "System.Windows.Forms.ComboBox" };
                    if (!lst.Contains(control.GetType().ToString(), StringComparer.OrdinalIgnoreCase)) continue;
                    if (string.IsNullOrEmpty(control.Text) && string.IsNullOrEmpty(control.Text))
                    {
                        MessageBox.Show(control.Name + " Can not be empty");
                    }
    
                }
            }
    
            private void btn_ViewRecords_Click(object sender, EventArgs e)
            {
                this.Hide();
                Dashboard dboard = new Dashboard();
                dboard.ShowDialog();
    
            }
    
            
    
            
            }
    
           
    
            
    
    
    
    
        }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,316

    Re: System.IndexOutOfRangeException error.

    Where does the error occur?

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,268

    Re: System.IndexOutOfRangeException error.

    Set breakpoints and step through your code. By doing this it should give you some sort of a clue of where your error may be.
    [SIGPIC][/SIGPIC]
    All my Articles
    Hannes

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)