Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Something wrong with "ItemArray.CopyTo()" [modified]

Something wrong with "ItemArray.CopyTo()" [modified]

Scheduled Pinned Locked Moved C#
helpquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dealon
    wrote on last edited by
    #1

    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

    C G 2 Replies Last reply
    0
    • D dealon

      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

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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 )

      D 1 Reply Last reply
      0
      • D dealon

        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

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        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.

        D 1 Reply Last reply
        0
        • C Christian Graus

          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 )

          D Offline
          D Offline
          dealon
          wrote on last edited by
          #4

          Thanks Christian Graus for your reply. The issue is : After "CopyTo" operation, the destination array(here is drNew.ItemArray) is still empty.

          If we dream, every thing is possible!

          1 Reply Last reply
          0
          • G Guffa

            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.

            D Offline
            D Offline
            dealon
            wrote on last edited by
            #5

            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!

            G 1 Reply Last reply
            0
            • D dealon

              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!

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              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.

              D 1 Reply Last reply
              0
              • G Guffa

                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.

                D Offline
                D Offline
                dealon
                wrote on last edited by
                #7

                I see.Thank you very much. :)

                If we dream, every thing is possible!

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups