Assign: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Hi All,

I am trying to download the CSV file and I am using a replace function to replace old csv file however I am getting the error “Assign: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index”

Directory.GetFiles(In_Config(“FileDownloadPath”).ToString.Replace(“user”,system.Environment.UserName),“*.csv”).OrderByDescending(Function(d) New FileInfo(d).CreationTime).Tolist(0)

Hey @dutta.marina ,

Put a breakpoint on Assign and just before it executes can you verify from immediate panel if you are getting something in the list, before you try n access the first value from the list.

Seems like your list might be empty.

strFilePath = Directory.GetFiles(yourfolder_path,“*.csv”).OrderByDescending(Function(d) New FileInfo(d).CreationTime).Tolist(0)

Because when i run this expression at my end, it runs fine.

You might want to break your expression to smaller pieces for easier debugging.

I’d assign this to a variable:

In_Config(“FileDownloadPath”).ToString.Replace(“user”,system.Environment.UserName)

Also, before using

.Tolist(0)

it is advisable to check whether you have elements in the list or not.

Your GetFiles isn’t finding any files, so the array is empty, so there is no 0 index element (which would be the first element in the array).

To solve this, put the GetFiles into an array variable with an Assign, then use an If to make sure it has values. If it does, take the (0) element from the array. If it doesn’t, do whatever you want to do if no files are found.

Hi Paul,

Can you please write the code how I should use here the check if condition statement. I meanin my case case how to check the values present in list or not.

Thanks @efelantti

Will try to implement the same.

@dutta.marina

Please use this in if condition first and then check for the values

Directory.GetFiles(yourfolder_path,“*.csv”).OrderByDescending(Function(d) New FileInfo(d).CreationTime).Count>0

And then on the then side use the (0)

Cheers

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.