1
 SqlConnection con = new SqlConnection(@"Data Source = M2\SQL2016; Initial Catalog = inventoryDB; Integrated Security = True");
 SqlDataAdapter sda = new SqlDataAdapter(@"select brnDB.catname, catDB.hsncode, brnDB.compname, itemDB.fullname, companyDB.compcode from brnDB cross join  itemDB cross join companyDB cross join catDB where catDB.catname=brnDB.catname and companyDB.compcode=2 order by fullname asc", con);
 DataTable dt = new DataTable();
 sda.Fill(dt);
 dataGridView1.Rows.Clear();
 dataGridView1.DataSource = dt;

I run this code, data is coming but multipul times (repeating more than 12 times)

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
vijay
  • 35
  • 10
  • 2
    Did you check your query in MS-SQL ? What is the result? Is it multiple or as you expect? – J.SMTBCJ15 Mar 20 '17 at 09:27
  • @ SMTBCJ15 I checked in SQL. There also same – vijay Mar 20 '17 at 09:29
  • what same? multiple ? attach a snapshot of your sql data returned . or > write dataGridView1.DataSource = null instead of dataGridView1.Rows.Clear(); – J.SMTBCJ15 Mar 20 '17 at 09:31
  • @ SMTBCJ15 I wrote what you told. But same problem. – vijay Mar 20 '17 at 09:38
  • Above code have no problem at all. And,it is impossible to trace out your problem until you give a snapshot of you data returned from table in SQL – J.SMTBCJ15 Mar 20 '17 at 09:40
  • @ SMTBCJ15 how to attach a snapshot in comments – vijay Mar 20 '17 at 09:41
  • [IMG]http://i64.tinypic.com/2rw8i0g.jpg[/IMG] – vijay Mar 20 '17 at 10:02
  • @SMTBCJ15 I added snapshot link. – vijay Mar 20 '17 at 10:04
  • I have looked at the image which you attached, what is multiple data there now? I didn't found multiple records there – J.SMTBCJ15 Mar 20 '17 at 10:07
  • @ SMTBCJ15 first record it self 3 times came below that record only. Like that all records – vijay Mar 20 '17 at 10:09
  • You have problem in your query where one company have ordered multiple products. For this you'll have to look into your query. – J.SMTBCJ15 Mar 20 '17 at 10:13
  • @ SMTBCJ15 there records repeating according to how many companies I am having. Now I am having 3 companies. so one record is adding into all companies so first one plues 3 times more like that it came. I created one more company and checked. – vijay Mar 20 '17 at 10:19

1 Answers1

1

Are you sure you need to do cross join instead of inner join?

Check out this question: CROSS JOIN vs INNER JOIN in SQL Server 2008

Community
  • 1
  • 1
Peter
  • 14,221
  • 15
  • 70
  • 110
  • @ Peter inner join some syntax error came so i used cross join – vijay Mar 20 '17 at 10:06
  • @ peter SqlDataAdapter sda = new SqlDataAdapter(@" select brnDB.brname, catDB.catname, itemDB.fullname from brnDB inner join catDB inner join itemDB on brnDB.brname=itemDB.brname ", con); – vijay Mar 20 '17 at 10:41
  • @vijay you need to specify what to join on: select brnDB.brname, catDB.catname, itemDB.fullname from brnDB inner join catDB on brnDB.catDBId = catDB.Id or something similar. Checkout this link for more information: https://www.w3schools.com/sql/sql_join_inner.asp – Peter Mar 20 '17 at 12:30