Click here to Skip to main content
14,730,348 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have some tables in dataset that i want encode all values in datrows

id name 
11   a
22   b
33   c

id name 
1   a
2   b
3   c

What I have tried:

I have tried

DataTable updateddataTable=new DataTable();
foreach( DataTable dataTable in dataSet)
foreach(DataRow dr in dataTable.Rows)
   for(int i=0;dataTable.Columns.Count;i++)

The updated datatble getting null here could you please suggest better approach for this.
That should be work without loss any data from dataset
Updated 6-Jan-21 0:18am
PIEBALDconsult 6-Jan-21 10:45am
Can't you do that upstream? If you're filling the datasets by querying a database, you may be able to add the encode to the query.

1 solution

Take a look at below code:

//create another dataset object
DataSet ds = new DataSet();
//loop through the tables in original dataset
foreach(DataTable origDt in origDataSet)
    //create new datatable and clone structure
    DataTable encodedDt = origDt.Clone();
    //loop through the rows in original datatable and import encoded row into "encoded" table
    for(int i=0; i<origDt.Rows.Count; i++)
        DataRow dr = origDt.Rows[i];
    //add datatable to "another" dataset
//you can use [ds] now
DGKumar 6-Jan-21 5:32am
tried but the below htmlencode method accept only string
but dr is object
my requirement is same dataset loop and assing other data set without loss data
Maciej Los 6-Jan-21 5:45am
How should i know that HttpEncode method accepts only string? I'm not familiar with that function.
Change above code to your needs.

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