question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pass array to postgresql problem after package update

See original GitHub issue

used the following code

C#

List<List<string>> items = new List<List<string>>();

foreach (OrderItemsDTO p in products)
{
    List<string> list = new List<string>();
    list.Add(p.product_id.ToString());
    list.Add(Convert.ToString(p.quantity, culture));
    list.Add(Convert.ToString(p.price, culture));
    list.Add(Convert.ToString(p.discount, culture));
    list.Add(Convert.ToString(p.total, culture));
    list.Add(Convert.ToString(p.cost, culture));
    list.Add(p.User.id.ToString());
    list.Add(p.order.ToString());
    items.Add(list);
}

command.Parameters.Add(null, NpgsqlTypes.NpgsqlDbType.Array | NpgsqlTypes.NpgsqlDbType.Varchar).Value = items.ToArray();

POSTGRESQL

CREATE OR REPLACE FUNCTION public.insert_request(
_character varying products[])
RETURNS integer
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
THE $BODY$
declare

it worked perfectly, but after I updated the Npgsql.dll package from version 2.0.12.0 to version 5.07.0, it crashed and returned the following error:

While trying to write an array, one of its elements failed validation. You may be trying to mix types in a non-generic IList, or to write a jagged array.

I can’t solve it at all.

The function in postgresql is working normally, the problem is in passing the parameters.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
DiegoSantanadsscommented, Oct 20, 2021

@roji muito obrigado, graças a você conseguir resolver o problema, acredite, parece besteira mas já tinha perdido um bom tempo tentando resolver.

Segue resolução.

      string[,] teste = new string[produtos.Count, 8];

        int y = 0;

        foreach (PedidoItensDTO p in produtos)
        {
            teste[y, 0] = p.id_produto.ToString();
            teste[y, 1] = Convert.ToString(p.quantidade, culture);
            teste[y, 2] = Convert.ToString(p.preco, culture);
            teste[y, 3] = Convert.ToString(p.desconto, culture);
            teste[y, 4] = Convert.ToString(p.total, culture);
            teste[y, 5] = Convert.ToString(p.custo, culture);
            teste[y, 6] = p.Usuario.id.ToString();
            teste[y, 7] = p.ordem.ToString();

            y++;
        }


   comando.Parameters.Add(null, NpgsqlTypes.NpgsqlDbType.Array | NpgsqlTypes.NpgsqlDbType.Varchar).Value = teste;
0reactions
DiegoSantanadsscommented, Oct 20, 2021

In this case, I have to pass an array of fixed dimensions. Right? but the problem is there, the matrix has to be dynamic, because the amount of products can vary, the fields will always be the same but the amount of lines is variable. So the List<List<string>> was serving me well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Passing json containing array of objects to PostgreSQL ...
The problem was with the syntax inside the function. The correct syntax for parsing array of objects and assigning it to json ARRAY:...
Read more >
How to pass a table type with an array field to a function in ...
Your function looks a bit overcomplicated for me, but anways. An array literal must be enclosed by single quotes, so try the following:...
Read more >
Documentation: 15: 43.6. Control Structures
If WHEN is specified, the next iteration of the loop is begun only if boolean-expression is true. Otherwise, control passes to the statement...
Read more >
Using Arrays in PostgreSQL: A Guide
With the array, PostgreSQL allows columns of a table to be defined as variable-length multidimensional arrays.
Read more >
PostgreSQL — SQLAlchemy 2.0 Documentation
When SQLAlchemy issues a single INSERT statement, to fulfill the contract of having the “last insert identifier” available, a RETURNING clause is added...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found