Question

I have a dataset populated from a database:

dataset_original = new DataSet()
data_adapter.Fill(dataset_original)

and I cloned it:

dataset_cloned = dataset_original.Clone()

I cloned it because 1 of the columns in the original is of type int, and I want to change that to type string:

dataset_cloned.Tables(0).Columns("int_column_name_goes_here").DataType = GetType(String)

Now I need to populate the new dataset with the data from the old dataset. How do I do that?

I am using asp.net 1.1 coded with vb.net.

Was it helpful?

Solution

This simple loop should work (even with OPTION STRICT ON):

Dim dataset_cloned = dataset_original.Clone()
dataset_cloned.Tables(0).Columns("int_column_name_goes_here").DataType = GetType(String)
For i As Int32 = 0 To dataset_original.Tables.Count - 1
    Dim tbl_original As DataTable = dataset_original.Tables(i)
    Dim tbl_cloned As DataTable = dataset_cloned.Tables(i)
    For Each row As DataRow In tbl_original.Rows
        tbl_cloned.Rows.Add(row.ItemArray)
    Next
Next

OTHER TIPS

assuming you have only one table in the data set, you can do something like this.

            int ColumnIndex = 0; //Column index of your data you want to copy
            for (int i = 0; i < dataset_original.Tables[0].Rows.Count; i++)
            {
                dataset_cloned.Tables[0].Rows[i].SetField(ColumnIndex, dataset_original.Tables[0].Rows[ColumnIndex].ItemArray[0].ToString());
            }

in the same for loop you can copy remaining columns data

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top