Something wrong with "ItemArray.CopyTo()" [modified]
-
hi all. I was wondering if anyone can assist me.. I have this code below: DataTable dtSource = m_dtUsed; DataTable dtDest = dtSource.Clone(); foreach (DataRow dr in dtSource.Rows) { DataRow drNew = dtDest.NewRow(); // this code work perfectly object[] obj = new object[dr.ItemArray.Length]; dr.ItemArray.CopyTo(obj, 0); drNew.ItemArray = obj; dtDest.Rows.Add(drNew); // why this code can't work? // after dr.ItemArray.CopyTo(drNew.ItemArray, 0), i find drNew.ItemArray is still empty,i don't know why? //drNew.ItemArray = new object[dr.ItemArray.Length]; //dr.ItemArray.CopyTo(drNew.ItemArray, 0); //dtDest.Rows.Add(drNew); } Any help would be appreciated.
If we dream, every thing is possible!
modified on Saturday, May 31, 2008 10:13 AM
-
hi all. I was wondering if anyone can assist me.. I have this code below: DataTable dtSource = m_dtUsed; DataTable dtDest = dtSource.Clone(); foreach (DataRow dr in dtSource.Rows) { DataRow drNew = dtDest.NewRow(); // this code work perfectly object[] obj = new object[dr.ItemArray.Length]; dr.ItemArray.CopyTo(obj, 0); drNew.ItemArray = obj; dtDest.Rows.Add(drNew); // why this code can't work? // after dr.ItemArray.CopyTo(drNew.ItemArray, 0), i find drNew.ItemArray is still empty,i don't know why? //drNew.ItemArray = new object[dr.ItemArray.Length]; //dr.ItemArray.CopyTo(drNew.ItemArray, 0); //dtDest.Rows.Add(drNew); } Any help would be appreciated.
If we dream, every thing is possible!
modified on Saturday, May 31, 2008 10:13 AM
What does 'not working' mean ? What does it do instead ? What does MSDN say ?
Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
hi all. I was wondering if anyone can assist me.. I have this code below: DataTable dtSource = m_dtUsed; DataTable dtDest = dtSource.Clone(); foreach (DataRow dr in dtSource.Rows) { DataRow drNew = dtDest.NewRow(); // this code work perfectly object[] obj = new object[dr.ItemArray.Length]; dr.ItemArray.CopyTo(obj, 0); drNew.ItemArray = obj; dtDest.Rows.Add(drNew); // why this code can't work? // after dr.ItemArray.CopyTo(drNew.ItemArray, 0), i find drNew.ItemArray is still empty,i don't know why? //drNew.ItemArray = new object[dr.ItemArray.Length]; //dr.ItemArray.CopyTo(drNew.ItemArray, 0); //dtDest.Rows.Add(drNew); } Any help would be appreciated.
If we dream, every thing is possible!
modified on Saturday, May 31, 2008 10:13 AM
dealon wrote:
// this code work perfectly object[] obj = new object[dr.ItemArray.Length]; dr.(obj, 0);
That won't even compile.
dealon wrote:
// why this code can't work? //drNew.ItemArray = new object[dr.ItemArray.Length]; //dr.ItemArray.CopyTo(drNew.ItemArray, 0); //dtDest.Rows.Add(drNew);
The ItemArray property returns a new object array that contains references to the data in the data row. You are copying the data to this array, and then you are throwing it away. Replacing the references in the object array doesn't change the data in the data row.
Despite everything, the person most likely to be fooling you next is yourself.
-
What does 'not working' mean ? What does it do instead ? What does MSDN say ?
Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
dealon wrote:
// this code work perfectly object[] obj = new object[dr.ItemArray.Length]; dr.(obj, 0);
That won't even compile.
dealon wrote:
// why this code can't work? //drNew.ItemArray = new object[dr.ItemArray.Length]; //dr.ItemArray.CopyTo(drNew.ItemArray, 0); //dtDest.Rows.Add(drNew);
The ItemArray property returns a new object array that contains references to the data in the data row. You are copying the data to this array, and then you are throwing it away. Replacing the references in the object array doesn't change the data in the data row.
Despite everything, the person most likely to be fooling you next is yourself.
Thanks Guffa for your help. I think my incorrect code copied confused you. The fact is like this: 1)This code can work and the drNew.ItemArray have my wanted data. ------------ object[] obj = new object[dr.ItemArray.Length]; dr.ItemArray.CopyTo(obj, 0); drNew.ItemArray = obj; 2)But the method below seems bad. ------------ drNew.ItemArray = new object[dr.ItemArray.Length]; dr.ItemArray.CopyTo(drNew.ItemArray, 0); dtDest.Rows.Add(drNew); i found drNew.ItemArray is still empty.I don't known why?
If we dream, every thing is possible!
-
Thanks Guffa for your help. I think my incorrect code copied confused you. The fact is like this: 1)This code can work and the drNew.ItemArray have my wanted data. ------------ object[] obj = new object[dr.ItemArray.Length]; dr.ItemArray.CopyTo(obj, 0); drNew.ItemArray = obj; 2)But the method below seems bad. ------------ drNew.ItemArray = new object[dr.ItemArray.Length]; dr.ItemArray.CopyTo(drNew.ItemArray, 0); dtDest.Rows.Add(drNew); i found drNew.ItemArray is still empty.I don't known why?
If we dream, every thing is possible!
dealon wrote:
i found drNew.ItemArray is still empty.I don't known why?
I already explained why: The ItemArray property creates a new array. Changing anything in that array doesn't change anything in the DataRow. So, you create a new array, fill it with data, and throw it away.
Despite everything, the person most likely to be fooling you next is yourself.
-
dealon wrote:
i found drNew.ItemArray is still empty.I don't known why?
I already explained why: The ItemArray property creates a new array. Changing anything in that array doesn't change anything in the DataRow. So, you create a new array, fill it with data, and throw it away.
Despite everything, the person most likely to be fooling you next is yourself.