Open Visual Studio and create a new ASP.NET Web Application, then right click on Solution and Add New Project. After that add Class Library. Give name DAL(Data Access Layer) and write the following code in Data Access Layer.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data.SqlClient;
- using System.Data;
- using System.Configuration;
-
- namespace DAL
- {
- public class Class1
- {
- SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString());
- SqlCommand cmd;
- DataTable dt;
-
- public DataTable Person(int Id)
- {
- using (cmd = new SqlCommand("Three_Tier_Test", con))
- {
- try
- {
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.AddWithValue("@Id", Id);
- if(con.State.Equals(ConnectionState.Closed))
- con.Open();
- SqlDataAdapter da=new SqlDataAdapter(cmd);
- dt=new DataTable();
- da.Fill(dt);
- return dt;
-
- }
- catch(Exception ex)
- {
- throw ex;
- }
- }
- }
- }
- }
Build this Layer
Now in the same way add BAL (Business Access Layer), this is also a Class Library. Write the following code in BAL:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data;
-
- namespace BAL
- {
- public class Class1
- {
- public DataTable Persons(int Id)
- {
- try
- {
- DAL.Class1 objDAL = new DAL.Class1();
- return objDAL.Person(Id);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- }
- }
Now Add a WebForm and Add a button, GridView and Label and a Textbox on it.
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication10.WebForm1" %>
- <!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>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <table>
- <tr>
- <td>
- <asp:Label ID="lblId" runat="server" Text="User Id"></asp:Label>
- </td>
- <td>
- <asp:TextBox ID="txtId" runat="server"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td>
- <asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
- </td>
- </tr>
- <tr>
- <td>
- <asp:GridView ID="gvDisplay" runat="server" AutoGenerateColumns="true"></asp:GridView>
- </td>
- </tr>
- </table>
- </div>
- </form>
- </body>
- </html>
Above is code behind on cs, after that write the following code:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- namespace WebApplication10
- {
- public partial class WebForm1 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!Page.IsPostBack)
- {
- gvDisplay.Visible = false;
- }
- }
-
- protected void btnSubmit_Click(object sender, EventArgs e)
- {
- BAL.Class1 objBal = new BAL.Class1();
- gvDisplay.DataSource= objBal.Persons(Convert.ToInt32(txtId.Text));
- gvDisplay.DataBind();
- gvDisplay.Visible = true;
- }
- }
- }
Run the application. Do not forget to add reference of BAL and DAL in the application. Also Add Reference DAL in BAL as from BAL we are calling DAL method. This is just the beginning, I will expand it to CRUD Operations and interview questions very soon.