Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Monday, March 11, 2013

How to Use Three Tier Architecture in SharePoint 2013 Visual WebPart



 SharePoint 2013 List Back end

Ascx
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ThreeLayesr.ascx.cs" Inherits="ThreeLayesr.ThreeLayesr.ThreeLayesr" %>
<table cellpadding="0" cellspacing="0" align="center">
    <tr><td colspan="2" align="center"></td></tr>
    <tr><td>Enter UserName</td><td>
        <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
        </td></tr>
    <tr><td>Enter PassWord</td><td>
        <asp:TextBox ID="txtPassWord" runat="server"></asp:TextBox>
        </td></tr>
    <tr><td colspan="2" align="center">
        <asp:Button ID="btnSave" runat="server" Text="Save Record" OnClick="btnSave_Click" />
        </td></tr>
</table>
Ascx.Cs
using System;
using System.ComponentModel;
using System.Web.UI.WebControls.WebParts;

namespace ThreeLayesr.ThreeLayesr
{
    [ToolboxItemAttribute(false)]
    public partial class ThreeLayesr : WebPart
    {
        // Uncomment the following SecurityPermission attribute only when doing Performance Profiling on a farm solution
        // using the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready
        // for production. Because the SecurityPermission attribute bypasses the security check for callers of
        // your constructor, it's not recommended for production purposes.
        // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]
        DataBase db = new DataBase();
        public ThreeLayesr()
        {
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            EntityClass obj=new EntityClass();
            obj.strUserName = txtUserName.Text;
            obj.strPassWord = txtPassWord.Text;
            db.InsertSample(obj.strUserName, obj.strPassWord);
            Page.Response.Write("Insertion Successful");
        }
    }
}
Business Layer CS File
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ThreeLayesr
{
    public class EntityClass
    {
        string UserName;
        string PassWord;

        public string strUserName
        {
            get
            {
                return UserName;
            }

            set
            {
                UserName = value;
            }
        }

        public string strPassWord
        {
            get
            {
                return PassWord;
            }

            set
            {
                PassWord = value;
            }
        }
    }
}
Database Layer CS File
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint;
using System.Security;

namespace ThreeLayesr
{
    public class DataBase
    {
       
        public void InsertSample(string strUserName, string strPassWord)
        {
            SPListItemCollection myColl = SPContext.Current.Web.Lists["Login"].Items;
            SPListItem item = myColl.Add();
            item["Title"] = strUserName;
            item["PassWord"] = strPassWord;
            item.Update();
        }
    }
}


No comments:

Post a Comment