How to solve the Index out of range exception?


I am getting out of range exception for this code-

public ActionResult AttendanceTable() {
 //var tbl_emp_attendance = db.tbl_emp_attendance.Include(t => t.tbl_employee);
 var attendanceTable = db.tbl_emp_attendance
  .OrderBy(a => a.date_and_time).ToList();

 var count = attendanceTable.Count;

 for (var i = 0; i < count; i = i + 2) {

  if (attendanceTable[i].date_and_time.Value.Date == attendanceTable[i + 1].date_and_time.Value.Date &&
   attendanceTable[i].employee_id == attendanceTable[i + 1].employee_id) {
   //Check which value is less using Time now,as it is sorted first value will be less than second
   if (attendanceTable[i].date_and_time.Value.TimeOfDay <
    attendanceTable[i + 1].date_and_time.Value.TimeOfDay) {


    //OUttime Greater one
    var inTime = attendanceTable[i].date_and_time.Value.TimeOfDay;
    var outTime = attendanceTable[i + 1].date_and_time.Value.TimeOfDay;

    var actualInTime = attendanceTable[i].date_and_time.Value.Date + new TimeSpan(10, 00, 00);
    var actualOutTime = attendanceTable[i].date_and_time.Value.Date + new TimeSpan(17, 00, 00);
    var actualWorkingHour = actualOutTime - actualInTime;
    if (inTime < actualInTime.TimeOfDay) {
     inTime = actualInTime.TimeOfDay;
    }
    var employeeworkingHour = outTime - inTime;



    var overTime = outTime - actualOutTime.TimeOfDay;

    var lateTime = actualInTime.TimeOfDay - inTime;


    var actualMonthlyWorkingMinute = (int) actualWorkingHour.TotalMinutes * 30;
    var mainSalary = attendanceTable[i].tbl_employee.emp_gross_amount;
    var mainPerminuteSalary = mainSalary / ((int) actualMonthlyWorkingMinute);
    var perHoursalary = (int) employeeworkingHour.TotalMinutes * mainPerminuteSalary;
    var overtimeHourlySalary = (int) overTime.TotalMinutes * mainPerminuteSalary;
    var deductedHourlySalary = (int) lateTime.TotalMinutes * mainPerminuteSalary;
    var actualSalary = ((int) employeeworkingHour.TotalMinutes) * mainPerminuteSalary * 30;
    if (actualSalary < mainSalary) {
     var deductedSalary = mainSalary - actualSalary;
    }
    if (actualSalary > mainSalary) {
     var overTimeSalary = actualSalary - mainSalary;
    }
    attendanceTable[i].emp_inTime = inTime;

    attendanceTable[i].emp_outTime = outTime;




    attendanceTable[i].emp_workingHour = employeeworkingHour;
    attendanceTable[i].emp_calculatedSalary = perHoursalary;
    attendanceTable[i].emp_overTime = overTime;
    attendanceTable[i].emp_late = lateTime.Duration();
    attendanceTable[i].emp_overtime_hourly_salary = overtimeHourlySalary;
    attendanceTable[i].emp_late_hourly_salary = deductedHourlySalary;



   }
  }
 }
 return View(attendanceTable.DistinctBy(a => a.date_and_time.Value.Date));
}

I know what is the problem. Problem is when I am entering a value it is checking and comparing with two values of same employee_id. Now when I am entering another value in database, it can't get the (i+1) element value because it doesn't exist. When I enter another value, it works fine. Now how can I solve it? 


Asked by:- SalmanZahir
0
: 2997 At:- 2/13/2018 12:43:53 PM
asp.net mvc c#







2 Answers
profileImage Answered by:- vikas_jk

You are accessing value which outside Array Index, or you can say array Length, you need to adjust code so that your array doesn't call out of length data.

Suppose If i have Array of Length 3, then To handle this type of exception, I will code something like this

int[] arr1 = new int[] { 3, 4, 5 };

for (int index = 0; index < arr1.Length; index++)
{
    Console.WriteLine(arr1[index]);
}

//here index < arr1.Length helps to stay away from Index out of range error

According to your description "when I am entering another value in database, it can't get the (i+1) element value because it doesn't exist"

When you enter another value, offcourse database is changed, now you need to change code according to it, please consider changing your code according to your database values and handle Index out of range as described above.

