tags:

views:

41

answers:

3

Hi I have used a textbox to store a date in DD-MM-YYYY format and since i use SQL server i used to get the value from the textbox and then place it in a variable of type DateTime dt

DateTime dt = DateTime.ParseExact(TextBox10.Text,"MM-DD-YYYY",CultureInfo.InvariantCulture);

I am getting format specification error, I went through many documentation but still couldn't get where i am going wrong

This i am using C# and ADO.net Please can anyone correct me?

+1  A: 

try this

DateTime.ParseExact("12-12-2010", "MM-dd-yyyy", System.Globalization.CultureInfo.InvariantCulture)

Take care of date format string casing. "dd" can be different from "DD"

Marcelo de Aguiar
@Marcelo de Aguiar the input wll be from textbox :(
Nagaraj Tantri
@Marcelo de Aguiar the format also didnt work... it still says, that "MM-dd-YYYY" is not a valid datetime :(
Nagaraj Tantri
A: 

As mentioned above, date format string is case sensitive.

However, are you sure you want to use DateTime.ParseExact as opposed to DateTime.Parse?

bryanjonker
@bryanjonker am asking the user to input (dd-mm-yyyy) but sql stores it as (MM-DD-YYYY) so i used it.. i guess then the case sensitive part i will have to give a look
Nagaraj Tantri
+1  A: 

It looks like you mixed up DD and MM. Try below:

DateTime dt = DateTime.ParseExact(TextBox10.Text,"dd-MM-yyyy",CultureInfo.InvariantCulture);

Notice that I changed "DD" to "dd"!

UPDATE:

Ok, so I changed "YYYY" to "yyyy" and ran the code below and it parsed sucessfully:

DateTime dt = DateTime.ParseExact("28-01-2010", "dd-MM-yyyy", provider);

If your users really are passing in dates in the format "dd-MM-yyyy" this should work for you. Keep in mind that the second string has nothing to do with whatever format you are storing in your database. It only has to do with the format of the first string argument in ParseExact. HTH.

Abe Miessler
i am asking the user to input (dd-mm-yyyy) but sql stores it as (MM-DD-YYYY)so i used it.. i guess then the case sensitive part i will have to give a look
Nagaraj Tantri
Hey Nagaraj, read my update. I still think this is an issue with how users are inputing dates and the format you expect them to be in when you parse.
Abe Miessler
also, note that dd means the following format: 28, 10, 01 etc. Pay special attention to the "01" example. It expects a two digit day, even for 1-9... Same goes for month.
Abe Miessler
@Abe , but then if you say my second string does not matter, which i cannot go for textbox.text in the first parameter?
Nagaraj Tantri
I'm not saying the second string doesn't matter. In fact i'm saying that string is the cause of all your problems. It's all that matters! I don't understand what you mean by, "which i cannot go for textbox.text in the first parameter?" Can you please clarify?
Abe Miessler
By textBox.text i mean, i have taken input from user through a textbox and since in India we follow dd-mm-yyyy most of the time, i thought ill ask user to enter through textbox then change it to DateTime which is in SQL MM-DD-YYYY...and yeah "yyyy" helps but it then does not parse it too MM-DD-YYYY and throws an exception saying its not according to gorgiean calander... :(
Nagaraj Tantri
I think there is some confusion here. The bottom line is that if your users are entering info in the dd-MM-yyyy format you will be able to parse that into a DateTime using the method i described. I don't know what you are doing after that but you should AT LEAST be able to parse it into a DateTime. Do you have an IM account I could talk to you on to try and explain?
Abe Miessler
@abe i am still new to .net with SQL, I am doing this project as a summer course but getting stuck in many things.. still learning process.. my username in gtalk is nagarajtantri
Nagaraj Tantri