Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET SQL DateTime
I have a textbox in which i take date input from user. But I can't save that date to my db as it shows error that invalid date format. How can I change the datetime format of asp.net(C#) to mysql db format which is yyyy-mm-dd?
Posted 11-Oct-10 2:32am
sazib373
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Convert.ToDateTime(txtDate.Text).ToString("yyyy-MM-dd")
  Permalink  
Comments
Member 9722231 at 7-Jun-13 9:02am
   
my problem solved! Thanks
mute_gemini at 27-Feb-14 1:50am
   
my problem solved! Thanks
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

The first step shown in m@dhu#s answer is correct: convert the string you get from the aspxpage to a DateTime value. But then, use a parameterized query to get the data into your database! By string concatenation, you would open it for SQL injection attacks.
Do you know how such an attack works? Well, read a thread in the hall of shame: http://www.codeproject.com/Feature/WeirdAndWonderful.aspx?msg=4335687#xx4335687xx[^] (the information on the injection attack comes late - and: they haven't fixed it yet, you can try your skills...)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 9

Try This Friend...
DateTime dtTimeObj = Convert.ToDateTime("9/14/2009 12:00:00 AM");
dtTimeObj.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

The Solution 01 is correct. But the standard way of doing this is using a "DateTimePicker". You can easily find it under the "Common Controls" in ToolBox. To format the date according to the given format, follow the steps given below:

1.select the "DateTimePicker"
2.press "F4" to see the properties window
3.change the property of "CustomFormat" as yyyy-MM-dd
4.change the property of "Format" as Custom


If you have not defined the relevant database field as a datetime field, don't forget to cast the output of "DateTimePicker" as it demands.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

You must convert date before inserting.
You can convert it from C#
http://www.dotnetperls.com/datetime-format

OR in sql
http://www.sqlusa.com/bestpractices/datetimeconversion/
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

var userdateformat = DateTime.ParseExact("20120827", "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

Try this
DateTime dt = DateTime.ParseExact(dateString, "ddMMyyyy", CultureInfo.InvariantCulture);
dt.ToString("yyyyMMdd");
  Permalink  
Comments
bhagirathimfs at 27-Aug-12 9:13am
   
Is it showing error?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

try this...
DateTime dtTimeObj = Convert.ToDateTime("9/14/2009 12:00:00 AM");
dtTimeObj.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 10

For SQL Convert(varchar(20), ColumnName, 20) AS ColumnSameName

I use this to import data to a datagridview in c# format the date at query and display the datetime

Then I get that information and export to a CSV - Otherwise the format will show dd/mm/yyyy HH:mm tt

Result:

yyyy-MM-dd HH:mm:ss

2014-11-21 02:03:04
  Permalink  
Comments
King Fisher at 22-Nov-14 1:31am
   
you are trying to solve 4 year old Questions
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 11

  Permalink  
Comments
King Fisher at 22-Nov-14 1:31am
   
you are trying to solve 4 year old Questions
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 12

i am getting date & time value from database to datagridview. even i assigned data type of date in database as "DATE" format.

And i tried the following code to resolve
dataGridView1.Columns[7].DefaultCellStyle.Format = "MM/dd/yyyy".

can anyone help me, where i have to focus resolving this issue
  Permalink  
Comments
CHill60 at 26-Feb-15 4:26am
   
If you have a question of your own don't post it as a "solution" to an old question. Use the "Ask a Question" link.
By the way you haven't described what the issue is and you will need to include the code that assigns the value to the cell
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 13

Hi,
Check what is the datafield value of the column of your date column. if its varchar any date can be inserted in the table. You can also set the datafield value to smalldatetime to store the value.

When retriving the value you can convert it in to string format as,

string date=reader("Date").ToString("MM/dd/yyyy"); 
  Permalink  
Comments
CHill60 at 26-Feb-15 4:28am
   
The question was asked over 4 years ago. If this was meant to be a response to the question posted as a solution then use the "Have a Question or Comment" link next to that post, or wait until the member posts their own question.
Manoj Sawant at 26-Feb-15 4:43am
   
So....
CHill60 at 26-Feb-15 5:27am
   
So ... if it was meant to be a response to Member 11221115 then they will not have seen your response because it has been posted in an inappropriate place. If it wasn't for that and was meant to be a response to a 4-year-old resolved question then be aware that this will usually attract down-votes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 144
1 Maciej Los 129
2 Sascha Lefévre 110
3 Abhinav S 104
4 Richard MacCutchan 90
0 Sergey Alexandrovich Kryukov 6,578
1 OriginalGriff 6,298
2 Peter Leow 2,534
3 Abhinav S 2,358
4 Maciej Los 2,352


Advertise | Privacy | Mobile
Web04 | 2.8.150414.1 | Last Updated 26 Feb 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100