Problem retrieving null Data for a DataTable in C #

Posted on

Question :

I’m working with C # and the PostGreSQL Database, I have a field of type Date where it can be filled in dd / mm / yyyy or empty null. the same problem is related to any field of the table, if it is empty, even if it may be, the error occurs.

When I do a select to retrieve all data and assign to a DataTable the following error occurs

An exception of type 'System.Data.ConstraintException' occurred in System.Data.dll but was not handled in user code

Additional information: Falha ao ativar restrições. Uma ou mais linhas contêm valores que violam as restrições non-null, unique ou foreign-key.

Method Used

public DataTable RetornaDT(string sSQL)
    DataTable dtDados = new DataTable();
    if (ConectaBanco())
        _comando.CommandText = sSQL;
       return dtDados;

Where do I have the following table in the database

  con_codigo serial NOT NULL,
  con_descricao character varying(60),
  con_valordaconta real,
  con_valoraserpago real,
  con_valorjapago real,
  con_formapagamento character varying(2),
  con_dinheiroouporc character varying(2),
  con_desconto real,
  con_juros real,
  con_jurosam real,
  con_multaporatraso real,
  con_datalancamento date,
  con_datavencimento date,
  con_datapagamento date,
  CONSTRAINT contas_pkey PRIMARY KEY (con_codigo)
  OWNER TO postgres;


Answer :

The problem is the version used of npgsql, version 3.2 requires that the data is not null or empty, well I do not know why this occurs, but when returning to an earlier version of npgsql I was able to solve the problem. >

Depending on the version used in Npgsql this error can occur, one way to solve this is to treat this in your sql, instead of sending the field as null you can change it and send it blank.

CASE WHEN DateColuna IS NULL THEN '' ELSE to_char(DateColuna, 'dd/mm/yyyy') END As Data


Leave a Reply

Your email address will not be published. Required fields are marked *