-1

Ola,

Estou a criar um programa para aprender um pouco mais sobre .NET C#, esse projeto é um projeto simples de CRUD.

Mas estou com um problema na minha model, quando executo o programa pra criar um novo registro ele está a me dar problema no campo de data.

Quando eu tento desta forma, me da este problema...

namespace CRUD.Models.Entities
{
[Table("Lyrics")]
public class Songs
{
    [Key]
    [Display(Description = "Code of the song")]
    public int ID_Song { get; set; }

    [Display(Description = "Title of the song")]
    public string Name_song { get; set; }

    [Display(Description = "Nome of the Album's song")]
    public string Album { get; set; }

    [Display(Description = "Band name")]
    public string Band { get; set; }

    [Display(Description = "Lyrics of the song")]
    public string Lyrics { get; set; }

    [DataType(DataType.Time)]
    [Display(Description = "Duration of the song")]
    public DataType Duration { get; set; }
}
}

An unhandled exception occurred while processing the request. FormatException: Format string can be only "G", "g", "X", "x", "F", "f", "D" or "d".

Achei aqui no stackoverflow uma possível solução e apliquei neste campo, mas agora me está a me dar outro erro que não consigo resolver.

namespace CRUD.Models.Entities
{
[Table("Lyrics")]
public class Songs
{
    [Key]
    [Display(Description = "Code of the song")]
    public int ID_Song { get; set; }

    [Display(Description = "Title of the song")]
    public string Name_song { get; set; }

    [Display(Description = "Nome of the Album's song")]
    public string Album { get; set; }

    [Display(Description = "Band name")]
    public string Band { get; set; }

    [Display(Description = "Lyrics of the song")]
    public string Lyrics { get; set; }

    [DataType(DataType.Time)]
    [DisplayFormat(DataFormatString = "{hh:mm:ss[.nnnnnnn]}", ApplyFormatInEditMode = true)]
    [Display(Description = "Duration of the song")]
    public DataType Duration { get; set; }
}
}

An unhandled exception occurred while processing the request. FormatException: Input string was not in a correct format. System.Text.StringBuilder.FormatError()

Na tabela que já existe na minha base de dados tem um campo Datetime da seguinte forma.

02:38:00.0000000 (hh:mm:ss[.nnnnnnn])

Como consigo resolver esse problema? Alguém consegue me ajudar?

Obrigado.

1
  • 1
    já tentou usar string? public string Duration 21/04/2021 às 22:25

1 Resposta 1

0

Você pode usar o tipo TimeSpan.
Por exemplo:

TimeSpan timeSpan = new TimeSpan(7, 15, 33);  
Console.WriteLine(timeSpan.ToString()); // "07:15:33"  

Então fica assim:

[Display(Description = "Duration of the song")]
[DataType(DataType.Time)]
public TimeSpan Duration { get; set; }

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .