Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BitArray: bit string length 1 does not match type bit(256) #2269

Closed
zezba9000 opened this issue Dec 20, 2018 · 5 comments
Closed

BitArray: bit string length 1 does not match type bit(256) #2269

zezba9000 opened this issue Dec 20, 2018 · 5 comments

Comments

@zezba9000
Copy link

zezba9000 commented Dec 20, 2018

Created a postgresql tabel on my local computer with 2 columns (SystemID [as uuid], TrackingIDs [as Bit[] with size 256]).

In C# ASP.NET I'm getting the error:

22026: bit string length 1 does not match type bit(256)

"new BitArray(256)" is 256 in length not '1' as the error suggests.

Here is an example of my C# code:

using (var connection = new NpgsqlConnection(DBUtils.connectionString))
{
    try
    {
        connection.Open();
        using (var cmd = connection.CreateCommand())
        {
            cmd.CommandText = "INSERT INTO hosts VALUES(@SystemID, @TrackingIDs)";
            cmd.Parameters.AddWithValue("@SystemID", NpgsqlDbType.Uuid, systemID);
            cmd.Parameters.AddWithValue("@TrackingIDs", NpgsqlDbType.Bit | NpgsqlDbType.Array, new BitArray(256));
            return cmd.ExecuteNonQuery() != 0 ? "Success" : "Failed";
        }
    }
    catch (Exception ex)
    {
        return ex.Message;
    }
    finally
    {
        connection.Close();
    }
}

Further technical details

pgAdmin4: 3.6
Npgsql version: 4.0.4
PostgreSQL version: 11.1
Operating system: Win10 x64
ASP.NET: .NET Core 2.2

@austindrenski
Copy link
Contributor

Per the previous thread:

Manually setting the size to 256 and its thinking its 1 in length?

What exactly are you trying to do by setting the size explicitly?

Consider the following sample which completes successfully:

static void Main(string[] args)
{
    var connection = $"Host=localhost;Port=5432;Username={args[0]};Password={args[1]};";

    using (var conn = new NpgsqlConnection(connection))
    {
        conn.Open();

        using (var cmd = conn.CreateCommand())
        {
            cmd.CommandText = "CREATE TEMP TABLE some_table( some_column bit[] );";
            cmd.ExecuteNonQuery();
        }

        using (var cmd = conn.CreateCommand())
        {
            cmd.CommandText = "INSERT INTO some_table VALUES( @p )";
            cmd.Parameters.AddWithValue("@p", NpgsqlDbType.Bit | NpgsqlDbType.Array, new BitArray(256));
            cmd.ExecuteNonQuery();
        }
    }

    Console.WriteLine("Completed successfully");
}
some_database=# \d some_table;
              Table "pg_temp_4.some_table"
   Column    |   Type   | Collation | Nullable | Default
-------------+----------+-----------+----------+---------
 some_column | bit(1)[] |           |          |

@zezba9000
Copy link
Author

O ic. "bit(8)[2]" would equal two bytes. I was thinking bit(8) was the same thing as bit[8] as in bit always meant a single bit as in a bit from a byte for example. Bit in this case can be any dimension for postgresql.

So guess I'm looking to create a fixed size of bit(1)[256] in length. Which means I create a single bit and set its length to 256 in postgresql terms?

@zezba9000
Copy link
Author

Yep that was it. Ok sorry for the confusion. Was just doing stuff in MySQL which is a lot more limited. PostgreSQL is awesome it divides stuff up like this.

@austindrenski
Copy link
Contributor

Yep that was it. Ok sorry for the confusion. Was just doing stuff in MySQL which is a lot more limited. PostgreSQL is awesome it divides stuff up like this.

No worries! I actually misread your error message to be bit[256] instead of bit(256), so apologies if that sample was a bit off base.

Glad that we were able to get to the bottom of it. Feel free to post back here if you encounter any other issues with this topic.

@austindrenski
Copy link
Contributor

P.S. I've opened #2270 to track updating the docs with a sample or two based on this thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants