473,396 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Argument Out Of Range Exception was unhandled.

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
3 4743
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
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
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

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

Similar topics

2
by: smith flyers | last post by:
string thingword=" testing hmmm"; string firstLetter,restOfWord; foreach (string word in thingword.Split()) { Console.WriteLine(word); // if this only use for output and the rest in...
3
by: Lou | last post by:
I can't track down this exception. If anyone has any ideas, please pass them on. I've been trying to kill this bug for days now. An unhandled exception of type 'System.NullReferenceException'...
3
by: Pengyu | last post by:
Whenever I call listView.RemoveAt(listView.Count-1), which remove the last item, I get an out of range exception. How to solve this problem? Thanks a lot, Pengyu.
0
by: Bill Johnson | last post by:
I have a further question regarding an archive post at: ...
4
by: Brad | last post by:
Now I am encountering my second very strange problem. I will describe the first one later. But my current problem lies with trying to bind to a combobox: Try If clubID <> "" And txtYear.Text <>...
0
by: Nick | last post by:
Hi people, I have a custom control combo box in a windows application that has a list of items to be displayed. there are currently 17 items that are bound to the table that acts as the data...
5
by: pcnerd | last post by:
I'm trying to create a program that plots randomly colored pixels on a bitmap & then displays the bitmap. When I run the program, I see the pixels being plotted down the left side of the form. When...
1
by: dedipya | last post by:
Environment W2003K with Oracle 10 client connected to oracle using oracle odp provider.orcle is on solaries machine. while filling the dataset .net is throughing the error "argument was out of the...
7
by: Pucca | last post by:
Hi: Below is the error I got from the 2 lines of code below. I don't understand why and how to correct it. The actionMenu.DropDownItems has 0 item in its collection at the time of the code. ...
8
by: amyl | last post by:
I have an application whose main function is encapsulated in encased in a try/catch block catching Exception. My application has crashed on several occasions with a CLR exception. CLR exception...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.