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. Visual Basic
  4. Use foreign key constraint with a loop ?

Use foreign key constraint with a loop ?

Scheduled Pinned Locked Moved Visual Basic
tutorialquestion
7 Posts 4 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.
  • J Offline
    J Offline
    Johan Hakkesteegt
    wrote on last edited by
    #1

    Hi, I have a DataSet with two tables that have a common field. Table 1 contains order header data and table 2 order row data. Both tables contain data of multiple orders. I want to handle each order separately, and my current method is a loop inside a loop and a "manual" check. For example something like this:

    For Each rwH as DataRow in ds.Tables("H").Rows
    'Get some header data
    '...
    For Each rwR as DataRow in ds.Tables("R").Rows
    If rwH.Item("KeyField").ToString = rwR.Item("KeyField").ToString Then
    'Get the relevant row data
    '...
    End If
    Next
    Next

    The question is, is there a way to avoid having to loop through the rows, and use this sort of crudish comparison ? I was thinking maybe creating a foreign key constraint. However, although I know how to create the constraint, I do not know how to use it to my advantage with any loop (assuming it is possible at all) ? Or can someone suggest an alternative (better) method ? All input is appreciated. Cheers, Johan

    My advice is free, and you may get what you paid for.

    P D W 3 Replies Last reply
    0
    • J Johan Hakkesteegt

      Hi, I have a DataSet with two tables that have a common field. Table 1 contains order header data and table 2 order row data. Both tables contain data of multiple orders. I want to handle each order separately, and my current method is a loop inside a loop and a "manual" check. For example something like this:

      For Each rwH as DataRow in ds.Tables("H").Rows
      'Get some header data
      '...
      For Each rwR as DataRow in ds.Tables("R").Rows
      If rwH.Item("KeyField").ToString = rwR.Item("KeyField").ToString Then
      'Get the relevant row data
      '...
      End If
      Next
      Next

      The question is, is there a way to avoid having to loop through the rows, and use this sort of crudish comparison ? I was thinking maybe creating a foreign key constraint. However, although I know how to create the constraint, I do not know how to use it to my advantage with any loop (assuming it is possible at all) ? Or can someone suggest an alternative (better) method ? All input is appreciated. Cheers, Johan

      My advice is free, and you may get what you paid for.

      P Offline
      P Offline
      Paul Conrad
      wrote on last edited by
      #2

      Why not have a query that joins the two tables and groups by KeyField or something of that nature?

      ""Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

      J 1 Reply Last reply
      0
      • J Johan Hakkesteegt

        Hi, I have a DataSet with two tables that have a common field. Table 1 contains order header data and table 2 order row data. Both tables contain data of multiple orders. I want to handle each order separately, and my current method is a loop inside a loop and a "manual" check. For example something like this:

        For Each rwH as DataRow in ds.Tables("H").Rows
        'Get some header data
        '...
        For Each rwR as DataRow in ds.Tables("R").Rows
        If rwH.Item("KeyField").ToString = rwR.Item("KeyField").ToString Then
        'Get the relevant row data
        '...
        End If
        Next
        Next

        The question is, is there a way to avoid having to loop through the rows, and use this sort of crudish comparison ? I was thinking maybe creating a foreign key constraint. However, although I know how to create the constraint, I do not know how to use it to my advantage with any loop (assuming it is possible at all) ? Or can someone suggest an alternative (better) method ? All input is appreciated. Cheers, Johan

        My advice is free, and you may get what you paid for.

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        Soooo, you're going to look at the same set of records (inner loop) over and over again, until you match them up with the records in the outer loop?? How many records in the outer loop again?? That seems AWFULLY inefficient. Why not just create a SQL query that matches everything up for you?

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak

        J 1 Reply Last reply
        0
        • J Johan Hakkesteegt

          Hi, I have a DataSet with two tables that have a common field. Table 1 contains order header data and table 2 order row data. Both tables contain data of multiple orders. I want to handle each order separately, and my current method is a loop inside a loop and a "manual" check. For example something like this:

          For Each rwH as DataRow in ds.Tables("H").Rows
          'Get some header data
          '...
          For Each rwR as DataRow in ds.Tables("R").Rows
          If rwH.Item("KeyField").ToString = rwR.Item("KeyField").ToString Then
          'Get the relevant row data
          '...
          End If
          Next
          Next

          The question is, is there a way to avoid having to loop through the rows, and use this sort of crudish comparison ? I was thinking maybe creating a foreign key constraint. However, although I know how to create the constraint, I do not know how to use it to my advantage with any loop (assuming it is possible at all) ? Or can someone suggest an alternative (better) method ? All input is appreciated. Cheers, Johan

          My advice is free, and you may get what you paid for.

          W Offline
          W Offline
          woopsydoozy
          wrote on last edited by
          #4

          You can at least eliminate the nested loop with a SELECT:

          dim drRows() as DataRow = ds.Tables("R").Select("KeyField = '" & rwH.Item("KeyField").ToString & "'")
          If drRows.Length > 0 Then
          'do your thing
          End If

          J 1 Reply Last reply
          0
          • D Dave Kreskowiak

            Soooo, you're going to look at the same set of records (inner loop) over and over again, until you match them up with the records in the outer loop?? How many records in the outer loop again?? That seems AWFULLY inefficient. Why not just create a SQL query that matches everything up for you?

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak

            J Offline
            J Offline
            Johan Hakkesteegt
            wrote on last edited by
            #5

            It is awfully inefficient, hence the question. :) In this scenario I need to create orders into an ERP system via a proprietary driver. That means that I need to declare a new "order" object, and fill in all its properties. Some header data (customer name, and address, and such), and an unknown number of rows (product number, quantity, price, etc.). In addition I need to translate that same information into an email message. So if I combine both header and row data into a single query, I would have to populate the header data of the order object over and over, for each row, or build in some kind of is-it-already-populated method. Somehow that does not seem very efficient (or elegant) either (I may very well be wrong of course). For clarity, the header table will generally only contain between 1 and 10 records, the rows table however can contain anywhere between 1 and 150 records per header record. Eliminating the nested loop is therefore my main concern here.

            My advice is free, and you may get what you paid for.

            1 Reply Last reply
            0
            • W woopsydoozy

              You can at least eliminate the nested loop with a SELECT:

              dim drRows() as DataRow = ds.Tables("R").Select("KeyField = '" & rwH.Item("KeyField").ToString & "'")
              If drRows.Length > 0 Then
              'do your thing
              End If

              J Offline
              J Offline
              Johan Hakkesteegt
              wrote on last edited by
              #6

              Thanks, I didn't know about this one. It seems to be exactly what I need.

              My advice is free, and you may get what you paid for.

              1 Reply Last reply
              0
              • P Paul Conrad

                Why not have a query that joins the two tables and groups by KeyField or something of that nature?

                ""Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                J Offline
                J Offline
                Johan Hakkesteegt
                wrote on last edited by
                #7

                Please see my answer to Dave's post.

                My advice is free, and you may get what you paid for.

                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