1
At:- 2/13/2018 4:01:21 PM Updated at:- 2/14/2018 9:47:08 AM
Thank you. I solved the problem using foreach loop within another foreach loop. This is the code, public ActionResult AttendanceTable() { var att = db.tbl_emp_attendance.OrderBy(a => a.date_and_time).ToList(); foreach (var employee in att) { var emp = db.tbl_emp_attendance.OrderBy(a=>a.date_and_time).ToList(); foreach (var emp1 in emp) { if (employee.date_and_time.Value.Date == emp1.date_and_time.Value.Date && employee.employee_id == emp1.employee_id) { var actualInTime = employee.date_and_time.Value.Date + new TimeSpan(10, 00, 00); var actualOutTime = employee.date_and_time.Value.Date + new TimeSpan(17, 00, 00); var actualWorkingHour = actualOutTime - actualInTime; if (employee.date_and_time.Value.TimeOfDay < emp1.date_and_time.Value.TimeOfDay) { employee.emp_inTime = employee.date_and_time.Value.TimeOfDay; employee.emp_outTime = emp1.date_and_time.Value.TimeOfDay; } else if (employee.date_and_time.Value.TimeOfDay == emp1.date_and_time.Value.TimeOfDay) { employee.emp_inTime = employee.date_and_time.Value.TimeOfDay; employee.emp_outTime = actualOutTime.TimeOfDay; } else { employee.emp_inTime = emp1.date_and_time.Value.TimeOfDay; employee.emp_outTime = employee.date_and_time.Value.TimeOfDay; } var inTime = employee.emp_inTime; var outTime = employee.emp_outTime; if (inTime < actualInTime.TimeOfDay) { inTime = actualInTime.TimeOfDay; } var employeeworkingHour = outTime- inTime; var overTime = outTime - actualOutTime.TimeOfDay; var lateTime = actualInTime.TimeOfDay - inTime; var actualMonthlyWorkingMinute = (int)actualWorkingHour.TotalMinutes * 30; var mainSalary = employee.tbl_employee.emp_gross_amount; var mainPerminuteSalary = mainSalary / ((int)actualMonthlyWorkingMinute); var perHoursalary = (int)employeeworkingHour.TotalMinutes * mainPerminuteSalary; var overtimeHourlySalary = (int)overTime.TotalMinutes * mainPerminuteSalary; var deductedHourlySalary = (int)lateTime.TotalMinutes * mainPerminuteSalary; var actualSalary = ((int)employeeworkingHour.TotalMinutes) * mainPerminuteSalary * 30; employee.emp_workingHour = employeeworkingHour; employee.emp_calculatedSalary = perHoursalary; employee.emp_overTime = overTime; employee.emp_late = lateTime.Duration(); employee.emp_overtime_hourly_salary = overtimeHourlySalary; employee.emp_late_hourly_salary = deductedHourlySalary; } } } return View(att.DistinctBy(a=>a.date_and_time.Value.Date)); } 0
By : SalmanZahir - at :- 2/14/2018 6:45:23 AM


profileImage Answered by:- SalmanZahir

Update:

Solved the problem by putting a foreach loop within another foreach loop-

public ActionResult AttendanceTable()
{
var att = db.tbl_emp_attendance.OrderBy(a => a.date_and_time).ToList();

foreach (var employee in att)
{
var emp = db.tbl_emp_attendance.OrderBy(a=>a.date_and_time).ToList();
foreach (var emp1 in emp)
{
if (employee.date_and_time.Value.Date == emp1.date_and_time.Value.Date &&
employee.employee_id == emp1.employee_id)
{
var actualInTime = employee.date_and_time.Value.Date + new TimeSpan(10, 00, 00);
var actualOutTime = employee.date_and_time.Value.Date + new TimeSpan(17, 00, 00);
var actualWorkingHour = actualOutTime - actualInTime;
if (employee.date_and_time.Value.TimeOfDay < emp1.date_and_time.Value.TimeOfDay)
{
employee.emp_inTime = employee.date_and_time.Value.TimeOfDay;
employee.emp_outTime = emp1.date_and_time.Value.TimeOfDay;
}
else if (employee.date_and_time.Value.TimeOfDay == emp1.date_and_time.Value.TimeOfDay)
{
employee.emp_inTime = employee.date_and_time.Value.TimeOfDay;
employee.emp_outTime = actualOutTime.TimeOfDay;
}
else
{
employee.emp_inTime = emp1.date_and_time.Value.TimeOfDay;
employee.emp_outTime = employee.date_and_time.Value.TimeOfDay;
}

var inTime = employee.emp_inTime;
var outTime = employee.emp_outTime;
if (inTime < actualInTime.TimeOfDay)
{
inTime = actualInTime.TimeOfDay;
}
var employeeworkingHour = outTime- inTime;

var overTime = outTime - actualOutTime.TimeOfDay;

var lateTime = actualInTime.TimeOfDay - inTime;


var actualMonthlyWorkingMinute = (int)actualWorkingHour.TotalMinutes * 30;
var mainSalary = employee.tbl_employee.emp_gross_amount;
var mainPerminuteSalary = mainSalary / ((int)actualMonthlyWorkingMinute);
var perHoursalary = (int)employeeworkingHour.TotalMinutes * mainPerminuteSalary;
var overtimeHourlySalary = (int)overTime.TotalMinutes * mainPerminuteSalary;
var deductedHourlySalary = (int)lateTime.TotalMinutes * mainPerminuteSalary;
var actualSalary = ((int)employeeworkingHour.TotalMinutes) * mainPerminuteSalary * 30;

employee.emp_workingHour = employeeworkingHour;
employee.emp_calculatedSalary = perHoursalary;
employee.emp_overTime = overTime;
employee.emp_late = lateTime.Duration();
employee.emp_overtime_hourly_salary = overtimeHourlySalary;
employee.emp_late_hourly_salary = deductedHourlySalary;

}
}
}
return View(att.DistinctBy(a=>a.date_and_time.Value.Date));
}

 

0
At:- 2/14/2018 6:46:34 AM Updated at:- 2/14/2018 8:40:57 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use