locked
XML dateFormat RRS feed

  • Question

  • User-543160537 posted

    Hi,

    I am trying to format the date column in XML generation, can someone plz help me to show as expected format..

    foreach (var item in uniqueEventIds)
                    {
                        DataTable EventDetails = (from DataRow dr in SelectedRows.Rows
                                                  where dr["Event Id"].ToString() == item.ToString()
                                                  select dr).CopyToDataTable();
    
                        writer.WriteStartElement("Event");                   
                        writer.WriteElementString("StartDate", EventDetails.Rows[0]["StartDate"].ToString("M/d/yyyy")); // Error : No overload for method 'Tostring' takes 1 arguments
    }
          

    I am betting below error..

    No overload for method 'Tostring' takes 1 arguments

    pls advice

    Thanks

    Sree
     

    Monday, September 21, 2015 3:54 PM

Answers

  • User-986267747 posted

    Hi sreeKanth.jonna

    sreekanth.jonna

    writer.WriteElementString("StartDate", EventDetails.Rows[0]["StartDate"].ToString("M/d/yyyy")); //

    You need to cast it to DateTime first:

    DateTime leave = (DateTime) EventDetails.Rows[0]["StartDate"];

    writer.WriteElementString("StartDate,leave.ToString("M/d/yyyy")); //or writer.WriteElementString("StartDate",((DateTime)rdMonthlyLeave["LEAVE_DATE"])).ToString("M/d/yyyy"));

    The return type of the EventDetails.Rows[0]["StartDate"] is just object, and object doesn't have an overload of ToString which takes a string. Don't forget that overloading is a compile-time decision - the compiler picks the appropriate method with a compatible signature, and only overriding occurs based on the execution-time type. In this case there is no overload of ToString with a compatible signature, so you get a compile-time error.

    I hope it's helpful to you.

    Best Regards

    Klein zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 22, 2015 2:15 AM

All replies

  • User2053451246 posted

    Call .ToString() once before doing in the format you want.

    writer.WriteElementString("StartDate", EventDetails.Rows[0]["StartDate"].ToString().ToString("M/d/yyyy"));

    Monday, September 21, 2015 4:05 PM
  • User37182867 posted

    EventDetails.Rows[0]["StartDate"] is probably not of the DateTime.

    Please check the type of that object.

    Looking at the error, it is probably a string. Which means you either need to cast it as a Date prior to writing your XML or you first need to cast it to date and then back to a formated string when writing your xml element.

    I would suggest doing it prior to writing your XML, I would do it when your are gathering your data from what ever data source you are pulling from.

    Monday, September 21, 2015 4:05 PM
  • User-986267747 posted

    Hi sreeKanth.jonna

    sreekanth.jonna

    writer.WriteElementString("StartDate", EventDetails.Rows[0]["StartDate"].ToString("M/d/yyyy")); //

    You need to cast it to DateTime first:

    DateTime leave = (DateTime) EventDetails.Rows[0]["StartDate"];

    writer.WriteElementString("StartDate,leave.ToString("M/d/yyyy")); //or writer.WriteElementString("StartDate",((DateTime)rdMonthlyLeave["LEAVE_DATE"])).ToString("M/d/yyyy"));

    The return type of the EventDetails.Rows[0]["StartDate"] is just object, and object doesn't have an overload of ToString which takes a string. Don't forget that overloading is a compile-time decision - the compiler picks the appropriate method with a compatible signature, and only overriding occurs based on the execution-time type. In this case there is no overload of ToString with a compatible signature, so you get a compile-time error.

    I hope it's helpful to you.

    Best Regards

    Klein zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 22, 2015 2:15 AM