By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,979 Members | 1,349 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,979 IT Pros & Developers. It's quick & easy.

Convert.DateTime Error in .Net 2.0

P: n/a
Me
Hi all

I am getting a really bizzare error on when I convert a string into a
datetime:

The code is :

DateTime dt1 = Convert.ToDateTime("10 Sep 2005");
Console.WriteLine(dt1.Year);

I have installed .Net 2.0, on xp Pro SP2. When I do the following I get
an error saying:

What is this? How strange. Its pretty vital that I can convert strings
to dates - anyone know where to start with this? I tried it in vb and
it still threw the same error.

System.IndexOutOfRangeException was unhandled
Message="Index was outside the bounds of the array."
Source="mscorlib"
StackTrace:
at
System.Globalization.DateTimeFormatInfo.InsertHash (TokenHashValue[]
hashTable, String str, TokenType tokenType, Int32 tokenValue)
at
System.Globalization.DateTimeFormatInfo.CreateToke nHashTable()
at System.Globalization.DateTimeFormatInfo.Tokenize(T okenType
TokenMask, TokenType& tokenType, Int32& tokenValue, __DTString& str)
at System.__DTString.GetSeparatorToken(DateTimeFormat Info dtfi)
at System.DateTimeParse.Lex(DS dps, __DTString& str,
DateTimeToken& dtok, DateTimeRawInfo& raw, DateTimeResult& result,
DateTimeFormatInfo& dtfi)
at System.DateTimeParse.TryParse(String s, DateTimeFormatInfo
dtfi, DateTimeStyles styles, DateTimeResult& result)
at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi,
DateTimeStyles styles)
at System.DateTime.Parse(String s, IFormatProvider provider)
at System.Convert.ToDateTime(String value)
at tmpWin1.Form1.Form1_Load(Object sender, EventArgs e) in D:\My
Stuff\Programming\Net 2.0\C#\tmp\tmpWin1\tmpWin1\tmpWin1\Form1.cs:line
20
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean
fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Mes sage& m)
at System.Windows.Forms.ContainerControl.WndProc(Mess age& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.O nMessage(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow( HandleRef
hWnd, Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolea n value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at
System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32
reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at tmpWin1.Program.Main() in D:\My Stuff\Programming\Net
2.0\C#\tmp\tmpWin1\tmpWin1\tmpWin1\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.Run UsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context( Object
state)
at System.Threading.ExecutionContext.Run(ExecutionCon text
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

This happens

Mar 28 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a
Well, that's not a date... neither is "The Tenth of September 2006".

Mar 28 '06 #2

P: n/a
try
DateTime dt1 = DateTime.Parse("10 Sep 2005");


"Me" <il***@igsoftwaresolutions.co.uk> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
Hi all

I am getting a really bizzare error on when I convert a string into a
datetime:

The code is :

DateTime dt1 = Convert.ToDateTime("10 Sep 2005");
Console.WriteLine(dt1.Year);

I have installed .Net 2.0, on xp Pro SP2. When I do the following I get
an error saying:

What is this? How strange. Its pretty vital that I can convert strings
to dates - anyone know where to start with this? I tried it in vb and
it still threw the same error.

System.IndexOutOfRangeException was unhandled
Message="Index was outside the bounds of the array."
Source="mscorlib"
StackTrace:
at
System.Globalization.DateTimeFormatInfo.InsertHash (TokenHashValue[]
hashTable, String str, TokenType tokenType, Int32 tokenValue)
at
System.Globalization.DateTimeFormatInfo.CreateToke nHashTable()
at System.Globalization.DateTimeFormatInfo.Tokenize(T okenType
TokenMask, TokenType& tokenType, Int32& tokenValue, __DTString& str)
at System.__DTString.GetSeparatorToken(DateTimeFormat Info dtfi)
at System.DateTimeParse.Lex(DS dps, __DTString& str,
DateTimeToken& dtok, DateTimeRawInfo& raw, DateTimeResult& result,
DateTimeFormatInfo& dtfi)
at System.DateTimeParse.TryParse(String s, DateTimeFormatInfo
dtfi, DateTimeStyles styles, DateTimeResult& result)
at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi,
DateTimeStyles styles)
at System.DateTime.Parse(String s, IFormatProvider provider)
at System.Convert.ToDateTime(String value)
at tmpWin1.Form1.Form1_Load(Object sender, EventArgs e) in D:\My
Stuff\Programming\Net 2.0\C#\tmp\tmpWin1\tmpWin1\tmpWin1\Form1.cs:line
20
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean
fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Mes sage& m)
at System.Windows.Forms.ContainerControl.WndProc(Mess age& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.O nMessage(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow( HandleRef
hWnd, Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolea n value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at
System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32
reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at tmpWin1.Program.Main() in D:\My Stuff\Programming\Net
2.0\C#\tmp\tmpWin1\tmpWin1\tmpWin1\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.Run UsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context( Object
state)
at System.Threading.ExecutionContext.Run(ExecutionCon text
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

This happens

Mar 28 '06 #3

P: n/a
Me
Of course it is. The Convert.ToDateTime method has always reconised
dates in various formats, in particular "10 Sep 2004" etc

This is the way it operated in .Net 1.1 and 1.0, but for some reason
2.0 apears to be different and I am lost as to why this is

Mar 28 '06 #4

P: n/a
My laptop is set to Pacific Time Zone and your code is working for me under
..NET 2.0. Was the Convert call line 20 on your form or is it some other part
other part of the form code?
Mar 28 '06 #5

P: n/a
Hi,

Your code is working with .NET 2.0. It could be something else...
Please show us your code.

using System;
using System.Collections.Generic;
using System.Text;

namespace cmd_time
{
class Program
{
static void Main(string[] args)
{
DateTime dt1 = Convert.ToDateTime("10 Sep 2005");
Console.WriteLine(dt1.Year);
System.Console.ReadKey();
}
}
}
Regards,
Lars-Inge Tønnessen
Mar 28 '06 #6

P: n/a
Me
The code is

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace tmpWin1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
DateTime dt1 = Convert.ToDateTime("10 Jan 2001");
Console.WriteLine(dt1.Year);
}
}
}

The error occurs on the line DateTime dt1 = Convert.ToDateTime("10 Jan
2001");
This happens even if I try "10/05/2001" and happens in VB.Net also!

Im lost!!!

Mar 29 '06 #7

P: n/a
Me
Its just code in the orm load event in a new project! Two lines and the
2nd doesnt even get executed!

Mar 29 '06 #8

P: n/a
Me
Tried that and I still got the same error!

Mar 29 '06 #9

P: n/a
I pasted your code into a win app project and it worked fine, except the
console writeline didn't go anywhere. I even tried changing my systems
regional settings but it worked everytime. If you figure out what is
happening please post it.

"Me" <il***@igsoftwaresolutions.co.uk> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
Tried that and I still got the same error!

Mar 30 '06 #10

P: n/a
Me
It doesnt work on my machine I get an error :
System.IndexOutOfRangeException was unhandled
Message="Index was outside the bounds of the array."
Source="mscorlib"

Mar 30 '06 #11

P: n/a
It's not just Me that gets this it's me aswell.

I managed to get it working on a machine today though by going to Regional
and Language options in control panel, Regional options, customize..., Time
tab. On here the "AM symbol" and "PM symbol" entries were blank. I changed
these to AM and PM respectively and it worked.

I've only got access to a couple of machines with .net framework 2 on it so
I can't prove whether this is always the answer. Could be other factors or
coincidence.

"Me" wrote:
It doesnt work on my machine I get an error :
System.IndexOutOfRangeException was unhandled
Message="Index was outside the bounds of the array."
Source="mscorlib"

Mar 31 '06 #12

P: n/a
Me
I think youre right.

I tried changing the date seperator character within the regional
setting option in control panel. It was currently set to a space
character. This causes .Net 2.0 to fail when converting the string to a
date. However I then changed the date seperator character to a "/"
character and it all worked!!

I tried it on a different machine with exactly the same setup and it
worked so long as the seperator character was not a blank space.

The issue now is - why does it work in .Net 1.0 and 1.1 but not in 2.0?

I guess we are sort of there, but not quiet!

Apr 2 '06 #13

P: n/a
JJ
I am getting the same error
ASP.NET 2.0 Visual Studio 2005 when running with Web options to "Use
Custom server" when I "Use Default Server" I do not get the error. I
am suspecting there is perhaps a bug in IIS on my machine. My coworkers
are not having a problem.

Source:
mscorlib
Error Message:
Index was outside the bounds of the array.
Stack Trace:
at
System.Globalization.DateTimeFormatInfo.InsertHash (TokenHashValue[]
hashTable, String str, TokenType tokenType, Int32 tokenValue)
at System.Globalization.DateTimeFormatInfo.CreateToke nHashTable()
at System.Globalization.DateTimeFormatInfo.Tokenize(T okenType
TokenMask, TokenType& tokenType, Int32& tokenValue, __DTString& str)
at System.__DTString.GetSeparatorToken(DateTimeFormat Info dtfi)
at System.DateTimeParse.Lex(DS dps, __DTString& str, DateTimeToken&
dtok, DateTimeRawInfo& raw, DateTimeResult& result, DateTimeFormatInfo&
dtfi)
at System.DateTimeParse.TryParse(String s, DateTimeFormatInfo dtfi,
DateTimeStyles styles, DateTimeResult& result)
at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi,
DateTimeStyles styles)
at System.DateTime.Parse(String s, IFormatProvider provider)
at System.Convert.ToDateTime(String value)
at WebUI.Modules.Login.LoginBtn_Click(Object sender, EventArgs e) in
c:\HDWS\DotNet\WebUI\Modules\Login.ascx.cs:line 176

*** Sent via Developersdex http://www.developersdex.com ***
Apr 3 '06 #14

P: n/a
JJ
Based on more research I think that "mscorlib" is being resolved
differently depending on where the app is executed.

Anyone know anything about how this stuff works?

JJ

*** Sent via Developersdex http://www.developersdex.com ***
Apr 3 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.