locked
conditional date formating in a webgrid - unexpected results RRS feed

  • Question

  • User379720387 posted

    column Date has null in some rows and a Date in other.

    Looks like it doesn't recognize the ternary operation I want to do.

    grid.Column("Date", format: @<text>@item.Date != null ? item.Date.ToSString("ddd") : “tbd”)</text>),

    results in:

    06/30/2015 != null ? item.Date.ToShortDateString() : “tbd”)

    Tuesday, June 30, 2015 5:58 PM

Answers

  • User-821857111 posted

    It seems to think that item.Date is a string data type. You'll need to convert that to a DateTime (assuming it's in a valid format):

    grid.Column("Date", format: @<text>@(item.Date != null ? Convert.ToDateTime(item.Date).ToString("ddd") : "tbd"))</text>),

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 1, 2015 11:54 AM

All replies

  • User1853794821 posted

    Try:

    grid.Column("Date", format: @<text>@(item.Date != null ? item.Date.ToSString("ddd") : “tbd”))</text>),

    Tuesday, June 30, 2015 6:43 PM
  • User379720387 posted

    thanks for taking a look at this

    There was an extra S in your code but the result is the same regardless....

    The best overloaded method match for 'string.ToString(System.IFormatProvider)' has some invalid arguments

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The best overloaded method match for 'string.ToString(System.IFormatProvider)' has some invalid arguments

    Source Error:

     

    Line 71:                 columns: grid.Columns(

    Line 72:                    grid.Column(format: @<a href="~/AddDate?id=@item.CID">Enter Date</a>),

    Line 73:                    grid.Column("Date", "Date", format: @<text>@(item.Date != null ? item.Date.ToString("ddd") : "")</text>),

    Tuesday, June 30, 2015 8:38 PM
  • User-821857111 posted

    grid.Column("Date", format: @<text>@(item.Date != null ? item.Date.ToString("ddd") : "tbd"))</text>),

    You had two SS in ToSString

    Wednesday, July 1, 2015 8:56 AM
  • User379720387 posted

    Server Error in '/' Application.


    The best overloaded method match for 'string.ToString(System.IFormatProvider)' has some invalid arguments

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The best overloaded method match for 'string.ToString(System.IFormatProvider)' has some invalid arguments

    Source Error:

    Line 71:                 columns: grid.Columns(
    Line 72:                    grid.Column(format: @<a href="~/AddDate?id=@item.CID">Enter Date</a>),
    Line 73:                    grid.Column("Date", format: @<text>@(item.Date != null ? item.Date.ToString("ddd") : "tbd"))</text>),
    Line 74:                     grid.Column("Date"),
    Line 75:                     grid.Column("TSStartTime"),
    Wednesday, July 1, 2015 11:07 AM
  • User-821857111 posted

    It seems to think that item.Date is a string data type. You'll need to convert that to a DateTime (assuming it's in a valid format):

    grid.Column("Date", format: @<text>@(item.Date != null ? Convert.ToDateTime(item.Date).ToString("ddd") : "tbd"))</text>),

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 1, 2015 11:54 AM
  • User379720387 posted

    Oops, Date was a nvarchar.

    Thanks.

    Someone could make some money taking all these useless (for most of us) Microsoft error messages and translate them to something that makes sense.

    Wednesday, July 1, 2015 2:19 PM
  • User1853794821 posted

    Could this be the cause?

    https://www.sqlite.org/datatype3.html

    Wednesday, July 1, 2015 2:23 PM
  • User379720387 posted

    Nope this is SQL Compact Edition

    Wednesday, July 1, 2015 2:29 PM