Solved

VB.net Problem loading dataset into DataGridView

Posted on 2014-11-15
5
212 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 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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