Monday, April 30, 2012

Fill Multiple GridView using DataReader Batch Select Statement

Design (Source Code)





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

<!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
        {
            width: 100%;
        }
        .style2
        {
            color: #FF0066;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        <table class="style1">
            <tr>
                <td class="style2">
                    Fill Grid View using DataReader Multiple Select statement
                </td>
            </tr>
            <tr>
                <td>
                    <asp:GridView ID="GridViewLedger" runat="server">
                    </asp:GridView>
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    <asp:GridView ID="GridViewDemo" runat="server">
                    </asp:GridView>
                </td>
            </tr>
              <tr>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    <asp:GridView ID="GridViewParty" runat="server">
                    </asp:GridView>
                </td>
            </tr>
        </table>
 
    </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.SqlClient;
using System.Data;
using System.Configuration;

public partial class FrmDataReader : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataReader dr;

    protected void Page_Load(object sender, EventArgs e)
    {
     
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);    
        SelectData();    

    }
    private void SelectData()
    {
        try
        {
            con.Open();

         
            using (cmd = new SqlCommand("select top(10) ledgerid,ledgername  from fms.fms_ledgers;select * from fms_demo;select * from fms.fms_parties", con))
            {

                // in case of multiple result set you will not be able to specify the  CommandBehavior
                // if so you will not be able to get the next result set from the reader

                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    // bind the first resultset
                    GridViewLedger.DataSource = reader;
                    GridViewLedger.DataBind();

                    reader.NextResult();

                    // bind the second resultset
                    GridViewDemo.DataSource = reader;
                    GridViewDemo.DataBind();

                    reader.NextResult();

                    // bind the third resultset
                    GridViewParty.DataSource = reader;
                    GridViewParty.DataBind();

                }

            }

        }
        catch (Exception ex)
        {
            string x = ex.Message.ToString();
        }
        finally
        {
            con.Close();
        }
    }
}





No comments:

Post a Comment