I have a problem that I can not solve because I do not see the error. I am developing a simple project in C #, when I execute a stored procedure of Postgresql (Insert):
pgsqlCommand cmd = new NpgsqlCommand("select insertclientes(" + @idcuenta_corriente + "," +
"'" + @nombre+ "','" + @apellido + "'," +
"'" + @domicilio + "'," + @idprovincia + "," +
"" + @idlocalidad + "," + @idtipo_identificador + "," +
"" + @numero_identificador + "," + @idcondicion_iva + "," +
"'" + @email + "'," + @telefono + "," + @idretenciones + "," + @idlistaprecios + ");", Connections());
It is not reflected in the database, that is, it does not make the insert, but it does not show any errors either. To see the output of the program and print the sql console do this:
Console.Write("select insertclientes(" + @idcuenta_corriente + "," +
"'" + @nombre + "','" + @apellido + "'," +
"'" + @domicilio + "'," + @idprovincia + "," +
"" + @idlocalidad + "," + @idtipo_identificador + "," +
"" + @numero_identificador + "," + @idcondicion_iva + "," +
"'" + @email + "'," + @telefono + "," + @idretenciones + "," + @idlistaprecios + ");");
It gives me as a result:
select insertclientes(1,'dfhsfhdfshsdfhsdf','dfhfhfh','ghfkghfkfgh',0,0,0,444,0,'hkjhkhjkhgjk',111,2,2);El programa '[15836] Contable1.exe: Seguimiento de programa' terminó con código 0 (0x0).
Now if I copy the query and the esdcuto from PgAdmin the data is inserted satisfactorily.
I pass the full script of the data layer:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//contableEntidad
using System.Windows;
using contableEntidad;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using Npgsql;
namespace contableDatos
{
public class DContable
{
public NpgsqlConnection Connections() {
var stringConnection = "Server=127.0.0.1;Port=5432;User Id=postgres;Password=admin;Database=contable";
NpgsqlConnection SQLPostgres = new NpgsqlConnection(stringConnection);
if (!string.IsNullOrWhiteSpace(stringConnection))
{
try
{
SQLPostgres = new NpgsqlConnection(stringConnection);
SQLPostgres.Open();
Console.WriteLine("conectado ");
}
catch (Exception)
{
SQLPostgres.Close();
Console.WriteLine("NO conectado ");
}
}
return SQLPostgres;
}
//Listar clientes
public DataTable Clientes() {
//NpgsqlDataAdapter dataContable = new NpgsqlDataAdapter("select listarclientes()", SQLPostgres);
//NpgsqlCommand cmd = new NpgsqlCommand("SELECT * from cliente", Connections());
//cmd.CommandType = CommandType.StoredProcedure;
NpgsqlDataAdapter Da = new NpgsqlDataAdapter("SELECT * from cliente", Connections());
DataTable Dt = new DataTable();
// DataSet ds = new DataSet();
Da.Fill(Dt);
return Dt;
}
//Listar
public List<Paises> Paiseslist()
{
List<Paises> _lista = new List<Paises>();
// List<string> listaPaises = new List<string>();
NpgsqlCommand cmd = new NpgsqlCommand("SELECT * FROM pais", Connections());
///// //cmd.CommandType = CommandType.StoredProcedure;
NpgsqlDataAdapter Da = new NpgsqlDataAdapter("SELECT * FROM pais", Connections());
NpgsqlDataReader _reader = cmd.ExecuteReader();
DataTable Dt = new DataTable();
DataSet ds = new DataSet();
Da.Fill(Dt);
for (var i = 0; _reader.Read(); i++) {
//listaPaises.Add(_reader[0].ToString());
Paises paises = new Paises();
paises.codigo = _reader.GetInt32(0);
paises.pais = _reader.GetString(1);
// listaPaises.Add(_reader[2].ToString());
_lista.Add(paises);
}
return _lista;
//return listaPaises;
}
//Listar identificador
public List<Tidentificacion> TipoIdentificador()
{
List<Tidentificacion> _lista = new List<Tidentificacion>();
NpgsqlCommand cmd = new NpgsqlCommand("SELECT * FROM public.tipo_identificador", Connections());
NpgsqlDataReader _reader = cmd.ExecuteReader();
for (var i = 0; _reader.Read(); i++)
{
Tidentificacion tidenti = new Tidentificacion();
tidenti.idtipo_identificacion = _reader.GetInt32(0);
tidenti.identificador = _reader.GetString(1);
_lista.Add(tidenti);
}
return _lista;
}
//Listar codigo de iva
public List<Codigoiva> codigoIVA()
{
List<Codigoiva> _lista = new List<Codigoiva>();
NpgsqlCommand cmd = new NpgsqlCommand("SELECT * FROM public.codiva", Connections());
NpgsqlDataReader _reader = cmd.ExecuteReader();
for (var i = 0; _reader.Read(); i++)
{
Codigoiva civa = new Codigoiva();
civa.codigo_iva = _reader.GetInt32(0);
civa.descripcion_iva = _reader.GetString(1);
_lista.Add(civa);
}
return _lista;
}
//localidad
public List<Localidad> listLocalidad()
{
List<Localidad> _lista = new List<Localidad>();
NpgsqlCommand cmd = new NpgsqlCommand("SELECT * FROM public.localidad", Connections());
NpgsqlDataReader _reader = cmd.ExecuteReader();
for (var i = 0; _reader.Read(); i++)
{
Localidad llocalidad = new Localidad();
llocalidad.idlocalidad = _reader.GetInt32(0);
llocalidad.cod_descripcion = _reader.GetString(1);
_lista.Add(llocalidad);
}
return _lista;
}
//provincia
public List<Provincia> listProvincia()
{
List<Provincia> _lista = new List<Provincia>();
NpgsqlCommand cmd = new NpgsqlCommand("SELECT * FROM public.provincia", Connections());
NpgsqlDataReader _reader = cmd.ExecuteReader();
for (var i = 0; _reader.Read(); i++)
{
Provincia lprovincia = new Provincia();
lprovincia.id_provincia = _reader.GetInt32(0);
lprovincia.provincia = _reader.GetString(1);
_lista.Add(lprovincia);
}
return _lista;
}
public void insertClient(
int idcuenta_corriente,
string nombre,
string apellido,
string domicilio,
int idprovincia,
int idlocalidad,
int idtipo_identificador,
int numero_identificador,
int idcondicion_iva,
string email,
int telefono,
int idretenciones,
int idlistaprecios) {
NpgsqlCommand cmd = new NpgsqlCommand("select insertclientes(" + @idcuenta_corriente + "," +
"'" + @nombre+ "','" + @apellido + "'," +
"'" + @domicilio + "'," + @idprovincia + "," +
"" + @idlocalidad + "," + @idtipo_identificador + "," +
"" + @numero_identificador + "," + @idcondicion_iva + "," +
"'" + @email + "'," + @telefono + "," + @idretenciones + "," + @idlistaprecios + ");", Connections());
//cmd.CommandType = CommandType.StoredProcedure;
Console.Write("select insertclientes(" + @idcuenta_corriente + "," +
"'" + @nombre + "','" + @apellido + "'," +
"'" + @domicilio + "'," + @idprovincia + "," +
"" + @idlocalidad + "," + @idtipo_identificador + "," +
"" + @numero_identificador + "," + @idcondicion_iva + "," +
"'" + @email + "'," + @telefono + "," + @idretenciones + "," + @idlistaprecios + ");");
}
}
}
For those who do not want to see the complete script, this is the method I use to perform the insert with the procedure.
public void insertClient(
int idcuenta_corriente,
string nombre,
string apellido,
string domicilio,
int idprovincia,
int idlocalidad,
int idtipo_identificador,
int numero_identificador,
int idcondicion_iva,
string email,
int telefono,
int idretenciones,
int idlistaprecios) {
NpgsqlCommand cmd = new NpgsqlCommand("select insertclientes(" + @idcuenta_corriente + "," +
"'" + @nombre+ "','" + @apellido + "'," +
"'" + @domicilio + "'," + @idprovincia + "," +
"" + @idlocalidad + "," + @idtipo_identificador + "," +
"" + @numero_identificador + "," + @idcondicion_iva + "," +
"'" + @email + "'," + @telefono + "," + @idretenciones + "," + @idlistaprecios + ");", Connections());
//cmd.CommandType = CommandType.StoredProcedure;
Console.Write("select insertclientes(" + @idcuenta_corriente + "," +
"'" + @nombre + "','" + @apellido + "'," +
"'" + @domicilio + "'," + @idprovincia + "," +
"" + @idlocalidad + "," + @idtipo_identificador + "," +
"" + @numero_identificador + "," + @idcondicion_iva + "," +
"'" + @email + "'," + @telefono + "," + @idretenciones + "," + @idlistaprecios + ");");
}
}