byte[] as PK

  • Thread starter Mircea Pleteriu
  • Start date
M

Mircea Pleteriu

Hi All,

byte[] b1 = Guid.NewGuid().ToByteArray();
byte[] b2 = Guid.NewGuid().ToByteArray();

DataTable dt = new DataTable();
dt.Columns.Add("C1", typeof(byte[]));
dt.PrimaryKey = new DataColumn[] {dt.Columns["C1"]};

dt.Rows.Add(new object[] {b1});
dt.Rows.Add(new object[] {b2});

This code throws the exception shown below. Does anyone know why?

Unhandled Exception: System.Data.ConstraintException: Column 'C1' is
constrained
to be unique. Value 'System.Byte[]' is already present.
at System.Data.DataTable.InsertRow(DataRow row, Int32 proposedID, Int32
pos)
at System.Data.DataRowCollection.Add(Object[] values)
at ConsoleApplication1.Class1.Main(String[] args) in d:\projects
development\
c#\test\consoleapplication1\consoleapplication1\class1.cs:line 34
 
M

Mark Ashton

In V2.0, DataSet can support byte[] as a primary key but it has issues with
byte[] are mutable given that you can change the value without the dataset
being aware of it - causes indexing issues. The DataSet could make a copy
of the byte[], but that isn't happening. You would need to replace the
byte[] object rather than just edit the value.

--
This posting is provided "AS IS", with no warranties, and confers no rights.
Please do not send email directly to this alias. This alias is for newsgroup
purposes only.

Mircea Pleteriu said:
I've found the reason at

http://www.dotnet247.com/247reference/msgs/47/238334.aspx

Mircea Pleteriu said:
Hi All,

byte[] b1 = Guid.NewGuid().ToByteArray();
byte[] b2 = Guid.NewGuid().ToByteArray();

DataTable dt = new DataTable();
dt.Columns.Add("C1", typeof(byte[]));
dt.PrimaryKey = new DataColumn[] {dt.Columns["C1"]};

dt.Rows.Add(new object[] {b1});
dt.Rows.Add(new object[] {b2});

This code throws the exception shown below. Does anyone know why?

Unhandled Exception: System.Data.ConstraintException: Column 'C1' is
constrained
to be unique. Value 'System.Byte[]' is already present.
at System.Data.DataTable.InsertRow(DataRow row, Int32 proposedID,
Int32
pos)
at System.Data.DataRowCollection.Add(Object[] values)
at ConsoleApplication1.Class1.Main(String[] args) in d:\projects
development\
c#\test\consoleapplication1\consoleapplication1\class1.cs:line 34
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top