locked
(Date Comparision) string was not recognized as a valid datetime in c# RRS feed

  • Question

  • Friends,

    Please find my code as follows;

     
    objsptfsnotegen.sptfqryargs = "select CONVERT(varchar(10),DATEPART(MM,MAX(iinfill_completion)))+'/'+CONVERT(varchar(10),DATEPART(DD,MAX(iinfill_completion)))+'/'+CONVERT(varchar(10),DATEPART(YYYY,MAX(iinfill_completion))) completionyear from bg_sptfinfillingreports repa join bg_sptfAppliedArea apa on apa.pKey_applied_area_id=repa.pKey_applied_area_id where tran_id=" + hiddentranid.Value;
            
                String m_completiondate = objsptfsnotegen.GetCustomeData();
                if (m_completiondate != "")
                {
                    if (!string.IsNullOrEmpty(m_completiondate))
                    {
                        DateTime dt1 = DateTime.Parse(m_completiondate);
                        DateTime dt2 = DateTime.ParseExact("12/10/2014", "MM/dd/yyyy", CultureInfo.InvariantCulture);
                        DateTime date1 = new DateTime(dt1.Year, dt1.Month, dt1.Day);
                        DateTime date2 = new DateTime(dt2.Year, dt2.Month, dt2.Day);
    
                        int result = DateTime.Compare(date1, date2);
    
                        if (result < 0)
                            objsptfsnotegen.sptfqryargs = "select top 1 CONVERT(varchar(50),Year_Announce) years,Scheme_Amount from scheme_anoncement sa join scheme_master sm on sm.Scheme_ID=sa.Scheme_ID where Scheme_Type_ID=1 and Scheme_Name='" + m_activityname + "_BG' and Year_Announce<='" + m_completiondate + "' and scheme_region='" + hiddenregion.Value + "' order by years desc";
                        else if (result == 0)
                            objsptfsnotegen.sptfqryargs = "select top 1 CONVERT(varchar(50),Year_Announce) years,Scheme_Amount from scheme_anoncement sa join scheme_master sm on sm.Scheme_ID=sa.Scheme_ID where Scheme_Type_ID=9 and Scheme_Name='" + m_activityname + "_BG' and Year_Announce<='" + m_completiondate + "' and scheme_region='" + hiddenregion.Value + "' order by years desc";
                        else
                            objsptfsnotegen.sptfqryargs = "select top 1 CONVERT(varchar(50),Year_Announce) years,Scheme_Amount from scheme_anoncement sa join scheme_master sm on sm.Scheme_ID=sa.Scheme_ID where Scheme_Type_ID=9 and Scheme_Name='" + m_activityname + "_BG' and Year_Announce<='" + m_completiondate + "' and scheme_region='" + hiddenregion.Value + "' order by years desc";
                    }  

    The error is coming where i bold in code... Err. Name string was not recognized as a valid datetime in c#...

    I had tried many ways but i can't. Please help me to fix this issue.

    Thanks & Regards,


    Wednesday, January 13, 2016 9:42 AM

Answers

  • This could be a culture issue. The date produced from the query is presumably in US format (month/day/year), but you could be running this on a PC defaulting to another format such as UK (which is day/month/year).

    Use ParseExact if you know exactly what format your input string is. See the examples here.

    P.S. Storing dates or as strings or getting them from the database as a string is a Bad Idea :). You would be much better off altering your database query to simply return a Date (or DateTime) in the first place rather than using CONVERT to turn it into a string.

    Then you wouldn't have this conversion issue in the first place and your code would be much more portable.

    E.g. Use the DateFromParts function or equivalent.

    • Proposed as answer by Spiri91 Wednesday, January 13, 2016 10:59 AM
    • Marked as answer by Kristin Xie Wednesday, January 20, 2016 2:06 AM
    Wednesday, January 13, 2016 10:12 AM

All replies


  • Can you show us how
    completiondate 
    looks before the if statement?

    Hope it helps. Spiri

    Wednesday, January 13, 2016 9:48 AM
  • For Example it comes like as follows;

    "6/18/2015"

    Wednesday, January 13, 2016 9:58 AM
  • This could be a culture issue. The date produced from the query is presumably in US format (month/day/year), but you could be running this on a PC defaulting to another format such as UK (which is day/month/year).

    Use ParseExact if you know exactly what format your input string is. See the examples here.

    P.S. Storing dates or as strings or getting them from the database as a string is a Bad Idea :). You would be much better off altering your database query to simply return a Date (or DateTime) in the first place rather than using CONVERT to turn it into a string.

    Then you wouldn't have this conversion issue in the first place and your code would be much more portable.

    E.g. Use the DateFromParts function or equivalent.

    • Proposed as answer by Spiri91 Wednesday, January 13, 2016 10:59 AM
    • Marked as answer by Kristin Xie Wednesday, January 20, 2016 2:06 AM
    Wednesday, January 13, 2016 10:12 AM