Problem retrieving null Data for a DataTable in C #

3

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;
        dtDados.Load(_comando.ExecuteReader());
        DesconectaBanco();
    }
       return dtDados;
 }

Where do I have the following 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;
    
asked by anonymous 22.03.2017 / 22:27

2 answers

1

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. >     

24.03.2017 / 13:34
0

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
    
23.03.2017 / 14:08