Click here to Skip to main content
16,016,345 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to retrive data from two related tables in a database "in VB.net" using left outer join and display the results in Datagridview
Posted
Comments
SwarupDChavan 19-Mar-14 9:28am    
basically why do you need to apply join in front-end when you can get the specified data from the backend itself.

1 solution

VB
Sub LoadData()
     If oCn.State = ConnectionState.Closed Then
         oCn.Open()
     End If
     Dim cmd As New SqlClient.SqlCommand("SELECT T_Cat.CategoryID, T_Cat.Cat_Name, TN.estbProducerID, TN.NewProducer FROM  T_Cat LEFT OUTER JOIN TN ON T_Cat.CategoryID = TN.CategoryID", oCn)
     Dim da As New SqlClient.SqlDataAdapter(cmd)
     Dim ds As New DataSet("bpl")
     Dim i As Integer = 0
     Me.DataGridView1.Rows.Clear()
     Try
         da.Fill(ds, "bpl")
         If ds.Tables(0).Rows.Count > 0 Then
             While (i <> ds.Tables(0).Rows.Count)
                 Me.DataGridView1.Rows.Add()
                 Me.DataGridView1.Item(0, i).Value = i + 1
                 Me.DataGridView1.Item(1, i).Value = ds.Tables(0).Rows(i).Item(0).ToString
                 Me.DataGridView1.Item(2, i).Value = ds.Tables(0).Rows(i).Item(1).ToString
                 Me.DataGridView1.Item(3, i).Value = ds.Tables(0).Rows(i).Item(2).ToString
                 Me.DataGridView1.Item(4, i).Value = ds.Tables(0).Rows(i).Item(3).ToString
                 Me.DataGridView1.Item(5, i).Value = Format(Date.Today, "dd-MMM-yyyy")
                 i = i + 1
             End While
         End If
     Catch ex As Exception
         MsgBox(ex.Message)
     End Try
 End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900