TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
johnixx
NA
1
1.5k
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.
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 Form
2
: Form
{
private OleDbConnection con = new OleDbConnection();
public Form
2
()
{
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 +
"','"
+ comboBox
3
.Text +
"','"
+ comboBox
2
.Text +
"','"
+ txtProb
1
.Text +
"','"
+ txtProb
2
.Text +
"','"
+ txtIncidentNo.Text +
"','"
+ txtCaller.Text +
"','"
+ txtCallFor.Text +
"','"
+ txtAssignedBy.Text +
"','"
+ txtAssignedDate.Text +
"','"
+ txtAssignedTime.Text +
"','"
+ txtFwdTo.Text +
"','"
+ txtContactPerson.Text +
"','"
+ txtFwdDate.Text +
"','"
+ txtFwdTime.Text +
"','"
+ txtAction
1
.Text +
"','"
+ txtAction
2
.Text +
"','"
+ txtNextStep
1
.Text +
"','"
+ txtNextStep
2
.Text +
"','"
+ txtClosedBy.Text +
"','"
+ txtClosedDate.Text +
"','"
+ txtCloseTime.Text +
"','"
+ txtRemarks
1
.Text +
"','"
+ txtRemarks
2
.Text +
"','"
+ comboBox
1
.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 Form
2
_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())
{
comboBox
1
.Items.Add(reader[
"StatusType"
].ToString());
comboBox
2
.Items.Add(reader[
"LocationType"
].ToString());
comboBox
3
.Items.Add(reader[
"CallingType"
].ToString());
}
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(
"Error "
+ ex);
}
}
private void groupBox
1
_Validating(object sender, CancelEventArgs e)
{
foreach (Control control in groupBox
1
.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 groupBox
3
_Validating(object sender, CancelEventArgs e)
{
foreach (Control control in groupBox
3
.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 groupBox
4
_Validating(object sender, CancelEventArgs e)
{
foreach (Control control in groupBox
4
.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 groupBox
7
_Validating(object sender, CancelEventArgs e)
{
foreach (Control control in groupBox
7
.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 groupBox
8
_Validating(object sender, CancelEventArgs e)
{
foreach (Control control in groupBox
8
.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();
}
}
}
Reply
Answers (
3
)
Real inteview quetions & Answers faced in interviews .net c#
About_C#_Corner_Book_Download