Sunday, 11 September 2016

Storing multiple tables in DataSet using Sql Data Adapter

Let's see with an example:
We are creating 3 table and a procedure
select * from employeeMaster




select * from employeeDesignationDetails



 select * from employeeLocationDetails


Creating Procedure for accessing data from the above tables as 3 result set
create proc A_GetmultipleResultSet

as
select * from employeeMaster
select * from employeeDesignationDetails
select * from employeeLocationDetails


In the aspx file put the following code


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MultipleTableinDataSet.aspx.cs" Inherits="ADONET.MultipleTableinDataSet" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <div>
                <asp:Label ID="lblEmployeeDetails" runat="server" Text="Employee Details"></asp:Label>
                <asp:GridView ID="gdvEmlpoyee" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                    <EditRowStyle BackColor="#999999" />
                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#E9E7E2" />
                    <SortedAscendingHeaderStyle BackColor="#506C8C" />
                    <SortedDescendingCellStyle BackColor="#FFFDF8" />
                    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                </asp:GridView>

            </div>
            <div>
                <asp:Label ID="lblLocationDetails" runat="server" Text="Location Details"></asp:Label>
                <asp:GridView ID="gdvLocationDetails" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                    <EditRowStyle BackColor="#999999" />
                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#E9E7E2" />
                    <SortedAscendingHeaderStyle BackColor="#506C8C" />
                    <SortedDescendingCellStyle BackColor="#FFFDF8" />
                    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                </asp:GridView>

            </div>
            <div>
                <asp:Label ID="lblDesignationDetails" runat="server" Text="Designation Details"></asp:Label>
                <asp:GridView ID="gdvDesignationDetails" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                    <EditRowStyle BackColor="#999999" />
                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#E9E7E2" />
                    <SortedAscendingHeaderStyle BackColor="#506C8C" />
                    <SortedDescendingCellStyle BackColor="#FFFDF8" />
                    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                </asp:GridView>

            </div>
        </div>
    </form>
</body>
</html>


 In code behind add the following code

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ADONET
{
    public partial class MultipleTableinDataSet : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String CS = ConfigurationManager.ConnectionStrings["EmplyoeeDB"].ConnectionString;
            using (SqlConnection sqlcon = new SqlConnection(CS))
            {
                SqlCommand sqlcmd = new SqlCommand("A_GetmultipleResultSet", sqlcon);
                sqlcmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adapter = new SqlDataAdapter(sqlcmd);
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                ds.Tables[0].TableName = "EmployeeDetails";
                ds.Tables[1].TableName = "DesignationDetails";
                ds.Tables[2].TableName = "LocationDetails";

                gdvEmlpoyee.DataSource = ds.Tables["EmployeeDetails"];
                gdvEmlpoyee.DataBind();

                gdvLocationDetails.DataSource = ds.Tables["LocationDetails"];
                gdvLocationDetails.DataBind();

                gdvDesignationDetails.DataSource = ds.Tables["DesignationDetails"];
                gdvDesignationDetails.DataBind();
            }
        }
    }
}


Lets Execute our application we will get the following output


0 comments:

Post a Comment