"arithmetic Exception, Numeric overflow, or string truncation string right truncation" in search using LINQ

Asked

Viewed 1,170 times

2

I have a table in a Firebird database

CREATE TABLE CIDADE (
CID_CD              SMALLINT NOT NULL,
CID_DS              CHAR(20) NOT NULL,
CID_UF              CHAR(2) NOT NULL,
CID_DISTANCIA_SEDE  SMALLINT NOT NULL,
CID_CD_ALTERNATIVO  INTEGER NOT NULL,
CID_DT_LK           DATE NOT NULL);

I am making a query using LINQ in the columns of this table and I am getting the error in the columns of type CHAR:

arithmetic Exception, Numeric overflow, or string truncation string right truncation

Identifying the problem, I realized that the size of the fields of the String type are breaking the limit of the fields. What I don’t understand is that I’m passing exactly limit amount of fields.

Just follow my code:

  public IQueryable<Cidade> Pesquisar(Cidade cidade)
    {
        string uf = cidade.UF; // "SP" por exemplo
        var query = pctxContexto.Cidade.Where(c=> c.UF.Contains(uf));
        return query;
    }
}

Mapping:

//Mapeamento de tabela
        ToTable("CIDADE");

        //Chave primária
        HasKey(t => new { t.Codigo });

        //Propriedades
        Property(t => t.Codigo).HasColumnName("CID_CD");

        Property(t => t.Descricao)
            .IsRequired()
            .HasMaxLength(20)
            .HasColumnType("Varchar")
            .HasColumnName("CID_DS");

        Property(t => t.UF)
            .IsRequired()
            .HasMaxLength(2)
            .HasColumnType("Char")
            .HasColumnName("CID_UF");

        Property(t => t.DistanciaSede)
            .IsRequired()
            .HasColumnType("Smallint")
            .HasColumnName("CID_DISTANCIA_SEDE");

        Property(t => t.Codigo_Alternativo)
            .IsRequired()
            .HasColumnType("Int")
            .HasColumnName("CID_CD_ALTERNATIVO");

 public Cidade()
    {
        Codigo = null;
        Descricao = string.Empty;
        UF = string.Empty;
        DistanciaSede = 0;
        Codigo_Alternativo = 0;
    }

    [Key]
    public int? Codigo { get; set; }

    public string Descricao { get; set; }

    public string UF { get; set; }

    public Int16 DistanciaSede { get; set; }

    public Int32 Codigo_Alternativo { get; set; }

Could someone explain to me why this mistake and what is the most elegant way to solve?

  • 1

    Post your class Cidade, please.

  • I edited the question by adding the city class

  • Post also your class Codigo, please.

  • 1

    Your class Cidade has a field Key that is nullable? This can’t be right.

  • Code is not class is an integer field and can be null if null a Trigger adds code.

1 answer

1


The most elegant way to solve is by decorating attributes. Dispenses up to the Fluent API that you’re using:

public class Cidade
{
    public Cidade()
    {
        Codigo = null;
        Descricao = string.Empty;
        UF = string.Empty;
        DistanciaSede = 0;
        Codigo_Alternativo = 0;
    }

    [Key]
    public int? Codigo { get; set; }
    [StringLength(20)]
    public string Descricao { get; set; }
    [StringLength(2)]
    public string UF { get; set; }

    public Int16 DistanciaSede { get; set; }

    public Int32 Codigo_Alternativo { get; set; }
}

If you use @Html.EditorFor() in Views, HTML fields are already generated with character limitation. [StringLength] also supports placing a custom error message per attribute.


EDIT

I don’t know if it could be because you’re wearing like, but I wouldn’t use contains for state acronym search:

var query = pctxContexto.Cidade.Where(c=> c.UF == uf);
  • Out of curiosity, the fact that in the table the field CID_CD be whole and key and in the class be an object of the type Cidade it’s not supposed to go wrong?

  • This primary key is really weird. It doesn’t even need that Fluent API that he posed. I just didn’t understand the problem with Cidade. In my view, the class is correct, at least in the statement.

  • My mistake of interpretation.

  • The error happens even when I assign 2 characters (which is the field limit). It adds "%SP%" for example and generates the error. Follow the generated query: SELECT &#xA;*&#xA;FROM "CIDADE" AS "C"&#xA;WHERE "C"."CID_UF" LIKE @p__linq__0 ESCAPE CAST(_UTF8'\' AS VARCHAR(8191))&#xA;&#xA;Parameters:&#xA;Name:p__linq__0 Type:VarChar Used Value:%MG%

  • @Paulohenrique I updated the answer.

  • thanks for everyone’s attention, in fact, I’ve done some tests here and actually the error occurs because it’s using contains. I can even change the code to c=> c.UF.Equals(uf) for acronym that is more viable however, the error happens in description also that in this case the like would be ideal.

  • @Paulohenrique The answer answers your question? See here the next steps. Thank you!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.