.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Articles >> DataBase >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Multiple Tables in a Single Dataset

Posted By:Creative13       Posted Date: September 18, 2012    Points: 200    Category: DataBase    URL: http://www.dotnetspark.com  

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)


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)
                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

    End Sub
End Class

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



 Subscribe to Articles


Further Readings:

Author: netgrid         Company URL: http://www.dotnetspark.com
Posted Date: September 24, 2012

Thanks guys. I spent a full day trying to figure out why my client app was hanging.

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend