Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Private Sub btnRentM_Click(sender As Object, e As EventArgs) Handles btnRentM.Click
        ID = dgvRentAllCarsM.Rows(CurrentRecord).Cells(0).Value

        Dim RentalInfo = (From aRental In CarRentaldb.Rentals
                         Where aRental.RentalID = ID
                         Select aRental).FirstOrDefault


        RentalInfo.RentalID = txtRentRentalIDM.Text
        RentalInfo.CarID = txtRentCarIDM.Text
        RentalInfo.CustomerCivilID = txtCustomerCivilIDM.Text
        RentalInfo.StaffID = txtRentStaffIDM.Text
        RentalInfo.StartDate = dtpRentStartM.Text
        RentalInfo.EndDate = dtpRentEndM.Text
        RentalInfo.ActualDate = dtpRentActualM.Text
        RentalInfo.PenaltyForLate = txtRentPenaltyForLateM.Text
        RentalInfo.PenaltyForDamage = txtlblRentPenaltyForDamageM.Text


        MsgBox("A new car is rented")
        CarRentaldb.Rentals.Add(RentalInfo)
        CarRentaldb.SaveChanges()
        UpdateAllRentals()
        CurrentRecord = 0
        dgvRentAllCarsM.Rows(CurrentRecord).Selected = True
        DisplayRentalInfo()
    End Sub


The error is
Quote:
"Index was out of range. Must be nonnegative and less than the size of the collection"
and it appears on the first sentence of the code I submitted ... Please help me!
Posted
Updated 6-May-14 5:45am
v2
Comments
Member 10798948 6-May-14 11:35am    
The error is:
"Index was out of range. Must be nonnegative and less than the size of the collection" and it appears on the first sentence of the code I submitted ... Please help me!
[no name] 6-May-14 11:43am    
Your error message means exactly what it says. You are trying to access an element of an array that does not exist.
Pheonyx 6-May-14 11:44am    
What have you done to debug the issue.. at a guess, I would say you need to do CurrentRecord - 1.<br>
I woudl suggest you step through the code and see how many entries you are seeing in the Rows collection in relation to your value of CurrentRecord. Then I would look at "Cells(0)" and see if that has entries?

1 solution

We can't say how to fix it: we can't run your code, and particularly not in isolation from your data!

So use the debugger: put a breakpoint on the first line of the method, and look at the values. At a guess, the problem is with CurrentRecord - it may be negative, or it may be larger than the max index of the Rows collection.

But without being able to run the code, we can't tell - and the problem will be caused elsewhere in your code, and we can't see that at all!
 
Share this answer
 
Comments
Member 10798948 6-May-14 14:40pm    
OK thank you for your help.
OriginalGriff 6-May-14 14:44pm    
You're welcome!
Sorry we can't help more - but without your code running in front of us, we can't do much at all! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900