Solved

VB.net Problem loading dataset into DataGridView

Posted on 2014-11-15
5
213 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:murbro
[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:murbro
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:murbro
ID: 40445792
Thanks
0
 
LVL 63

Expert Comment

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

Featured Post

Independent Software Vendors: 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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

734 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