Solved

VB.net Problem loading dataset into DataGridView

Posted on 2014-11-15
5
204 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
  • 3
  • 2
5 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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
Comment Utility
Hi

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

Accepted Solution

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

Author Closing Comment

by:murbro
Comment Utility
Thanks
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Not a problem, glad to help.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now