Monday 27 August 2012

How to bind three Gridview Control with a Single Stored Procedure

How to bind three Gridview Control with a Single Stored Procedure






In Disconnected Architechture

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="twogridbindwith single stored procedure.aspx.cs" Inherits="twogridbindwith_single_stored_procedure" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    <asp:GridView ID="GridView1" runat="server" BackColor="Red" Font-Bold="True"
        ForeColor="White">
    </asp:GridView>
    <br />
    <asp:GridView ID="GridView2" runat="server" BackColor="Green" ForeColor="White">
    </asp:GridView>
    <br />
    <asp:GridView ID="GridView3" runat="server" BackColor="#0099FF"
        Font-Bold="True" ForeColor="White">
    </asp:GridView>
    </form>
</body>
</html>


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class twogridbindwith_single_stored_procedure : System.Web.UI.Page
{
    SqlDataAdapter da;
    DataSet ds;
   
    string constr = ConfigurationManager.ConnectionStrings["employyeeConnectionString"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        da = new SqlDataAdapter("exec [twotable]", constr);
        ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds.Tables[1];
        GridView1.DataBind();
        GridView2.DataSource = ds.Tables[0];
        GridView2.DataBind();
        GridView3.DataSource = ds.Tables[2];
        GridView3.DataBind();
    }
}
In Connected Architechture


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="twogridbindwith single stored procedure.aspx.cs" Inherits="twogridbindwith_single_stored_procedure" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    <asp:GridView ID="GridView1" runat="server" BackColor="Red" Font-Bold="True"
        ForeColor="White">
    </asp:GridView>
    <br />
    <asp:GridView ID="GridView2" runat="server" BackColor="Green" ForeColor="White">
    </asp:GridView>
    <br />
    <asp:GridView ID="GridView3" runat="server" BackColor="#0099FF"
        Font-Bold="True" ForeColor="White">
    </asp:GridView>
    </form>
</body>
</html>

Stored Procedure


create proc [dbo].[twotable]
as
begin



select product.*,[member name]=(select top 1 member.member_name from member where member.productid=product.productid) from product
select product.*,[member number]=(select count( member.member_name) from member where member.productid=product.productid ) from product

select product.*,[Total Member number]=(select count( member.member_name) from member where member.productid=product.productid) from product

end

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class twogridbindwith_single_stored_procedure : System.Web.UI.Page
{
   
    SqlDataReader dr;
    SqlCommand com;
    string constr = ConfigurationManager.ConnectionStrings["employyeeConnectionString"].ConnectionString;
    SqlConnection con;
   
    protected void Page_Load(object sender, EventArgs e)
    {
        con=new SqlConnection (constr );
        com = new SqlCommand("exec [twotable]", con);
        con.Open();

        dr = com.ExecuteReader();
        GridView1.DataSource = dr;
        GridView1.DataBind();
        dr.NextResult();
        GridView2.DataSource = dr;
        GridView2.DataBind();
        dr.NextResult();
        GridView3.DataSource = dr;
        GridView3.DataBind();
    }
}

0 comments: