johnixx

johnixx

  • NA
  • 1
  • 1.4k

System.IndexOutOfRangeException Error

May 30 2016 10:14 AM
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.
 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Configuration;  
  5. using System.Data;  
  6. using System.Data.OleDb;  
  7. using System.Drawing;  
  8. using System.Linq;  
  9. using System.Text;  
  10. using System.Threading.Tasks;  
  11. using System.Windows.Forms;  
  12.   
  13. namespace Call_Logger  
  14. {  
  15.     public partial class Form2 : Form  
  16.     {  
  17.         private OleDbConnection con = new OleDbConnection();  
  18.         public Form2()  
  19.         {  
  20.             InitializeComponent();  
  21.             con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString();  
  22.         }  
  23.   
  24.         private void btn_Save_Click(object sender, EventArgs e)  
  25.         {  
  26.             try  
  27.             {  
  28.   
  29.                 con.Open();  
  30.                 OleDbCommand command = new OleDbCommand();  
  31.                 command.Connection = con;  
  32.                 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 + "')");  
  33.                 command.ExecuteNonQuery();  
  34.   
  35.                 MessageBox.Show("Data Saved Successfully");  
  36.                 con.Dispose();  
  37.                 this.Hide();  
  38.                 Dashboard dboard = new Dashboard();  
  39.                 dboard.ShowDialog();  
  40.                 con.Close();  
  41.             }  
  42.   
  43.             catch (Exception ex)  
  44.             {  
  45.                 MessageBox.Show("Error " + ex);  
  46.             }  
  47.         }  
  48.   
  49.         private void Form2_Load(object sender, EventArgs e)  
  50.         {  
  51.             try  
  52.             {  
  53.   
  54.                 con.Open();  
  55.                 OleDbCommand command = new OleDbCommand();  
  56.                 command.Connection = con;  
  57.                 command.CommandText = ("select * from Status");  
  58.                 command.CommandText = ("select * from Location");  
  59.                 command.CommandText = ("select * from CallType");  
  60.   
  61.                 OleDbDataReader reader = command.ExecuteReader();  
  62.                 while (reader.Read())  
  63.                 {  
  64.                     comboBox1.Items.Add(reader["StatusType"].ToString());  
  65.                     comboBox2.Items.Add(reader["LocationType"].ToString());  
  66.                     comboBox3.Items.Add(reader["CallingType"].ToString());  
  67.                 }  
  68.                 con.Close();  
  69.             }  
  70.   
  71.             catch (Exception ex)  
  72.             {  
  73.                 MessageBox.Show("Error " + ex);  
  74.             }  
  75.         }  
  76.   
  77.         private void groupBox1_Validating(object sender, CancelEventArgs e)  
  78.         {  
  79.             foreach (Control control in groupBox1.Controls)  
  80.             {  
  81.                 var lst = new List<string>() { "System.Windows.Forms.TextBox""System.Windows.Forms.ComboBox" };  
  82.                 if (!lst.Contains(control.GetType().ToString(), StringComparer.OrdinalIgnoreCase)) continue;  
  83.                 if (string.IsNullOrEmpty(control.Text) && string.IsNullOrEmpty(control.Text))  
  84.                  
  85.               {  
  86.                     MessageBox.Show(control.Name + " Can not be empty");  
  87.                 }  
  88.   
  89.             }  
  90.         }  
  91.   
  92.         private void groupBox3_Validating(object sender, CancelEventArgs e)  
  93.         {  
  94.             foreach (Control control in groupBox3.Controls)  
  95.             {  
  96.                 var lst = new List<string>() { "System.Windows.Forms.TextBox" };  
  97.                 if (!lst.Contains(control.GetType().ToString(), StringComparer.OrdinalIgnoreCase)) continue;  
  98.                 if (string.IsNullOrEmpty(control.Text) && string.IsNullOrEmpty(control.Text))  
  99.                 {  
  100.                     MessageBox.Show(control.Name + " Can not be empty");  
  101.                 }  
  102.   
  103.             }  
  104.         }  
  105.   
  106.         private void groupBox4_Validating(object sender, CancelEventArgs e)  
  107.         {  
  108.             foreach (Control control in groupBox4.Controls)  
  109.             {  
  110.                 var lst = new List<string>() { "System.Windows.Forms.TextBox""System.Windows.Forms.ComboBox" };  
  111.                 if (!lst.Contains(control.GetType().ToString(), StringComparer.OrdinalIgnoreCase)) continue;  
  112.                 if (string.IsNullOrEmpty(control.Text) && string.IsNullOrEmpty(control.Text))  
  113.                 {  
  114.                     MessageBox.Show(control.Name + " Can not be empty");  
  115.                 }  
  116.   
  117.             }  
  118.         }  
  119.   
  120.         private void groupBox7_Validating(object sender, CancelEventArgs e)  
  121.         {  
  122.             foreach (Control control in groupBox7.Controls)  
  123.             {  
  124.                 var lst = new List<string>() { "System.Windows.Forms.TextBox" };  
  125.                 if (!lst.Contains(control.GetType().ToString(), StringComparer.OrdinalIgnoreCase)) continue;  
  126.                 if (string.IsNullOrEmpty(control.Text) && string.IsNullOrEmpty(control.Text))  
  127.                 {  
  128.                     MessageBox.Show(control.Name + " Can not be empty");  
  129.                 }  
  130.   
  131.             }  
  132.         }  
  133.   
  134.         private void groupBox8_Validating(object sender, CancelEventArgs e)  
  135.         {  
  136.             foreach (Control control in groupBox8.Controls)  
  137.             {  
  138.                 var lst = new List<string>() { "System.Windows.Forms.TextBox""System.Windows.Forms.ComboBox" };  
  139.                 if (!lst.Contains(control.GetType().ToString(), StringComparer.OrdinalIgnoreCase)) continue;  
  140.                 if (string.IsNullOrEmpty(control.Text) && string.IsNullOrEmpty(control.Text))  
  141.                 {  
  142.                     MessageBox.Show(control.Name + " Can not be empty");  
  143.                 }  
  144.   
  145.             }  
  146.         }  
  147.   
  148.         private void btn_ViewRecords_Click(object sender, EventArgs e)  
  149.         {  
  150.             this.Hide();  
  151.             Dashboard dboard = new Dashboard();  
  152.             dboard.ShowDialog();  
  153.   
  154.         }  
  155.   
  156.           
  157.   
  158.           
  159.         }  
  160.   
  161.          
  162.   
  163.           
  164.   
  165.   
  166.   
  167.   
  168.     }  
 

Answers (3)