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.
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.)
See more on this question at Stackoverflow