Problem Retrieving Null Data for a DataTable in C#

Question:

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

When I then do a select to retrieve all the data and assign it 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;
        dtDados.Load(_comando.ExecuteReader());
        DesconectaBanco();
    }
       return dtDados;
 }

Where do I have the following corresponding table in the database

CREATE TABLE conta
(
  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)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE conta
  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 don't know why this happens, but when I went back to a previous version of npgsql I managed to solve the problem.

Scroll to Top