1

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

1 Answer 1

1

Try switching your line

cmd.Parameters.AddWithValue("_docente_id", textIdentificacao.Text)

to

cmd.Parameters.Add("_docente_id", SqlDbType.Int);
cmd.Parameters["_docente_id"].Value = int.Parse(textIdentificacao.Text);

or even just cmd.Parameters.AddWithValue("_docente_id", int.Parse(textIdentificacao.Text));

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Guru, I still don't have enough reputation but I will once I have it. I tried several times but it doesn't let me mark.
@RaquelWill0 interesting, NP =)
:) I was able to rate you. Thank you @gurustron
@RaquelWill0 NP. Was glad to help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.