ARTICLE

Multiple Tables in a Single Dataset

Posted by Mayur Dighe Articles | ADO.NET in VB.NET February 27, 2011
In this Article, we will learn how to store multiple tables in a single DataSet using SqlServer + VB.NET.
Reader Level:

In this Article, we will learn how to store multiple tables in a single DataSet using SqlServer + VB.NET.

Step 1:

  • Create One Database File named " DemoFile" in Microsoft SqlServer Management Studio Express
  • Create 3 Tables as follows
     
    • Employee_Info (EmpID, Name, Department, Designation)
    • Payment_Info (EmpID, BasicSalary, Allowance, GrossSalary)
    • Work_Info (EmpID, WorkAssigned)

Now come to Front End Window Forms Application in VB.NET

Step 2:

  • Take One ListBox (named List1), One Button (named cmdUpdate), Four Labels and One DataGridView (named GridView)

    MultiTable1.gif

Step 3: Code behind Form

Imports System.Data.SqlClient

Imports System.Data.SqlClient

Public Class DataSetDemo
    Dim Conn As New SqlConnection("Data Source=Studio\SqlExpress; Initial Catalog=DemoFile;Integrated Security=True")                                   'Create new SqlConnection String
    Dim Dset As New DataSet()              'Create new Instance of DataSet Class
    Dim adapt As SqlDataAdapter          'Create new Instance of SqlDataAdapter Class

    Public Sub Clear()                            'Clear the Previous Contents on Labels
        lblRow.Text = ""
        lblCol.Text = ""
    End Sub

    Private Sub cmdUpdate_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdUpdate.Click
 
        Dset.Clear()                      'Clear the DataSet Contents

        adapt = New SqlDataAdapter("select * from Employee_Info", Conn)
        adapt.Fill(Dset, "Employee_Info")            'Add Employee_Info Table to DataSet

        adapt = New SqlDataAdapter("select * from Payment_Info", Conn)
        adapt.Fill(Dset, "Payment_Info")             'Add Payment_Info Table to DataSet
 
        adapt = New SqlDataAdapter("select * from Work_Info", Conn)
        adapt.Fill(Dset, "Work_Info")                  'Add Work_Info Table to DataSet

'Now Select the TableName From ListBox And Load the Same in DataGridView
 For Each item As Integer In List1.SelectedIndices
                 GridView.DataSource = Dset.Tables(item)
                             Clear()
                lblRow.Text = Dset.Tables(item).Rows.Count.ToString         
'Count the no. of Rows in the Table
     lblCol.Text = Dset.Tables(item).Columns.Count.ToString      'Count the no. of Columns in the Table
            Next

    End Sub
End Class

Step 4: Execute the Code, Select Table Name and Click the Update Button

MultiTable2.gif

MultiTable3.gif

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor