Click here to Skip to main content
14,743,331 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
public static int insLabTestingDetails(Object objBrStone)
           int iStatus;
               string[] arrBooking = BAL.JSONToString(objBrStone);
               LabTesting clsLab = new LabTesting();
               clsLab.Lab = arrBooking[0].Split(':')[1];
               clsLab.Agr_No = arrBooking[1].Split(':')[1];
               clsLab.BillNo = arrBooking[2].Split(':')[1];
               clsLab.ReturnDate = arrBooking[3].Split(':')[1];
               clsLab.Return_By = arrBooking[4].Split(':')[1];
               clsLab.Remark = arrBooking[5].Split(':')[1];
               clsLab.TestingId = int.Parse(arrBooking[6].Split(':')[1]);

               BAL clsBAL = new BAL();
               iStatus = clsBAL.UpdateLabTesting(clsLab);
               return iStatus;
           catch (Exception err)
               iStatus = 0;
           return iStatus;

What I have tried:

Log Written Date: 03/07/2020 13:34:56
Error Line No : line 58
Error Message: IndexOutOfRangeException
Exception Type: System.IndexOutOfRangeException
Error Location : Index was outside the bounds of the array.
 Error Page Url:

The log file is showing Error Index was outside the bounds of the array. How can i resolve it in above function.
Updated 3-Jul-20 4:03am
F-ES Sitecore 3-Jul-20 8:44am
This question is asked every day, please do basic research before asking a question like using google. Search for the error message or look at the many "Related Questions" at the right of the screen, you're going to get the exact same answer as all of those.
Patrice T 3-Jul-20 8:50am
and line 58 is ...

Look at line 58 and see what index values or variables are being referred to. Then, if you cannot figure out why, you need to run the code in the debugger and trap it at the point of the exception. That will allow you to examin all the variables in order to diagnose the problem.

By the way, lines such as the following are not the best idea:
clsLab.Lab = arrBooking[0].Split(':')[1];

If the data you are splitting does not contain two fields separated by a colon then that statement will cause the error. Do the split first and check the results to ensure you have the correct number of fields. Never assume that your data is in the correct format.
[no name] 3-Jul-20 9:01am
And also already in case arrBooking is empty. +5
Richard MacCutchan 3-Jul-20 9:06am
Exactly so, but I hoped OP would figure that out (yes, I know he probably wont).
The log file is showing Error Index was outside the bounds of the array.

No matter what is the exact problem, this means that somewhere in your code, you expect an array to be a given size, but is shorter.
The only thing to do is to check the size of array, either you change you code to do the check, either you use the debugger.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^][^][^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

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