Update one DataTable from another

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Greg Peck

    Update one DataTable from another

    Hi I need to update a field (fldX) in one DataTable (dt1) where there is a
    match on fldA in another DataTable (dt2).

    Is the only way to do this by itterating through the DataRows of dt1,
    serching dt2 for a matching fldA and if found do the update and then move on
    to the next row?

    Thanks Greg


  • Cor Ligthert

    #2
    Re: Update one DataTable from another

    Greg,

    You can use a relation and than use the childcollection s and parent
    properties.

    I hope this helps,

    Cor


    Comment

    • Greg Peck

      #3
      Re: Update one DataTable from another

      Cor,
      A relation will not update dt1, will it?
      I need to update the field where there is a match and then later update the
      field on records where there were no matches with data from a different
      source.

      So I assume the only way is itterating through the DataTables?

      Greg

      "Cor Ligthert" <notmyfirstname @planet.nl> wrote in message
      news:ORYZX1yZFH A.3488@tk2msftn gp13.phx.gbl...[color=blue]
      > Greg,
      >
      > You can use a relation and than use the childcollection s and parent
      > properties.
      >
      > I hope this helps,
      >
      > Cor
      >
      >[/color]


      Comment

      • Cor Ligthert

        #4
        Re: Update one DataTable from another

        Greg,

        You can get a(the) child row(s) using a relation, the same is for the parent



        I hope this helps,

        Cor


        Comment

        • Greg Peck

          #5
          Re: Update one DataTable from another

          Cor,
          I must not be explaining this correctly.

          I can see that I could display a child field and even retrieve it but that
          is not adequate, I need to update the master table.

          I start with a DataTable, SOs with 2 fields that contain 'Nulls', UnitsTo
          and UnitsConv.

          I need to populate these two fields.

          Taking 1st Field as an example, UnitsTo, populating it involves:
          find matching records in a Table ItemMaster. ItemMaster has fields including
          ItemProd and ItemToUnits.
          Matching records are found using SOProd = ItemProd And SOWhs = ItemWhs

          First stage involves Updating [SOs].UnitsTo with value of
          [ItemMaster ].ItemToUnits
          Not all records in SOs has a matching record in ItemMaster so not all
          records will be updated

          Next stage involves Updating [SOs].UnitsTo with value from
          [SOs].UnitsStocking

          Updating [SO].UnitsConv is even more complicated.

          I suppose that I can create a relationship between SOs and ItemMaster
          Then itterate through SOs
          and use the code in the link you provided to return the value of ItemToUnits
          if there is a matching record
          and use it to update SOs.UnitsTo
          and if there is not a matching record then use SOs.UnitsStocki ng to update
          SOs.UnitsTo

          This is better than itterating both tables but still involves itterating
          SOs.

          Is that the way you would achieve the result?

          Greg

          "Cor Ligthert" <notmyfirstname @planet.nl> wrote in message
          news:O31BnezZFH A.3364@TK2MSFTN GP09.phx.gbl...[color=blue]
          > Greg,
          >
          > You can get a(the) child row(s) using a relation, the same is for the[/color]
          parent[color=blue]
          >
          >[/color]
          http://msdn.microsoft.com/library/de...drowstopic.asp[color=blue]
          >
          > I hope this helps,
          >
          > Cor
          >
          >[/color]


          Comment

          • Cor Ligthert

            #6
            Re: Update one DataTable from another

            Greg,

            In my opinion is one thing you would not be afraid of in a modern computer,
            that is (normally) itterating. It takes mostly *no* time. When you compare
            what a simple resizing of a window does, than you know why I write this.

            Therefore in my opinion you would take the approach which is the most
            comfortable for you. (Which you can easy maintain). I only gave you an
            alternative.

            Cor


            Comment

            • Greg Peck

              #7
              Re: Update one DataTable from another

              Sorry but I can not see how the link you provided provides me a different
              way to achieve the results that I am looking for.

              Thanks anyway.


              Comment

              • Cor Ligthert

                #8
                Re: Update one DataTable from another

                Greg,
                [color=blue]
                > Sorry but I can not see how the link you provided provides me a different
                > way to achieve the results that I am looking for.[/color]

                For me that is obvious, with setting the right relation I can in one time
                get the rows that are affected.

                However nobody says that you would use that. All your replys look for me if
                you don't want to have an answer, only somebody who is telling that your way
                is the best way. And than you probably will not get here.

                Cor


                Comment

                • Doug Bell

                  #9
                  Re: Update one DataTable from another

                  Greg,
                  I don't think that you can create complex relationships in a DataSet ie
                  where the relationship uses multiple fields.

                  If you just need to display the correct for the UnitsTo etc and that is not
                  in a DataGrid, you could do the logical selection when the record is
                  selected.

                  But if you need the data in a Grid or to update the Source DB I think you
                  will need to go about it the way you originally proposed, Record by record
                  doing the search, if found replace otherwise substitue.

                  Doug

                  "Greg Peck" <mail@vodaphone .com.au> wrote in message
                  news:eTFUh80ZFH A.2900@TK2MSFTN GP15.phx.gbl...[color=blue]
                  > Cor,
                  > I must not be explaining this correctly.
                  >
                  > I can see that I could display a child field and even retrieve it but that
                  > is not adequate, I need to update the master table.
                  >
                  > I start with a DataTable, SOs with 2 fields that contain 'Nulls', UnitsTo
                  > and UnitsConv.
                  >
                  > I need to populate these two fields.
                  >
                  > Taking 1st Field as an example, UnitsTo, populating it involves:
                  > find matching records in a Table ItemMaster. ItemMaster has fields[/color]
                  including[color=blue]
                  > ItemProd and ItemToUnits.
                  > Matching records are found using SOProd = ItemProd And SOWhs = ItemWhs
                  >
                  > First stage involves Updating [SOs].UnitsTo with value of
                  > [ItemMaster ].ItemToUnits
                  > Not all records in SOs has a matching record in ItemMaster so not all
                  > records will be updated
                  >
                  > Next stage involves Updating [SOs].UnitsTo with value from
                  > [SOs].UnitsStocking
                  >
                  > Updating [SO].UnitsConv is even more complicated.
                  >
                  > I suppose that I can create a relationship between SOs and ItemMaster
                  > Then itterate through SOs
                  > and use the code in the link you provided to return the value of[/color]
                  ItemToUnits[color=blue]
                  > if there is a matching record
                  > and use it to update SOs.UnitsTo
                  > and if there is not a matching record then use SOs.UnitsStocki ng to update
                  > SOs.UnitsTo
                  >
                  > This is better than itterating both tables but still involves itterating
                  > SOs.
                  >
                  > Is that the way you would achieve the result?
                  >
                  > Greg
                  >
                  > "Cor Ligthert" <notmyfirstname @planet.nl> wrote in message
                  > news:O31BnezZFH A.3364@TK2MSFTN GP09.phx.gbl...[color=green]
                  > > Greg,
                  > >
                  > > You can get a(the) child row(s) using a relation, the same is for the[/color]
                  > parent[color=green]
                  > >
                  > >[/color]
                  >[/color]
                  http://msdn.microsoft.com/library/de...drowstopic.asp[color=blue][color=green]
                  > >
                  > > I hope this helps,
                  > >
                  > > Cor
                  > >
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  Working...