Solved

VB.net Problem loading dataset into DataGridView

Posted on 2014-11-15
5
214 Views
Last Modified: 2014-11-16
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
0
Comment
Question by:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40445745
This line of code,

Me.DataGridView1.DataSource = Create_DataSet()

returns a DataSet object and assigns it to the DataSource of the DataGridView. The DataSet holds a collection of DataTables. So Which of the tables do you want the DataGridView to Display? For Example this should work to display the Products table.

Me.DataGridView1.DataSource = Create_DataSet().Tables("Products")
0
 

Author Comment

by:Murray Brown
ID: 40445751
Hi

I wanted to display the combination of the Products and Suppliers table or say two columns from each
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40445758
In that case you will need two DataGridView's one for the Products and another for the Suppliers.
0
 

Author Closing Comment

by:Murray Brown
ID: 40445792
Thanks
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40445794
Not a problem, glad to help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question