Follow by Email

Monday, 24 September 2012

Select records in asp.net using three tier architecture

Application Layer:

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

<!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>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
        <Columns>
        <asp:ButtonField CommandName="Edit" Text="EDIT" />
        <asp:BoundField DataField="fname" HeaderText="First Name" />
        <asp:BoundField DataField="lname" HeaderText="Last Name" />
        </Columns>
       </asp:GridView>
  
    </div>
    </form>
</body>
</html>

BUSINESS ACCESS LAYER:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for BAL
/// </summary>
public class BAL
{

    DAL d1 = new DAL();
    DataSet ds = new DataSet();
    public BAL()
    {
        //
        // TODO: Add constructor logic here
        //
    }
   
    public DataSet select_BAL(string Str_Pro)
    {
       ds = d1.select_record("select_records");
       
        //DataSet ds = new DataSet();
        return ds;

    }


}

STORED PROCEDURE USED:

CREATE PROCEDURE select_records

AS
BEGIN

SELECT * FROM test_tab2

END


DATA ACCESS LAYER:

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

/// <summary>
/// Summary description for DAL
/// </summary>
public class DAL
{
    SqlConnection con = new SqlConnection();
    public DAL()
    {
        con = new SqlConnection("Data Source=EVEREST5026;Initial Catalog=sample;Integrated Security=True");
    }

    public DataSet select_record(string Str_Pro)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand(Str_Pro, con);
        cmd.CommandType = CommandType.StoredProcedure;

        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;
        da.Fill(ds);
        return ds;
       
    }
}

CODE BEHIND PAGE:

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

public partial class Show_Records : System.Web.UI.Page
{
    BAL b1 = new BAL();
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        fill_records();
    }

    private void fill_records()
    {
        ds = b1.select_BAL("select_records");

        GridView1.DataSource = ds;
        GridView1.DataBind();
        ds.Clear();

    }
}


0 comments:

Post a Comment


                                                            

 
Design by Abhinav Ranjan Sinha