I am trying to mark a column in my DataTable _Table
as the primary key but it don't always works.
DataTable _Table
will be populated with one row, that is copied from another DataTable (Table
)
Therefor, DataTable _Table
will either hold a row fetched from a table from SQL Server,
or it will hold a new row that does not yet exists in SQL Server.
After copying the row from Table
into _Table
I call this code
_Table.PrimaryKey = new DataColumn[] { Table.Columns[PrimaryKeyName] };
Now, when the row came from an existing row in the database, there is no exception when I run this line of code. That is because there is an unique value in the primary key column.
But when it holds a new row, the value in the primary key column is still empty (identity column) and that will probably be the reason I now get this error.
These columns don't currently have unique values.'
Currently I am catching the exception, and then I just eat the exception, and all is working well.
But, it feels like some dirty hack, I want to do this properly.
So, is there a way I can mark a column as primary key
in a datatable, that only has one row in it, and where this column currently holds a null value ?