By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,815 Members | 1,143 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,815 IT Pros & Developers. It's quick & easy.

Argument Out Of Range Exception was unhandled.

P: 2
I want to display daily attendence report of employees. But I keep getting this error in this line( DataGridView1.Rows(z - 1).Cells("WORK").Value = CInt(HOURSWORKED / 60) ) as Argument Out Of Range Exception. Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index.

Expand|Select|Wrap|Line Numbers
  1. Private Sub CALCULATELOGINOITS()
  2.         Dim HALFDAY1 As Integer = 0
  3.         Dim HALFDAY2 As Integer = 0
  4.         Dim NOOFLOGS As Integer = 0
  5.         Dim j As Integer = 0
  6.         Dim dh As Integer = 0
  7.         Dim dsp As DataSet = DataLayer.ExecuateDataSet("select * from TIMEATENDENCEVIEW1 where eid=" & cmbemployee.SelectedValue & " order by orderno")
  8.         If dsp.Tables(0).Rows.Count > 0 Then
  9.             For Each row As DataRow In dsp.Tables(0).Rows
  10.                 duty = duty & row("ScheduleFrom").ToString & " to " & row("ScheduleTO").ToString & "  "
  11.                 gracetime = gracetime + row("gracetime")
  12.                 dh = DateDiff(DateInterval.Minute, row("ScheduleFrom"), row("ScheduleTO"))
  13.                 If dh < 0 Then dh = DateDiff(DateInterval.Minute, row("ScheduleFrom"), DateAdd(DateInterval.Day, 1, row("ScheduleTO")))
  14.                 dutyhours = dutyhours + dh
  15.                 j = j + 1
  16.                 ListBox1.Items.Add(row("ScheduleFrom"))
  17.                 ListBox2.Items.Add(row("ScheduleTO"))
  18.             Next
  19.             gracetime = gracetime / j
  20.             lblgraceperiod.Text = gracetime
  21.             lbldayschedule.Text = duty
  22.             lbldutyhours.Text = CInt(dutyhours / 60)
  23.             HALFDAY1 = CInt(Val(lbldutyhours.Text) * 0.33)
  24.             HALFDAY2 = CInt(Val(lbldutyhours.Text) * 0.75)
  25.             For i = 1 To 31
  26.                 Try
  27.                     Dim s1 As String = cmbmonth.SelectedIndex + 1 & "/" & i & "/" & txtyear.Text
  28.                     Dim dd As Date = Convert.ToDateTime(s1) 'cDate(txtyear.Text & "-" & cmbmonth.SelectedIndex + 1 & "-" & i & " 12:00:00")
  29.                     s1 = dd.DayOfWeek.ToString()
  30.                     DataGridView1.Rows.Add(s1.Substring(0, 3) & " " & cmbmonth.SelectedIndex + 1 & "-" & i & "-" & txtyear.Text, "", 0, 0, 0, 0, 0, 0)
  31.                 Catch ex As Exception
  32.                 End Try
  33.             Next
  34.         End If
  35.         Dim logs As String = ""
  36.         Dim HOURSWORKED As Integer = 0
  37.         Dim latecomming As Integer = 0
  38.         Dim TIME1 As String = ""
  39.         Dim TIME2 As String = ""
  40.         Dim date01 As Date
  41.         Dim date02 As Date
  42.         For z = 1 To 31
  43.             date01 = Now
  44.             date02 = Now
  45.             Try
  46.                 date01 = CDate(cmbmonth.SelectedIndex + 1 & "/" & z & "/" & txtyear.Text.Trim & " 06:00:00")
  47.                 date02 = DateAdd(DateInterval.Hour, 23, date01)
  48.             Catch ex As Exception
  49.                 Exit For
  50.             End Try
  51.             logs = ""
  52.             HOURSWORKED = 0
  53.             ''LOG IN OUT DETAILS CALCULATION
  54.             Dim dsp02 As DataSet = DataLayer.ExecuateDataSet("select * from attendance where eid=" & cmbemployee.SelectedValue & " and pdate >='" & date01 & "' and pdate <= '" & date02 & "' order by pdate")
  55.             If dsp02.Tables(0).Rows.Count > 0 Then
  56.                 Dim ZZZ As Integer = 0
  57.                 For Each row02 As DataRow In dsp02.Tables(0).Rows
  58.                     logs = logs & Format(DatePart(DateInterval.Hour, row02("pdate")), "00") & ":" & Format(DatePart(DateInterval.Minute, row02("pdate")), "00") & " "
  59.                     ZZZ = ZZZ + 1
  60.                     If (ZZZ = dsp02.Tables(0).Rows.Count) And (dsp02.Tables(0).Rows.Count Mod 2 <> 0) Then
  61.                         Dim ppp = Format(DatePart(DateInterval.Hour, row02("pdate")), "00") & ":" & Format(DatePart(DateInterval.Minute, row02("pdate")), "00") & " "
  62.                         ppp = DateDiff(DateInterval.Minute, CDate(ppp), CDate(ListBox2.Items(ListBox2.Items.Count - 1)))
  63.                         If ppp > 0 Then
  64.                             logs = logs & ListBox2.Items(ListBox2.Items.Count - 1).ToString.Substring(0, 5) & " "
  65.                         Else
  66.                             logs = logs.Substring(0, Len(logs) - 6)
  67.                         End If
  68.                     End If
  69.                     ZZZ = ZZZ - 1
  70.                     If ZZZ Mod 2 = 0 Then
  71.                         TIME1 = row02("pdate")
  72.                         TIME2 = ""
  73.                     Else
  74.                         TIME2 = row02("pdate")
  75.                         HOURSWORKED = HOURSWORKED + DateDiff(DateInterval.Minute, CDate(TIME1), CDate(TIME2))
  76.                     End If
  77.                     ZZZ = ZZZ + 1
  78.                 Next
  79.             End If
  80.             If logs <> "" Then
  81.                 HOURSWORKED = 0
  82.                 Dim logs2 = logs
  83.                 For i = 1 To (Len(logs) / 6)
  84.                     Try
  85.                         TIME1 = logs2.Substring(0, 5)
  86.                         TIME2 = logs2.Substring(6, 5)
  87.                         Try
  88.                             logs2 = logs2.Substring(12)
  89.                         Catch ex As Exception
  90.                         End Try
  91.                         If Hour(CDate(TIME1)) < 6 Then TIME1 = Now.Date & " " & TIME1 & ":00" Else TIME1 = DateAdd(DateInterval.Day, -1, Now.Date) & " " & TIME1 & ":00"
  92.                         If Hour(CDate(TIME2)) < 6 Then TIME2 = Now.Date & " " & TIME2 & ":00" Else TIME2 = DateAdd(DateInterval.Day, -1, Now.Date) & " " & TIME2 & ":00"
  93.                         HOURSWORKED = HOURSWORKED + DateDiff(DateInterval.Minute, CDate(TIME1), CDate(TIME2))
  94.                     Catch ex As Exception
  95.                     End Try
  96.                     i = i + 1
  97.                 Next
  98.                 DataGridView1.Rows(z - 1).Cells("DETAILS").Value = logs & " [" & CInt(HOURSWORKED / 60) & "]"
  99.             End If
  100.  
  101.             DataGridView1.Rows(z - 1).Cells("WORK").Value = CInt(HOURSWORKED / 60)
  102.  
  103.             ''OVER TIME CALCULATION
  104.             If HOURSWORKED <> 0 Then
  105.                 If (HOURSWORKED / 60) - Val(lbldutyhours.Text) > 0 Then
  106.                     DataGridView1.Rows(z - 1).Cells("ot").Value = (HOURSWORKED / 60) - Val(lbldutyhours.Text)
  107.                 Else
  108.                     DataGridView1.Rows(z - 1).Cells("ot").Value = 0
  109.                 End If
  110.                 If HOURSWORKED <= HALFDAY1 Then
  111.                     DataGridView1.Rows(z - 1).Cells("S").Value = "A"
  112.                 ElseIf HOURSWORKED >= HALFDAY1 And HOURSWORKED < HALFDAY2 Then
  113.                     DataGridView1.Rows(z - 1).Cells("S").Value = "H"
  114.                 Else
  115.                     DataGridView1.Rows(z - 1).Cells("S").Value = "P"
  116.                 End If
  117.             Else
  118.                 DataGridView1.Rows(z - 1).Cells("ot").Value = 0
  119.                 DataGridView1.Rows(z - 1).Cells("S").Value = "A"
  120.             End If
  121.  
  122.  
  123.             NOOFLOGS = Len(logs.ToString) / 6
  124.             Dim STR As String = logs
  125.             Dim STR2 As String = ""
  126.             Dim K As Integer = 0
  127.             Dim P As Integer = 0
  128.             Dim kk As Integer = 0
  129.             Try
  130.                 For I = 1 To NOOFLOGS
  131.                     K = STR.IndexOf(" ")
  132.                     STR2 = STR.Substring(0, 5)
  133.                     STR = STR.Substring(K + 1)
  134.                     P = 0
  135.                     If I Mod 2 <> 0 Then
  136.                         If I = 1 Then
  137.                             Dim jj1
  138.                             Dim jj2
  139.                             If CDate(STR2) < "06:00" Then
  140.                                 jj1 = CDate(DateAdd(DateInterval.Day, 1, Now.Date) & " " & STR2)
  141.                             Else
  142.                                 jj1 = CDate(Now.Date & " " & STR2)
  143.                             End If
  144.                             If CDate(CDate(ListBox1.Items(0))) < "06:00" Then
  145.                                 jj2 = CDate(DateAdd(DateInterval.Day, 1, Now.Date) & " " & CDate(ListBox1.Items(0)))
  146.                             Else
  147.                                 jj2 = CDate(Now.Date & " " & CDate(ListBox1.Items(0)))
  148.                             End If
  149.                             P = DateDiff(DateInterval.Minute, jj2, jj1)
  150.                             If P >= 0 Then DataGridView1.Rows(z - 1).Cells("late").Value = DataGridView1.Rows(z - 1).Cells("late").Value + P
  151.                         End If
  152.                     Else
  153.                         If I = NOOFLOGS Then
  154.                             Dim jj1
  155.                             Dim jj2
  156.                             If CDate(STR2) < "06:00" Then
  157.                                 jj1 = CDate(DateAdd(DateInterval.Day, 1, Now.Date) & " " & STR2)
  158.                             Else
  159.                                 jj1 = CDate(Now.Date & " " & STR2)
  160.                             End If
  161.                             If CDate(CDate(ListBox2.Items(ListBox2.Items.Count - 1))) < "06:00" Then
  162.                                 jj2 = CDate(DateAdd(DateInterval.Day, 1, Now.Date) & " " & CDate(ListBox2.Items(ListBox2.Items.Count - 1)))
  163.                             Else
  164.                                 jj2 = CDate(Now.Date & " " & CDate(ListBox2.Items(ListBox2.Items.Count - 1)))
  165.                             End If
  166.                             P = DateDiff(DateInterval.Minute, jj1, jj2)
  167.                             If P >= 0 Then DataGridView1.Rows(z - 1).Cells("EARLY").Value = DataGridView1.Rows(z - 1).Cells("EARLY").Value + P
  168.                         End If
  169.                         kk = kk + 1
  170.                     End If
  171.                 Next
  172.             Catch ex As Exception
  173.             End Try
  174.         Next
  175.     End Sub
