How to use cross join to show data in datagridview.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SEhtesham
    New Member
    • Aug 2011
    • 45

    How to use cross join to show data in datagridview.

    Hi,
    I have a datagridview.An d as per my requirement the data is supposed to be filled from three different tables in database.I have created the query and is succesfully able to populated the data in it with the cross join.
    Have a look at my structure.
    Table1,Table2,T able3.
    table1 has 2 records.
    Table2 has 7 records.
    and Table 3 has 15 records.
    Below is my code that reads data from Cross Join Query
    Code:
    If dr.HasRows Then
                While dr.Read
                    combo1.Items.Add(dr(0))
                    combo2.Items.Add(dr(1))
                    combo3.Items.Add(dr(2))
                End While
            End If
    
    my query
    strquery="Select Table1.Column1,Table2.Column1,
    Table3.Column1 from Table1 cross 
    join Table2 cross join table3"
    It is giving me duplicate records.Please give me logic to do it correctly.
    Last edited by SEhtesham; Sep 13 '11, 02:20 PM. Reason: Alignment
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    A cross join will join every record to every other record. That's what it's supposed to do. Without knowing how you define a duplicate and a sampling of the data, you've given us no information that we can use to help you resolve your problem.

    Comment

    • SEhtesham
      New Member
      • Aug 2011
      • 45

      #3
      hi Rabbit,
      Above is my exact code its just that i have only truncated the names of my Database tables and combobox.What is hapenning is i suppose i have 3 record in Table1 and 5 in table2 then 3 multiplied by 5=15 thats what the number of records which are obviously duplicated records and such result is expected from cross join.i wanted some guidance that in while reading a row through Dr can i check wheather this particular records is already added to my combo or not.If not added than it should add if yes than it should skip.

      Comment

      • SEhtesham
        New Member
        • Aug 2011
        • 45

        #4
        Hi frnds done with my Issue.
        Im just checking in the combobox that weather the data in dr already exists in combo or not.This way
        Code:
        if combo1.Items.Contain(dr(0))=false then
        combo1.Items.Add(dr(0))
        end if

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          You still haven't told us how you know a record is duplicated nor did you give us a sample of the data.

          A cross join creates no "duplicated " records if there was no duplicated records in the original tables. Duplication is a business logic issue.

          Comment

          • SEhtesham
            New Member
            • Aug 2011
            • 45

            #6
            Hi,
            actually i used the wrong term may as duplication.
            My issue was the records were getting repeated.Like just for an example
            I have a Designation master which has only 3 designations in it
            MD,CEO,Presiden t.But when i was running the Query and the data was getting filled using dr it was repeatedly adding the same records again till the loop was executing.
            Like my table1 had 3 records and table2 had 5 so it was executing 15 times because of cross join and getting my each designation value number of times.
            I hope this has cleared what exactly my question was.
            If needed can provide with further explanation.
            Thanks for support

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              If you only need the one column from the one table. You shouldn't be joining to the other tables in the first place.

              Comment

              Working...