Click here to Skip to main content
15,904,023 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
string[] dateF = dateFrom.Split('/');
            string[] dateT = dateTo.Split('/');

            DateTime dtFrom = new DateTime(Convert.ToInt32(dateF[2]), Convert.ToInt32(dateF[1]), Convert.ToInt32(dateF[0]));
            DateTime dtTo = new DateTime(Convert.ToInt32(dateT[2]), Convert.ToInt32(dateT[1]), Convert.ToInt32(dateT[0]));


when i run the above code i m getting index was outside the bounds of the array

how to resolve this problem.
Posted
Updated 28-Oct-15 19:23pm
v2
Comments
Suvendu Shekhar Giri 29-Oct-15 1:25am    
First check what dateFrom exactly contains.
RelicV 29-Oct-15 1:33am    
Always add validation before you use static variables in arrays. Make sure the array contains more than the number of items. Say if you use array[2], then add a if condition array.Length >= 3. This way, if the array contains the information, it will execute the code. Else, its fail proof.

Then as suvendu said, check for the values whether they are coming as per your expected values or not.

It all depends on dateFrom and dateTo. Split will return empty array if they are empty strings and only one element in the array if there is no "/" delimiters. If first case, index 0 will be invalid, in second, more likely case, index 1 will be invalid. Isn't it obvious? Anyway, in any doubt, use the debugger, always.

But your whole approach is wrong. With .NET, you should not parse time values in such a home-baked way. Instead, use these methods:
https://msdn.microsoft.com/en-us/library/system.datetime.parse%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.datetime.parseexact%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.datetime.tryparse%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.datetime.tryparseexact%28v=vs.110%29.aspx[^].

As you can see, altogether, where are many similar methods. You can control the format of expected string by culture or exact format string.

But let me make another step forward: I'm not sure that you need to parse any strings at all. It could be simply the result of the attempt of working with strings representing data instead data itself, a very "popular" sorrowful trend. Normally, you need a string only if you output some time information in screen. Usually, it's possible to work only with System.DateTime (and sometime System.TimeSpan type, without any parsing. For example, input of time values can be done through some kind of already available date/time input control or ask current time from the OS.

—SA
 
Share this answer
 
As per error details, if the current index is exceeds the actual index in that case we will get this type of error. I request you to debug the code and check in which line you got this error and check index of string array may be this lower of your calling(index), instead of giving index statically better to give index based on index length and check the length condition before allocate it to some object.
 
Share this answer
 
Best way to get rid of this error is to do debug. Check whether string[] dateT has items ??

One more clue to not get this error is to use ajax datetime extender to enter valid dd/MM/yyyy or MM/dd/yyyy control.

You can use below javascript validations
http://www.the-art-of-web.com/javascript/validate-date/

-BChaudhari
 
Share this answer
 
v3

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