Click here to Skip to main content
15,897,519 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

C#
DataTable updateddataTable=new DataTable();
foreach( DataTable dataTable in dataSet)
{
foreach(DataRow dr in dataTable.Rows)
{
   for(int i=0;dataTable.Columns.Count;i++)
{
row[i]=HttpEncode(row[i]);
}
updateddataTable.ImportRow(row);
}


The updated datatble getting null here could you please suggest better approach for this.
That should be work without loss any data from dataset
Posted
Updated 5-Jan-21 23:18pm
v2
Comments
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:

C#
//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];
        encodedDt.ImportRow(HttpEncode(dr));
    }
    //add datatable to "another" dataset
    ds.Tables.Add(encodedDt);
}
//done!
//you can use [ds] now
 
Share this answer
 
v2
Comments
DGKumar 6-Jan-21 5:32am    
tried but the below htmlencode method accept only string
encodedDt.ImportRow(HttpEncode(dr));
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