Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
String was not recognized as a valid DateTime.Couldn't store <25/12/2013> in ClosingDate Column. Expected type is DateTime.

If i give the date less than 12 means 11/12/2013 then it works fine but if i give it greater than 12 means 25/12/2013 the it gives this error.Why is this error coming?


C#
private int _id;

    private DateTime _ClosingDate;
    public DateTime ClosingDate
    {
        get
        {
            return _ClosingDate;
        }
        set
        {
           _ClosingDate = value;
        }
    }


private List<PotentialEntity> fetchrecords(DataTable FTable)
        {
            List<PotentialEntity> Entity = new List<PotentialEntity>();
            foreach (DataRow oRow in FTable.Rows)
            {
                PotentialEntity pEntity = new PotentialEntity();                
                pEntity.ClosingDate =(DateTime)(oRow["ClosingDate"]);                
                Entity.Add(pEntity);
            }
            return Entity;
        }

GridView1.DataSource = Entity;
        GridView1.DataBind()

ASP.NET
<asp:TemplateField HeaderStyle-Width="100px" HeaderStyle-ForeColor="White" meta:resourcekey="TemplateFieldResource5">
            <itemtemplate>
                <asp:Label ID="lblClosingDate" runat="server" Text='<%# Eval("ClosingDate").ToString().Substring(0,10) %>'
                    meta:resourcekey="lblClosingDateResource1"/></itemtemplate>
            <HeaderStyle Width="100px" />
Posted
Updated 29-Dec-13 21:15pm
v7
Comments
Aydin Homay 28-Dec-13 3:25am    
Please explain your problem, Do you want convert String to DateTime ?
Sumit Bhargav 28-Dec-13 3:29am    
Exactly,I am trying to save a string of date "28/12/2013" into a datetime data type in sql table
via stored procedure expecting a datetime variable.I tried many ways but whenever i try to save the days of a date greater than 12 it throw me that error.What could be the possible reason?
Aydin Homay 28-Dec-13 3:50am    
Why you don`t use nvarchar data type ? it can make good facilities in implementation and you can handle data converting string to date time in the application layer.
Sumit Bhargav 28-Dec-13 3:55am    
Hi aydin,
The question was for converting string to datetime not for datetime to nvarchar.
Richard MacCutchan 28-Dec-13 4:38am    
Do not use strings to save dates in a database.

 
Share this answer
 
I still don't understand the solution of this problem but it has been resolved.
In the database i was fetching the records while converting the date in the format like this
convert(varchar(10),datecolumn,103) but when i change this to convert(varchar(10),datecolumn,110) it starts working,now it's working fine but i still don't get this y it wasn't working when i was
taking the format of 103.
I humbly thanks to all those who helped me in resolving this issue.
 
Share this answer
 
Comments
CHill60 31-Dec-13 8:51am    
Convert style 103 is UK style dd/mm/yyyy whereas 110 is US format mm-dd-yyyy. If you retrieved the original date from the database with style 103 and stored it into a string in your program then the string would have held the date in the wrong format. See my solution (6) below for a better approach. It is also better to use the correct data type throughout.
Member 11665191 5-May-15 4:59am    
This solution worked for me too.
try
string Date="28/12/2013";
DateTime Dt = DateTime.Now(); 
DateTime.TryParse(string,out Dt);
 
Share this answer
 
C#
string value = oRow["ClosingDate"] + "";
       // keep a break point and check the value, in which format the value appears
       // based on the format you can change the format value
       string format = "MM/dd/yyyy";
      var datetime =  DateTime.ParseExact(value,format,System.Globalization.CultureInfo.InvariantCulture );





Reference:
Date Time casting..[^]
 
Share this answer
 
Comments
Sumit Bhargav 30-Dec-13 4:40am    
the datetime format of the string is 30/12/2013 12:00:00
What will be the format now?
Karthik_Mahalingam 30-Dec-13 4:48am    
"dd/MM/yyyy hh:mm:ss"
Sumit Bhargav 30-Dec-13 5:38am    
i did this but it says string is not a valid datetime
Karthik_Mahalingam 30-Dec-13 5:42am    
"30/12/2013 12:00:00 "
to
"30/12/2013 12:00:00"
remove empty space at the end..
Sumit Bhargav 30-Dec-13 6:13am    
hi karthik,
i tried as per you said,still it gives the same error
I guess that you have american date culture. Therefore 28/12/2013 is 12th day, 28th month. Clearly such a date doesnt exist so it throws an exception. Try to change the string to 12/28/2013 to be sure this is not the problem. TryParse or Parse is the way as was already said...
 
Share this answer
 
this is because it is displaying in "MM/dd/yyyy" not in "dd/MM/yyyy".

try

((DateTime)(oRow["ClosingDate"])).ToString("dd/MM/yyyy")


this will change it to dd/MM/yyyy format
 
Share this answer
 
Further to the other solutions, you can avoid issues with dates and localised presentations of dates by always using ISO 8601 ("International") format dates yyyy-mm-dd - see http://en.wikipedia.org/wiki/ISO_8601[^]

Another method is to use the unambiguous date format "dd-MMM-yyyy" e.g. "25-DEC-2013" - most databases will accept this input.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900