2

I am in the following situation :

I have to patch a problem in a very poorly designed application. In the code, the previous developer get informations from DB and put it in a DataTable. Then, he sets Primary Key on a field of this datatable.

However, the PK column is varchar type, and I have the following error :

These columns don't currently have unique values.

This is because in the data, I have :

123b
123B

It seems these values are identical for the datatable, whereas they are not exactly the same. Is there any quick and dirty workaround to avoid this problem, like CaseSensitive = False

PS : I already see this question, and this one, but I'd like to avoid to recreate the entire application...
I know I should create an identity column and set it as PK, but there are a lot of collateral issues and I'll do it only if I can't find a fast solution.

FYI, the datatable is constructed like that :

Dim dataColumn As New DataColumn(ucStatus.CheckColumnKey, GetType(Boolean))
dataColumn.DefaultValue = False
dataColumn.AllowDBNull = False
sourceInt.Columns.Add(dataColumn)
sourceInt.Columns(dataColumn.ColumnName).SetOrdinal(0)

sourceInt.PrimaryKey = New DataColumn() {sourceInt.Columns(MyColumn)}

The last line is a problem a MyColumn column contains the problematic datas.
The code is in VB.NET, but C# answer is accepted !

Thanks for your help

Community
  • 1
  • 1
user2687153
  • 427
  • 5
  • 24

1 Answers1

3

Setting the CaseSensitive property on the DataTable object to true before assigning the PrimaryKey property will allow you to use a PrimaryKey that considers the two values, 123b and 123B, to be unique. I had the same problem and answered this here

Community
  • 1
  • 1
Anilof
  • 326
  • 2
  • 7