Vineet More Vineet More - 1 year ago 335
C# Question

Character array insertion in postgresql using Npgsql

I would like to insert an array of strings into a table in PostgreSQL using Npgsql in C#. I have written the code below but am getting an InvalidCastexception.

eventcommand.Parameters.AddWithValue("@participants", NpgsqlDbType.Array).Value = participant.Text;

where participant is an textbox and eventcommand is an NpgsqlCommand.

Answer Source

You're calling AddWithValue, but not providing the value - you're providing the type. Additionally, you're not providing an array - you're just providing a single string. I suspect you just want:

command.Parameters.Add("@participants", NpgsqlDbType.Array | NpgsqlDbType.Text).Value
    = new[] { participant.Text };

Or you may want to split participant.Text into an array of strings first, or something similar.

(I've adjusted the type as per comments.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download