Convert.DateTime Error in .Net 2.0


M

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.CreateTokenHashTable()
at System.Globalization.DateTimeFormatInfo.Tokenize(TokenType
TokenMask, TokenType& tokenType, Int32& tokenValue, __DTString& str)
at System.__DTString.GetSeparatorToken(DateTimeFormatInfo 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(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef
hWnd, Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(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.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

This happens
 
Ad

Advertisements

L

Lauren

try
DateTime dt1 = DateTime.Parse("10 Sep 2005");




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.CreateTokenHashTable()
at System.Globalization.DateTimeFormatInfo.Tokenize(TokenType
TokenMask, TokenType& tokenType, Int32& tokenValue, __DTString& str)
at System.__DTString.GetSeparatorToken(DateTimeFormatInfo 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(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef
hWnd, Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(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.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

This happens
 
M

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
 
G

Guest

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?
 
L

Lars-Inge Tønnessen \(VJ# MVP\)

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
 
Ad

Advertisements

M

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!!!
 
M

Me

Its just code in the orm load event in a new project! Two lines and the
2nd doesnt even get executed!
 
L

Lauren

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.
 
M

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"
 
Ad

Advertisements

G

Guest

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.
 
M

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!
 
J

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.CreateTokenHashTable()
at System.Globalization.DateTimeFormatInfo.Tokenize(TokenType
TokenMask, TokenType& tokenType, Int32& tokenValue, __DTString& str)
at System.__DTString.GetSeparatorToken(DateTimeFormatInfo 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
 
Ad

Advertisements

J

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top