Converting string to date time

E

eric.goforth

Hello,

I'm trying to convert a string to a date time in a C# web service.

I'm passing in a string parameter and I have a localization setting in
my Web.config file:

My app is blowing up on the first line here, with a"Value does not fall
within the expected range" error.

CultureInfo ci = new CultureInfo("en-US",true);
ci.DateTimeFormat.ShortDatePattern =
ConfigurationSettings.AppSettings["DateFormat"];

try
{
DateTime myStartDate = DateTime.Parse(startDate, ci);
}

Any idea what's wrong? Is there a simpler way to do this?

-Eric
 
P

Paul E Collins

I'm trying to convert a string to a date time in a
C# web service. [...] Any idea what's wrong?

Well, what's the value that you're passing in?
Is there a simpler way to do this?

Why can't you pass in a DateTime instead of a string? It also won't
fail when you have to deal with locales outside the United States
(e.g. Europe and Japan, which each have their own different date
formats).

Eq.
 
E

eric.goforth

Hello,

My web service is a test harness to mimic the behavior of another
company's web service, so I have to use the API that's they use.

I don't think that it matters what I pass through, I'm passing in
"01.01.06". My app is giving me the Value does not fall
within the expected range" when I try to execute the first line:

CultureInfo ci = new CultureInfo("en-US",true);

Thanks,
-Eric
 
P

Paul E Collins

I don't think that it matters what I pass through, I'm passing in
"01.01.06". My app is giving me the Value does not fall within
the expected range" when I try to execute the first line:

CultureInfo ci = new CultureInfo("en-US",true);


Sorry - read it too quickly.

"Value does not fall within the expected range" is the default message
for ArgumentException, which the CultureInfo constructor can throw,
but only if "<name> is not a valid culture name" - and your line of
code with "en-US" works fine on my machine here. I don't think it's
possible to make it fail by somehow adding or removing cultures,
because they're a standard set, regardless of what international
support the particular computer has got.

How are you determining where the error is? Do you have the complete
stack trace available? I've sometimes seen the debugger indicate the
wrong line (one above or below), so it *might* not be that first line.

Eq.
 
J

Jon Skeet [C# MVP]

My web service is a test harness to mimic the behavior of another
company's web service, so I have to use the API that's they use.

I don't think that it matters what I pass through, I'm passing in
"01.01.06". My app is giving me the Value does not fall
within the expected range" when I try to execute the first line:

CultureInfo ci = new CultureInfo("en-US",true);

That seems somewhat unlikely. What does the full stack trace look like?
Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
E

eric.goforth

I get the same thing when I try to execute the same line in a simple
Winforms application, here is the code for my Form1:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Globalization;

namespace WindowsApplication2
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
CultureInfo ci = new CultureInfo("en-US",true);

}
}
}
 
P

Paul E Collins

I get the same thing when I try to execute the same
line in a simple Winforms application, here is the code
for my Form1:
[... snipped example program ...]

I copied and pasted that exactly, and checked that it was reaching the
CultureInfo creation line, and it ran perfectly. There must be some
strange configuration problem at your end. Short of running it on a
different machine - or removing and reinstalling the Framework (which,
of course, isn't guaranteed to fix it) - I don't know what to suggest,
unfortunately.

Eq.
 
J

Jon Skeet [C# MVP]

I get the same thing when I try to execute the same line in a simple
Winforms application, here is the code for my Form1:

Just a hint for future examples - console apps make much simpler little
test apps. Does the following work on your computer?

using System;
using System.Globalization;

class Test
{
static void Main()
{
Console.WriteLine(new CultureInfo("en-US",true));
}
}
 
E

eric.goforth

Strangely your sample app does work. When I comment out your line and
put in CultureInfo ci = new CultureInfo("en-US",true); it doesn't
work.

-Eric

using System;
using System.Globalization;

class Test
{
static void Main()
{
//Console.WriteLine(new CultureInfo("en-US",true));
CultureInfo ci = new CultureInfo("en-US",true);

}

}
 
E

eric.goforth

Weird weird weird. If I copy and past your "new
CultureInfo("en-US",true)" to the right of my "=" sign it does work.
If I do a Ctrl-Z and step back though my code it doesn't. I guess
there's some kind of weird character in there or something?

This works:

using System;
using System.Globalization;

class Test
{
static void Main()
{
//Console.WriteLine(new CultureInfo("en-US",true));
CultureInfo ci = new CultureInfo("en-US",true);

}

}

This does NOT work:

using System;
using System.Globalization;

class Test
{
static void Main()
{
//Console.WriteLine(new CultureInfo("en-US",true));
CultureInfo ci = new CultureInfo("en-US",true);

}

}
 
E

eric.goforth

I'm getting past the line that was blowing up before, apparently some
weird character in there. I copied the line of code off a website and
changed it to my "en-US", maybe there was some kind of weird character
in there. I tried to copy and paste through notepad to filter them
out, but it didn't help. Usually that will work to get plain text for,
say, Word documents. Now I'm getting a "Could not determine the order
of year, month, and date from dd.mm.yy. When I try to DateTime.Parse.
All this culture info stuff seems nice if your customers use the
default date format for their culture, but the old-fashioned Format
seems much easier for customization.

-Eric

using System;
using System.Globalization;

class Test
{
static void Main()
{
String stringDate = "01.01.06";
DateTime myStartDate;

CultureInfo ci = new CultureInfo("en-US",true);
ci.DateTimeFormat.ShortDatePattern = "dd.mm.yy";

myStartDate = DateTime.Parse(stringDate, ci);

Console.WriteLine("Successful.");

}

}
 
J

Jon Skeet [C# MVP]

I'm getting past the line that was blowing up before, apparently some
weird character in there. I copied the line of code off a website and
changed it to my "en-US", maybe there was some kind of weird character
in there. I tried to copy and paste through notepad to filter them
out, but it didn't help. Usually that will work to get plain text for,
say, Word documents. Now I'm getting a "Could not determine the order
of year, month, and date from dd.mm.yy. When I try to DateTime.Parse.
All this culture info stuff seems nice if your customers use the
default date format for their culture, but the old-fashioned Format
seems much easier for customization.

If you need to parse a specific format, look at DateTime.ParseExact and
specify the format string yourself. You still need to give a culture
info, but in many cases the format string can be culture-insensitive
anyway.

Jon
 
G

Guest

I found that when I'm sending a query or inserting a record that has a date
which I grab from a textbox i've had to do the followin

"#" & datetextbox & "#"

I was getting the same error, SQL likes the pound signs on each end of the
date.

John
 
J

Jon Skeet [C# MVP]

Vear said:
I found that when I'm sending a query or inserting a record that has a date
which I grab from a textbox i've had to do the followin

"#" & datetextbox & "#"

I was getting the same error, SQL likes the pound signs on each end of the
date.

That's definitely not the right way to do it - you've opened yourself
up for a SQL injection attack. You should parse the string into a
DateTime, and then use a parameterized SQL statement, setting the
parameter values appropriately.
 

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