Avatar of Murray Brown
Murray Brown
Flag for United Kingdom of Great Britain and Northern Ireland

asked on 

VB.net Problem loading dataset into DataGridView

Hi

I am using the following code to load the results of a dataset into a DataGridView. Nothing loads into the DataGridView. What am I doing wrong

    Private Sub btnLoadDataSet_Click(sender As System.Object, e As System.EventArgs) Handles btnLoadDataSet.Click
        Me.DataGridView1.DataSource = Create_DataSet()
    End Sub

    Function Create_DataSet() As DataSet
        Try
            Dim connectionString As String = Globals.ThisAddIn.oRIGHT.lblConnectionString.Text

            ' Create a SqlConnection to the Northwind database.
            Using connection As SqlConnection = New SqlConnection( _
               connectionString)

                ' Create a SqlDataAdapter for the Suppliers table.
                Dim suppliersAdapter As SqlDataAdapter = _
                   New SqlDataAdapter()

                ' A table mapping names the DataTable.
                suppliersAdapter.TableMappings.Add("Table", "Suppliers")

                ' Open the connection.
                connection.Open()
                Console.WriteLine("The SqlConnection is open.")

                ' Create a SqlCommand to retrieve Suppliers data.
                Dim suppliersCommand As SqlCommand = New SqlCommand( _
                   "SELECT ID, Company FROM Suppliers;", _
                   connection)
                suppliersCommand.CommandType = CommandType.Text

                ' Set the SqlDataAdapter's SelectCommand.
                suppliersAdapter.SelectCommand = suppliersCommand

                ' Fill the DataSet.
                Dim oDataSet As DataSet = New DataSet("Suppliers")
                suppliersAdapter.Fill(oDataSet)

                ' Create a second SqlDataAdapter and SqlCommand to get
                ' the Products table, a child table of Suppliers.  
                Dim productsAdapter As SqlDataAdapter = _
                    New SqlDataAdapter()
                productsAdapter.TableMappings.Add("Table", "Products")

                Dim productsCommand As SqlCommand = New SqlCommand( _
                   "SELECT ID, [Supplier IDs] FROM Products;", _
                   connection)
                productsAdapter.SelectCommand = productsCommand

                ' Fill the DataSet.
                productsAdapter.Fill(oDataSet)

                ' Close the connection.
                connection.Close()
                Console.WriteLine("The SqlConnection is closed.")

                ' Create a DataRelation to link the two tables
                ' based on the SupplierID.
                Dim parentColumn As DataColumn = _
                   oDataSet.Tables("Suppliers").Columns("ID")
                Dim childColumn As DataColumn = _
                   oDataSet.Tables("Products").Columns("Supplier IDs")
                Dim relation As DataRelation = New  _
                   System.Data.DataRelation("SuppliersProducts", _
                   parentColumn, childColumn)
                oDataSet.Relations.Add(relation)

                Console.WriteLine( _
                   "The {0} DataRelation has been created.", _
                   relation.RelationName)

                Create_DataSet = oDataSet
            End Using



        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Function
Visual Basic.NET

Avatar of undefined
Last Comment
Fernando Soto

8/22/2022 - Mon