I cannot insert and save data on a table in my window forms program.
The code on window forms is:
private void BtnInsert_Click(object sender, EventArgs e)
{
rowIndex = -1;
textIdentificacao.Enabled = textNome.Enabled = true;
textIdentificacao.Text = textNome.Text = null;
textIdentificacao.Select();
}
private void BtnSave_Click(object sender, EventArgs e)
{
int result = 0;
if (rowIndex < 0)//insert
{
try
{
conn.Open();
sql = @"Select * from insert(:_docente_id,:_nome_docente)";
cmd = new NpgsqlCommand(sql, conn);
cmd.Parameters.AddWithValue("_docente_id", textIdentificacao.Text);
cmd.Parameters.AddWithValue("_nome_docente", textNome.Text);
result = (int)cmd.ExecuteScalar();
conn.Close();
if (result == 1)
{
MessageBox.Show("Inserido com sucesso.");
Select();
}
else
{
MessageBox.Show("Inserção falhou.");
}
}
catch (Exception ex)
{
conn.Close();
MessageBox.Show("Inserção falhou. Error:" + ex.Message);
}
}
result = 0;
textIdentificacao.Text = textNome.Text = null;
textIdentificacao.Enabled = textNome.Enabled = false;
}
}
the function in posgresql is:
create or replace function insert(_docente_id int, _nome_docente varchar)
returns int as
$$
begin
insert into docente(docente_id, nome_docente)
values(_docente_id, _nome_docente);
if found then
return 1;
else return 0;
end if;
end
$$
language plpgsql
The error is: error 42883 function insert(text, text) does not exist
I think the problem is connected with the int and text. Because the function in postgresql has one int and one varchar and in c# I'm using cmd.Parameters.AddWithValue("_docente_id", textIdentificacao.Text); cmd.Parameters.AddWithValue("_nome_docente", textNome.Text);
Thanks