Monday 18 June 2012

How to retrive More than one Select Statement in a single Command.And Show in a GridView.Using Normal query and Storeprocedure.

Page Design

Source Code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!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></title>
    <style type="text/css">
        .style1
        {
            background-color: #66CCFF;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:gridview ID="Gridview1" runat="server"></asp:gridview>
    <asp:gridview ID="Gridview2" runat="server"></asp:gridview>
        <asp:Button ID="BtnReader" runat="server" Text="Data Reader"
            onclick="BtnReader_Click" style="position:absolute; top: 310px; left: 325px;"
            CssClass="style1" />
        <asp:Button ID="BtnAdapter" runat="server" Text="Data Adapter"
            onclick="BtnAdapter_Click" style="position:absolute; top: 312px; left: 191px;"
            CssClass="style1"  />
        <asp:Button ID="BtnStored" runat="server" Text="Stored Procedure"
            style="position:absolute; top: 311px;" onclick="BtnStored_Click"
            CssClass="style1" />
    </div>
    </form>
</body>
</html>


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

public partial class Default3 : System.Web.UI.Page
{
    SqlConnection con;
    SqlDataAdapter da;
    SqlCommand cmd;
    DataSet ds;
    SqlDataReader dr;
    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["Satya"].ConnectionString);
        con.Open();
        if (!IsPostBack)
        {
        }
    }
    public void satya_DA()
    {
        da = new SqlDataAdapter("
Select * from Rakesh;Select * from Demo", con);
        ds = new DataSet();
        da.Fill(ds);
        Gridview1.DataSource = ds.Tables[0].DefaultView;
        Gridview1.DataBind();
        Gridview2.DataSource = ds.Tables[1].DefaultView;
        Gridview2.DataBind();
    }
    public void satya_Reader()
    {
        using (cmd = new SqlCommand("
Select * from Rakesh;Select * from Demo", con))
        {
            using (dr = cmd.ExecuteReader())
            {
                Gridview1.DataSource = dr;
                Gridview1.DataBind();
                dr.NextResult();
                Gridview2.DataSource = dr;
                Gridview2.DataBind();
            }
        }
    }
    public void satya_SP()
    {
        cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;

    //Create StoreProcedure and Call Here
        cmd.CommandText = "
exec Select_Employee_Sallary_Qualification exec Select_Employee_Sallary_Qualification";
        da = new SqlDataAdapter(cmd);
        ds = new DataSet();
        da.Fill(ds);
        Gridview1.DataSource = ds.Tables[0].DefaultView;
        Gridview1.DataBind();
        Gridview2.DataSource = ds.Tables[1].DefaultView;
        Gridview2.DataBind();
        da.Dispose();
        con.Close();
    }
    protected void BtnReader_Click(object sender, EventArgs e)
    {
        satya_Reader();
    }
    protected void BtnAdapter_Click(object sender, EventArgs e)
    {
        satya_DA();
    }
    protected void BtnStored_Click(object sender, EventArgs e)
    {
        satya_SP();
    }
}


No comments:

Post a Comment