2
votes

I am trying insert asp.net form field values to oracle database table. I have a date field which is in "MM-DD-YYYY" format. I need to add that date to oracle table. So i am trying to convert that date format to "DD-MMM-YYYY" format. But i am getting the following error.

code:

var creation_date = DateTime.ParseExact(CreationDateTextBox.Text, "DD-MMM-YYYY",null);

Text box value is: 12-12-2013.(No time) i am getting error like "String was not recognized as a valid DateTime".

5
have you tried to check links on internet???The Hungry Dictator
You've been Skeet'd. :)crthompson
try DateTime.ParseExact("12-dec-2013", "dd-MMM-yyyy",null)Damith
A DateTime has no format. It's basically a fancy Int64 counting ticks. So you cannot "convert" a DateTime from one format to another. You can "convert" (i.e. parse) a String to get a DateTime. But to do that, you need to tell the parse method the format of the String you're parsing from. In this case: "MM-dd-yyyy". Then, if you want to display that abstract number of ticks, you convert it back to the human readable form you like: date.ToString("dd-MMM-yyyy"). But putting it into a DB should not require that.Corak

5 Answers

6
votes

This might help :)

String myString = "12-30-2014"; // get value from text field
DateTime myDateTime = new DateTime();
myDateTime = DateTime.ParseExact(myString, "MM-dd-yyyy",null);
String myString_new = myDateTime.ToString("dd-MM-yyyy"); // add myString_new to oracle
9
votes

You need to parse the date using MM-dd-yyyy but then you shouldn't need to format it at all. Just pass it to the database as a DateTime using parameterized SQL.

DateTime creationDate;
if (DateTime.TryParseExact(CreationDateTextBox.Text, "MM-dd-yyyy",
                           CultureInfo.InvariantCulture, DateTimeStyles.None,
                           out creationDate))
{
    // Use creationDate within a database command, but *don't* convert it
    // back to a string.
}
else
{
    // Handle invalid input
}

Avoid string conversions wherever you can. Indeed, ideally use a date/time picker of some description rather than just a text field - that will give users a better experience and reduce the risk of poor conversions.

Also note that whenever you want to use custom string conversions (parsing or formatting) you should read the MSDN docs - YYYY and DD aren't valid format specifiers.

3
votes

Try this

DateTime dt = Convert.ToDateTime(CreationDateTextBox.Text);
var creation_date=String.Format("{0:dd-MMM-yyyy}", dt)

OR try as

 dt.ToString("dd MMM yyyy"); 
1
votes

You have three M for the month, which is used for month names. Just use two M.

1
votes
    private DateTime ConvertToDateTime(string strDateTime)
    {
        DateTime dtFinaldate; string sDateTime;
        try { dtFinaldate = Convert.ToDateTime(strDateTime); }
        catch (Exception e)
        {
            string[] sDate = strDateTime.Split('/');
            sDateTime = sDate[1] + '/' + sDate[0] + '/' + sDate[2];
            dtFinaldate = Convert.ToDateTime(sDateTime);
        }
        return dtFinaldate;
    }