byte[] as PK

  • Thread starter Thread starter Mircea Pleteriu
  • Start date 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
 
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
 
Back
Top