Search code examples
c#wpfdatagridupdatecommand

How to change values of column of DataTable and show only in DataGrid not updating actual table of database


I have a column with encrypted name(all other columns are not encrypted) in SQL Database table. And I have to decrypt the column with encrypted name to show in DataGrid to users of my application but the actual table of SQL database should not be changed.(has to remain as encrypted name).

I think UpdateCommand works to update the actual table and I have to find an alternative than below UpdateCommand.

Or is there alternative way to decrypt only 1 column on DataTable which is not influencing the actual table of database?

My simple code is,

SqlCommand gridcomm = new SqlCommand();
gridcomm.Connection = Conn;

gridcomm.CommandText = "SELECT Id, customername, phonenumber FROM customers";

SqlDataAdapter gridda = new SqlDataAdapter(gridcomm);

SqlDataReader gridreader = gridcomm.ExecuteReader();
while (gridreader.Read())
{
}
gridreader.Close();

DataTable griddt = new DataTable("customers");
gridda.Fill(griddt);

foreach (DataRow row in griddt.Rows)
{
    string strcustomername = (string) row["customername"].ToString();
    bytecustomername = Convert.FromBase64String(strcustomername);
    string decryptedcustomername = DecryptStringFromBytes_Aes(bytecustomername, byteAESKey, byteAESIV);

    row["customername"] = decryptedcustomername;
}

gridda.UpdateCommand = new SqlCommandBuilder(gridda).GetUpdateCommand();

dataGrid_Totalcustomerlist.ItemsSource = griddt.DefaultView;
gridda.Update(griddt);

Solution

  • I've read many posts but there were no solution for me as this case is unusual. However, I just thought logically and finally found solution by myself.

    We just need to delete 2 line of Update related code because we don't need to update.

    gridda.UpdateCommand = new SqlCommandBuilder(gridda).GetUpdateCommand();
    
    gridda.Update(griddt);
    

    Hope this helps someone..