Mar 26 '12 #1
Share this Question
Share on Google+
3 Replies


P: 3
Looks to me like "z-1" is lower than your DataGrid index. You could put a message box just before that line to see what "z" is at that point.
Expand|Select|Wrap|Line Numbers
  1. ...
  2. i = i + 1
  3. Next
  4. MsgBox(z)
  5. DataGridView1.Rows(z - 1).Cells("DETAILS").Value = logs & " [" & CInt(HOURSWORKED / 60) & "]"
  6. ...
  7.  
I always use the MsgBox to find out what a variable is showing. You may have to remove the "-1" and see what that gives you.

Peace to you and yours,
Matthew "Dra'Gon" Stohler
Mar 26 '12 #2

P: 2
Hai....
When I put MsgBox(z), below the DataGridView1.Rows(z-1).Cells("DETAILS").Value=logs & "[" & CInt(HOURSWORKED/60) & "]", the MsgBox shows gives a value 1 and when I close the MsgBox, again this same error appears. Am getting this error even if I put this MsgBox on top of this line. Am a novice, and if u dont mind can u explain.
Mar 27 '12 #3

P: 3
The Msgbox was just to show you what value "z" held, a troubleshooting method I often use. It doesn't really need to be there for your code.

I haven't worked with DataGridView but if it's anything like ListView, ComboBox, etc... then you have a specific Index you need to be aware of. It'll start with either 0 or 1.

If it starts at 0 and you're getting that error right at the start then "z" at that point is equal to 0 and you're trying to enter data at Index "z - 1" {0 - 1} or -1. It will give you the "Out of Range" error. If "z" is equal to 1 and the first Index of DataGridView is 1 then you're trying to enter data at Index 0 {again "z - 1" or 1 - 1 = 0}, which may not exist for that control.

Try removing the "- 1" from that line and see if that works.

Peace to you and yours,
Matthew "Dra'Gon" Stohler
Mar 27 '12 #4

Post your reply

Sign in to post your reply or Sign up for a free account.