Saturday, 8 June 2013

 3 TIER ARCHITECTURE 

aspx code
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <table>
        <tr>
            <td>
                <asp:Label ID="l1" runat="server" Text="Id"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtid" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label2" runat="server" Text="Address"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtaddress" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label3" runat="server" Text="Email"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtemai" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label4" runat="server" Text="Cell"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtcell" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td>
                <asp:Button ID="btnsubmit" runat="server" Text="Submit" OnClick="btnsubmit_Click" />
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td>
                <asp:Label ID="lblerror" runat="server"></asp:Label>
            </td>
        </tr>
    </table>
</asp:Content>





c# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    BAL bal = new BAL();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        try
        {
            string sql = "insert into tbl_student([Name],[Address],[Email],[Mobile])values('" + txtname.Text + "','" + txtaddress.Text + "','" + txtemai.Text + "','" + txtcell.Text + "')";
            int id=bal.insert(sql);
            if (id == 1)
            {
                lblerror.Text = "data inserted";
            }
        }
        catch (Exception ex)
        {
            lblerror.Text = ex.Message;
        }
    }
}

Add App_code in that add  Two folders BAL and DAL add C# .cs files in both folders with the name BAL.cs,DAL.cs insert the following cde in BAL

BAL:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for BAL
/// </summary>
public class BAL
{
    DAL dal = new DAL();
public BAL()
{
//
// TODO: Add constructor logic here
//
}
    public int insert(string sql)
    {
        try
        {
            int id=dal.insert(sql);
            return id;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

}


DAL:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DAL
/// </summary>
public class DAL
{
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["class"].ToString());
public DAL()
{
//
// TODO: Add constructor logic here
//
}
    public int insert(string sql)
    {
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            int id=cmd.ExecuteNonQuery();
            return id;

        }
        catch (Exception ex)
        {
            throw ex;
        }

    }

}


DATABASE
create table admin ( id int identity(1000,1),Name varchar(200),Address varchar(200),Email varchar(200),Mobile varchar(200))

Webconfig

  <add name="con" connectionString="Data Source=.;Initial Catalog=3tirer;Integrated Security=true;"/>

OUTPUT



No comments:

Post a Comment

Receive All Free Updates Via Facebook.