Sitelet https://github.com/npgsql/npgsql/issues/6067
Skip to content

PostgresException: 42804 when executing several queries with parameters with one open connection #6067

Description

@HoMS1987

When executing Queries with parameters on the same table, I get the error:
Npgsql.PostgresException: 42804: Column »bool1« has Type boolean but expression has type integer

But when debugging the datatype of the NpgsqlParameter is correct, directly before I call ExecuteNonQuery()

I attached a little test project, that reproduces this error.
Only the connection string inside has to be adjustet.
It creates a new database, a new table and first tries to update a row (which is not still there) and as it couldn't update the row it creates it.
During the creation of this row the Exception is thrown.

NpgsqlTest.zip

Activity

  1. vonzshik commented on Mar 21, 2025

    @vonzshik
    Contributor

    As a workaround, you can specify the name of the parameter before adding it to the collection.

    private static IDbDataParameter AddParameter(this IDbCommand command)
    {
    	IDbDataParameter parameter = command.CreateParameter();
    	var index = command.Parameters.Count;
    	parameter.ParameterName = index.ToString(CultureInfo.InvariantCulture);
    	command.Parameters.Add(parameter);
    	
    	return parameter;
    }

    The problem is that because when the parameter is added for the first time it doesn't have a name, it's considered as positional (unnamed), and it seems like we do not react that well whenever their name is changed.

    I think removing _caseInsensitiveLookup.Count == 0 check from here should do the trick?

    if (_caseInsensitiveLookup is null || _caseInsensitiveLookup.Count == 0)
    return;

    cc @NinoFloris

  2. HoMS1987 commented on Mar 24, 2025

    @HoMS1987
    Author

    Thx, this workaround really helped :)

  3. added this to the 9.0.4 milestone on Mar 24, 2025
  4. added theissue type on Mar 24, 2025
  5. self-assigned this
    on Mar 24, 2025
  6. added 2 commits that reference this issue on Mar 26, 2025
    5cbd025
    d119175
  7. added a commit that references this issue on Mar 26, 2025
    ef219b7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions