Vel_More
Vel_More

Reputation: 363

PostgreSQL Function doesn't Exist in C# (Error: 42883)

I am creating a winforms interface in which I am connecting my PostgreSQL database, in order to select, update and delete my data. So far, I've created the "Insert" & "Select" functions and I am proceeding with the "Update" one. I've created the following function on PostgreSQL,

PostgreSQL Update Function

create function ka_update
(
    _cus_name varchar,
    _cus_phone varchar,
    _cus_vat varchar,
    _cus_street varchar,
    _cus_street_num varchar,
    _cus_city varchar,
    _cus_zip varchar,
    _cus_email varchar,
    _cus_id integer
)returns int as
$$
begin
    update customer
    set
        cus_name = _cus_name,
        cus_phone = _cus_phone,
        cus_vat = _cus_vat,
        cus_street = _cus_street,
        cus_street_num = _cus_street_num,
        cus_city = _cus_city,
        cus_zip = _cus_zip,
        cus_email = _cus_email
    where cus_id=_cus_id;
    if found then
        return 1;
    else
        return 0;
    end if;
end
$$
language plpgsql

and then I am proceeding with my C# code,

C# Script to Use the Update Function from PostgreSQL

private NpgsqlConnection conn;
private DataTable dt;
private NpgsqlCommand cmd;
private string sql = null;
private DataGridViewRow r;


private void button2_Click(object sender, EventArgs e)
        {
            if (r == null)
            {
                MessageBox.Show("Please choose a customer to update information", "Oops", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            try
            {
                conn.Open();
                sql = @"select * from ka_update(:_cus_name,:_cus_phone,:_cus_vat,:_cus_street,:_cus_street_num,:_cus_city,:_cus_zip,:_cus_email,:_cus_id)";
                cmd = new NpgsqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("_cus_name", textBox1.Text);
                cmd.Parameters.AddWithValue("_cus_phone", textBox2.Text);
                cmd.Parameters.AddWithValue("_cus_vat", textBox3.Text);
                cmd.Parameters.AddWithValue("_cus_street", textBox4.Text);
                cmd.Parameters.AddWithValue("_cus_street_num", textBox5.Text);
                cmd.Parameters.AddWithValue("_cus_city", textBox6.Text);
                cmd.Parameters.AddWithValue("_cus_zip", textBox7.Text);
                cmd.Parameters.AddWithValue("_cus_email", textBox8.Text);
                cmd.Parameters.AddWithValue("_cus_id", r.Cells["_cus_id"].Value.ToString());
                
                if ((int)cmd.ExecuteScalar() == 1)
                {
                    conn.Close();
                    MessageBox.Show("Updated a new customer successfully", "Well Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    button4.PerformClick(); //If updated successfully then load all customers again
                    //reset textbox components
                    textBox1.Text = textBox2.Text = textBox3.Text = textBox4.Text = textBox5.Text = textBox6.Text = textBox7.Text = textBox8.Text = null;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message, "UPDATE FAIL!!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                conn.Close();
            }



private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                r = dataGridView1.Rows[e.RowIndex];
                textBox1.Text = r.Cells["_cus_name"].Value.ToString();
                textBox2.Text = r.Cells["_cus_phone"].Value.ToString();
                textBox3.Text = r.Cells["_cus_vat"].Value.ToString();
                textBox4.Text = r.Cells["_cus_street"].Value.ToString();
                textBox5.Text = r.Cells["_cus_street_num"].Value.ToString();
                textBox6.Text = r.Cells["_cus_city"].Value.ToString();
                textBox7.Text = r.Cells["_cus_zip"].Value.ToString();
                textBox8.Text = r.Cells["_cus_email"].Value.ToString();

            }
        }


I am receiving the following error

enter image description here

I am trying to figure out, what I am doing wrong with the writing of the function but I've zero clue so far. Any ideas???

Upvotes: 1

Views: 1686

Answers (1)

DanielZhang-MSFT
DanielZhang-MSFT

Reputation: 174

The error indicates that the parameter types do not match. jjanes has also pointed out that the last parameter is int

If your _cus_id column is of type int, you can use the Convert.ToInt32 method to directly convert it to type int.

cmd.Parameters.AddWithValue("_cus_id", Convert.ToInt32(r.Cells["_cus_id"].Value));

Upvotes: 1

Related Questions