locked
Cannot implicitly convert type 'string' to 'System.DateTime' RRS feed

  • Question

  • User2109257026 posted

    I am getting the following error message Cannot implicitly convert type 'string' to 'System.DateTime'

    please see code highlighted below.

    f.Date = dt.Rows[0]["Date Performed"].ToString();

    Thanks,

            public Feedback GetFeedbackObject(int recordNumber)
            {
                //get feedback datatable
                DataTable dt = dal.GetData("select * from NCRSDB.[dbo].[AR_tbl_PerfMan_ParentTable] where AuditPerformed_ID = '" + recordNumber + "'");
    
                //convert dt to feedback object
                Feedback f = new Feedback();
    
                f.RecordNumber = Convert.ToInt32(dt.Rows[0]["Parent_Id"]);
                f.StaffName = dt.Rows[0]["Ward Clerk"].ToString();
                f.Ward = dt.Rows[0]["Ward Name"].ToString();
                f.Date = dt.Rows[0]["Date Performed"].ToString();
                f.UnitNos = dt.Rows[0]["Unit Numbers Audited"].ToString();
    
    
                DateTime d1;
                if (DateTime.TryParse(dt.Rows[0]["Date Performed"].ToString(), out d1))
                {
                    f.Date = d1;
                }
    
                DateTime d2;
                if (DateTime.TryParse(dt.Rows[0]["Date of Divisional Feedback"].ToString(), out d2))
                {
                    f.Date = d2;
                }
    
    
                f.ActionsList = GetActionList(recordNumber);
    
                return f;
            }

    Friday, July 11, 2014 9:40 AM

Answers

  • User1401801381 posted

    f.Date = dt.Rows[0]["Date Performed"].ToString();

    it seems that your f.Date is a DateTime

    so you can do this:

    f.Date = dt.Rows[0].Field<DateTime>("Date Performed");

    (but you have to add a reference : System.Data.DataSetExtension

    or you can try

    f.Date =  Convert.ToDateTime(dt.Rows[0]["Date Performed"]);

    but check first if the value is not null

    if(dt.Rows[0].IsNull("Date Performed") == false)
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 11, 2014 9:54 AM

All replies

  • User753101303 posted

    Hi,

    f.Date is declared as a date so you should assign a date rather than a string to it that is don't use :

    f.Date = dt.Rows[0]["Date Performed"].ToString();

    but rather:

    f.Date = dt.Rows[0]["Date Performed"];

    You may have to take an extra step if the column could be NULL. Is this the case ?

    It's also a bit unclear why you are testing dates a bit later. Are they really dates in the db ? If yes it seems to be pointless to convert those dates to a string and then check again if this string is a valid date. Just process data under their format. usually you convert to a string as late as possible (ie when jyou show the value to a user) and convert to the native type as soon as possible.

    BTW not sure what is the db you are using but what you are doing is the purpose of Entity Framework. It does expose your db data as .NET objects. See http://msdn.microsoft.com/en-US/data/ef for details...

    Friday, July 11, 2014 9:49 AM
  • User1401801381 posted

    f.Date = dt.Rows[0]["Date Performed"].ToString();

    it seems that your f.Date is a DateTime

    so you can do this:

    f.Date = dt.Rows[0].Field<DateTime>("Date Performed");

    (but you have to add a reference : System.Data.DataSetExtension

    or you can try

    f.Date =  Convert.ToDateTime(dt.Rows[0]["Date Performed"]);

    but check first if the value is not null

    if(dt.Rows[0].IsNull("Date Performed") == false)
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 11, 2014 9:54 AM