Click here to Skip to main content
16,006,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have dataable and iam masking one column by adding new column name as MASKEDSSN.
It has been masked. But i need to Remove Old Column name SSN. While Removing it throws an error as
Quote:
Quote:
Cannot remove this column, because it is part of an expression: MaskedSSN = 'XXX-XX-'+SUBSTRING(CONVERT(SSN, System.String),6,4).


DataTable employeeTable = new DataTable();
employeeTable.Rows.Add("123455789");
employeeTable.Rows.Add("123447789");
employeeTable.Rows.Add("823456719");
employeeTable.Columns.Add("SSN");


Below is the code i have tried

int index = employeeTable.Columns["SSN"].Ordinal;
               employeeTable.Columns.Add("MaskedSSN", typeof(string));
               employeeTable.Columns["MaskedSSN"].Expression = "'XXX-XX-'+SUBSTRING(CONVERT(SSN, System.String),6,4)";
               DataTable newsss = new DataTable();

               newsss = employeeTable.Copy();
               newsss.AcceptChanges();
               newsss.Columns.RemoveAt(index);
               newsss.Columns.Remove("SSN");


What I have tried:

<pre> int index = employeeTable.Columns["SSN"].Ordinal;
                employeeTable.Columns.Add("MaskedSSN", typeof(string));
                employeeTable.Columns["MaskedSSN"].Expression = "'XXX-XX-'+SUBSTRING(CONVERT(SSN, System.String),6,4)";
                DataTable newsss = new DataTable();

                newsss = employeeTable.Copy();
                newsss.AcceptChanges();
                newsss.Columns.RemoveAt(index);
                newsss.Columns.Remove("SSN");
Posted
Updated 20-Dec-18 21:17pm

1 solution

Well yes. What did you expect?
You create MASKEDSSN and set it's value to be a computed column based on SSN. Computer columns aren't "evaluate once and forget", they are evaluated when the value is needed: when a row is added, when a row is displayed. If you could remove the SSN column, the MASKEDSSN column would be unable to compute the value of rows!

So very understandably, the system complains and tells you "You can't do that".
 
Share this answer
 
Comments
Vinodh Muthusamy 21-Dec-18 3:26am    
Is there any other way to mask, without adding new columns
Richard Deeming 21-Dec-18 10:57am    
Loop through the rows and update the column value to the masked value.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900