Saturday, 5 April 2014

A Small Policy Plan Application in Asp.Net

Ans:-
Here i have created 4 (.aspx).
1.For create Policy. 
  i have taken two TextBoxes (policyid & policyname), for Empty TextBoxes provide validation using JavaScript and three Buttons(Add,Clear & Save).
2.For create Plan. 
 i have taken two TextBoxes (planid & planname), for Empty TextBoxes provide JavaScript validation and three Buttons(Add,Clear & Save).
3.For save policy plan
 i have taken two DropDownList(choose policy & choose plan), provide javascript validation to DDL  and one Button(save).
4.For Show created policy wise plan
 i have taken one Gridview.
5.For GetConnection add one Class file in App_Code folder.

Means overall concept is here1. i will create one policy then 2. create plans like(Gold,Platinum,Silver,Bronze) and then 3.one policy can keep more than one plans using DropDownList and save then 4. show created policywise plans in Gridview. So for code see in below..

1.



AddPolicy.aspx:-

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

<!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>Add Policy Page</title>
    <script type="text/javascript" language="javascript">
         function validate() {
             var id = document.getElementById("txtPolicyId");
             var name = document.getElementById("txtPolicyName");
             if (id.value == "") {
                 alert("Plz Click Add Button to generate Id..");
                 id.focus();
                 return false;
             }
             else if (name.value == "") {
                 name.focus();
                 alert("Plz Enter Name..");
                 return false;
             }
         }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <center>
    <div>
    <asp:hyperlink ID="Hyperlink1" NavigateUrl="~/AddPolicy.aspx" runat="server">AddPolicy</asp:hyperlink>
&nbsp;
<asp:hyperlink ID="Hyperlink2" NavigateUrl="~/AddPlans.aspx" runat="server">AddPlans</asp:hyperlink>
&nbsp;
<asp:hyperlink ID="Hyperlink4" NavigateUrl="~/SavePolicyPlans.aspx" runat="server">SavePolicyPlans</asp:hyperlink>
&nbsp;
<asp:hyperlink ID="Hyperlink3" NavigateUrl="~/ShowPolicyPlanDatas.aspx" runat="server">ShowPolicyPlansData</asp:hyperlink>
  
    </div>
    <div><b>Add Policy Here</b></div>
    <div>
    
    <table>
    <tr>
    <td>Policy Id</td>
    <td>:</td>
    <td><asp:TextBox ID="txtPolicyId" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
    <td>Policy Name</td>
    <td>:</td>
    <td><asp:TextBox ID="txtPolicyName" runat="server"></asp:TextBox></td>
    </tr>
    </table>
   
    </div>
    <br />
    
    <asp:Label ID="lblMsg" runat="server" Text="Label"></asp:Label>
    
    <br />
    <div>
    
    <asp:Button ID="btnAdd" runat="server" Text="ADD" onclick="btnAdd_Click" />&nbsp;
     <asp:Button ID="btnClear" runat="server" Text="Clear" onclick="btnClear_Click" />&nbsp;
      <asp:Button ID="btnSave" runat="server" Text="Save" OnClientClick="return validate()" onclick="btnSave_Click" />&nbsp;
     
    </div>
     </center>
    </form>
</body>
</html>

AddPolicy.aspx.cs:-

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

public partial class _Default : System.Web.UI.Page 
{
    Class1 obj_Class1 = new Class1();
    SqlConnection cn;
    SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        cn = new SqlConnection(obj_Class1.GetConnection());
        cmd = new SqlCommand("  select isnull(MAX(policyid),0)+1 as policyid from policy", cn);
        var j="";
        try
        {
            cn.Open();
            j = cmd.ExecuteScalar().ToString();
            txtPolicyId.Text = j;
        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message;
        }
        finally
        {
            cn.Close();
        }
        
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        txtPolicyId.Text = string.Empty.ToString();
      txtPolicyName.Text = string.Empty.ToString();
      lblMsg.Text = string.Empty.ToString();

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        cn = new SqlConnection(obj_Class1.GetConnection());
        cmd = new SqlCommand("insert into policy(policyid,policyname) values(@policyid,@policyname)", cn);
        cmd.Parameters.AddWithValue("@policyid", txtPolicyId.Text);
        cmd.Parameters.AddWithValue("@policyname",txtPolicyName.Text);
        int i;
        try
        {
            cn.Open();
            i = cmd.ExecuteNonQuery();
            if (i >= 0)
            {
                lblMsg.Text = "Inserted Successfully..";
            }
            else
            {
                lblMsg.Text = "Inserted Failed..";
            }
        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message;
        }
        finally
            {
                cn.Close();
            }

    }
}

2.



AddPlans.aspx:-

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

<!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>Add Plans Page</title>
     <script type="text/javascript" language="javascript">
         function validate() {
             var id = document.getElementById("txtPlanId");
             var name = document.getElementById("txtPlanName");
             if (id.value == "") {
                 id.focus();
                 alert("Plz Click Add Button to generate Id..");
                 return false;
             }
             else if (name.value == "") {
                 name.focus();
                 alert("Plz Enter Name..");
                 return false;
             }
         }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <center>
    <div>
     <asp:hyperlink ID="Hyperlink1" NavigateUrl="~/AddPolicy.aspx" runat="server">AddPolicy</asp:hyperlink>
&nbsp;
<asp:hyperlink ID="Hyperlink2" NavigateUrl="~/AddPlans.aspx" runat="server">AddPlans</asp:hyperlink>
&nbsp;
<asp:hyperlink ID="Hyperlink4" NavigateUrl="~/SavePolicyPlans.aspx" runat="server">SavePolicyPlans</asp:hyperlink>
&nbsp;
<asp:hyperlink ID="Hyperlink3" NavigateUrl="~/ShowPolicyPlanDatas.aspx" runat="server">ShowPolicyPlansData</asp:hyperlink>
   
    </div>
    <div><b>Add Plans Here</b></div>
   <div>
    
    <table>
    <tr>
    <td>Plan Id</td>
    <td>:</td>
    <td><asp:TextBox ID="txtPlanId" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
    <td>Plan Name</td>
    <td>:</td>
    <td><asp:TextBox ID="txtPlanName" runat="server"></asp:TextBox></td>
    </tr>
    </table>
   
    </div>
    <br />
     
    <asp:Label ID="lblMsg" runat="server" Text="Label"></asp:Label>
   
    <br />
    <div>
    
    <asp:Button ID="btnAdd" runat="server" Text="ADD" onclick="btnAdd_Click" />&nbsp;
     <asp:Button ID="btnClear" runat="server" Text="Clear" onclick="btnClear_Click" />&nbsp;
      <asp:Button ID="btnSave" runat="server" Text="Save" OnClientClick="return validate()" onclick="btnSave_Click" />
     
    </div>
     </center>
    </form>
</body>
</html>

AddPlans.aspx.cs:-

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

public partial class AddPlans : System.Web.UI.Page
{
    Class1 obj_Class1 = new Class1();
    SqlConnection cn;
    SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        txtPlanName.Focus();
        cn = new SqlConnection(obj_Class1.GetConnection());
        cmd = new SqlCommand("select isnull(MAX(planid),0)+1 as planid from plans", cn);
        var j = "";
        try
        {
            cn.Open();
            j = cmd.ExecuteScalar().ToString();
            txtPlanId.Text = j;
        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message;
        }
        finally
        {
            cn.Close();
        }
        
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        txtPlanId.Text = string.Empty.ToString();
        txtPlanName.Text = string.Empty.ToString();
        lblMsg.Text = string.Empty.ToString();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
            cn = new SqlConnection(obj_Class1.GetConnection());
            cmd = new SqlCommand("insert into plans(planid,planname) values(@planid,@planname)", cn);
            cmd.Parameters.AddWithValue("@planid", txtPlanId.Text);
            cmd.Parameters.AddWithValue("@planname", txtPlanName.Text);
            int i;
            try
            {
                cn.Open();
                i = cmd.ExecuteNonQuery();
                if (i >= 0)
                {
                    lblMsg.Text = "Inserted Successfully..";
                }
                else
                {
                    lblMsg.Text = "Inserted Failed..";
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.Message;
            }
            finally
            {
                cn.Close();
            }
        }
}

3.


SavePolicyPlans.aspx:-

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

<!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>Save Policy Plan Page</title>
    <script type="text/javascript" language="javascript">
        function validate() {

            var items1 = document.getElementById('<%=DDLPolicyNameId.ClientID%>');
            var items2 = document.getElementById('<%=DDLPlanNameID.ClientID%>')
            if (items1.selectedIndex == 0) {
                items1.focus();
                alert("Plz Enter PolicyNameId..");
                return false;
            }
            else if (items2.selectedIndex == 0) {
            items2.focus();
            alert("Plz Enter PlanNameId..");
            return false;
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <center>
    <div>
    
<asp:hyperlink ID="Hyperlink1" NavigateUrl="~/AddPolicy.aspx" runat="server">AddPolicy</asp:hyperlink>
&nbsp;
<asp:hyperlink ID="Hyperlink2" NavigateUrl="~/AddPlans.aspx" runat="server">AddPlans</asp:hyperlink>
&nbsp;
<asp:hyperlink ID="Hyperlink4" NavigateUrl="~/SavePolicyPlans.aspx" runat="server">SavePolicyPlans</asp:hyperlink>
&nbsp;
<asp:hyperlink ID="Hyperlink3" NavigateUrl="~/ShowPolicyPlanDatas.aspx" runat="server">ShowPolicyPlansData</asp:hyperlink>
  
    </div>
    <div><b>Save PolicyPlans Here</b></div>
    <div>
    <table>
     <tr>
    <td>Choose Policy</td>
    <td>:</td>
    <td><asp:DropDownList ID="DDLPolicyNameId" runat="server"></asp:DropDownList></td>
    <td></td>
    <td></td>
    </tr>
     <tr>
    <td>Choose Plans</td>
    <td>:</td>
    <td><asp:DropDownList ID="DDLPlanNameID" runat="server"></asp:DropDownList></td>
    </tr>
   
    </table>
    </div>
    <br />
  <asp:Label ID="lblMsg" runat="server" Text="Label"></asp:Label>
   <br />
    <div>
      <asp:Button ID="btnSave" runat="server" Text="Save" OnClientClick="return validate()" onclick="btnSave_Click" />
    </div>
     </center>
    </form>
</body>
</html>

SavePolicyPlans.aspx.cs:-

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

public partial class AddPlans : System.Web.UI.Page
{
    Class1 obj_Class1 = new Class1();
    SqlConnection cn;
    SqlCommand cmd;
    SqlDataAdapter da;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDDLPolicyNameId();
            BindPlanName();
        }
    }
    void BindDDLPolicyNameId()
    {
        cn = new SqlConnection(obj_Class1.GetConnection());
        cmd = new SqlCommand("select policyname+'-'+CAST(policyid as varchar) as policyname,policyid from policy", cn);
        da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        DDLPolicyNameId.DataTextField = "policyname";
        DDLPolicyNameId.DataValueField = "policyid";
        DDLPolicyNameId.DataSource = dt;
        DDLPolicyNameId.DataBind();
        DDLPolicyNameId.Items.Insert(0, "--Choose Policy--");
    }
    void BindPlanName()
    {
        cn = new SqlConnection(obj_Class1.GetConnection());
        cmd = new SqlCommand("select planname+'-'+Cast(planid as varchar) as planname,planid from plans", cn);
        da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        DDLPlanNameID.DataTextField = "planname";
        DDLPlanNameID.DataValueField = "planid";
        DDLPlanNameID.DataSource = dt;
        DDLPlanNameID.DataBind();
        DDLPlanNameID.Items.Insert(0, "--Choose Plan--");
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        cn = new SqlConnection(obj_Class1.GetConnection());
        cmd = new SqlCommand();
        cmd.Connection = cn;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "spSavePolicyPlansData";
        cmd.Parameters.AddWithValue("@policypid", SqlDbType.VarChar).Value = DDLPolicyNameId.SelectedValue;         //Get Integer value from DDL
        cmd.Parameters.AddWithValue("@policypname", SqlDbType.VarChar).Value = DDLPolicyNameId.SelectedItem.Text;   //Get String value from DDL
        cmd.Parameters.AddWithValue("@planpid", SqlDbType.VarChar).Value = DDLPlanNameID.SelectedValue;
        cmd.Parameters.AddWithValue("@planpname", SqlDbType.VarChar).Value = DDLPlanNameID.SelectedItem.Text;;
        cmd.Parameters.AddWithValue("@mode", "insert");
        int i=0;
        try
        {
            cn.Open();
            i = cmd.ExecuteNonQuery();
            if (i >= 0)
            {
                lblMsg.Text = "Inserted Successfully..";
            }
            else
            {
                lblMsg.Text = "Inserted Failed..";
            }
        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message;
        }
        finally
        {
            cn.Close();
        }

    }
}

4.


ShowPolicyPlanDatas.aspx:-

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

<!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>Show Policy Plan Data Page</title>
</head>
<body>
    <form id="form1" runat="server">
     <center>
     <div>
      <asp:hyperlink ID="Hyperlink1" NavigateUrl="~/AddPolicy.aspx" runat="server">AddPolicy</asp:hyperlink>
&nbsp;
<asp:hyperlink ID="Hyperlink2" NavigateUrl="~/AddPlans.aspx" runat="server">AddPlans</asp:hyperlink>
&nbsp;
<asp:hyperlink ID="Hyperlink4" NavigateUrl="~/SavePolicyPlans.aspx" runat="server">SavePolicyPlans</asp:hyperlink>
&nbsp;
<asp:hyperlink ID="Hyperlink3" NavigateUrl="~/ShowPolicyPlanDatas.aspx" runat="server">ShowPolicyPlansData</asp:hyperlink>
  
    
     </div>
     <div><b>Show PolicyPlan Data</b></div>
    <div>
   
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
       
    </div>
     </center>
    </form>
</body>
</html>

ShowPolicyPlanDatas.aspx.cs:-

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

public partial class ShowPolicyPlanDatas : System.Web.UI.Page
{
    Class1 obj_Class1 = new Class1();
    SqlConnection cn;
    SqlCommand cmd;
    SqlDataAdapter da;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGvData();
        }
    }
    void BindGvData()
    {
        cn = new SqlConnection(obj_Class1.GetConnection());
        cmd = new SqlCommand("select * from policyplans", cn);
        da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();

    }
}

5.

In Class.cs:- file for GetConnection..

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

/// <summary>
/// Summary description for Class1
/// </summary>
public class Class1
{
          public Class1()
          {
                   //
                   // TODO: Add constructor logic here
                   //
          }
    public string GetConnection()
    {
        string strCon = "server=.;database=subsdb;user id=sa;pwd=123";
        return strCon;
    }
}

Create Tables &  Stored Procedures:-

1.create table policy(policyid int constraint pr primary key ,policyname varchar(30))

2.create table plans(planid int constraint pr2 primary key,planname varchar(30))   
  
  --using on cascade delete & on cascade UPDATE
 3. create table policyplans(policypid int constraint fr1 foreign key references policy(policyid) on delete cascade on update cascade,policypname varchar(30),planpid int constraint fr2 foreign key references plans(planid) on delete cascade on update cascade,planpname varchar(30) )

4. insert into policy values(1,'subspolicy');
    insert into plans values(101,'Gold');

 --system generating policyid
  5.select isnull(MAX(policyid),0)+1 as policyid from policy
  
  --append string & integer
 6. select policyname+'-'+CAST(policyid as varchar) as policyname,policyid from policy
  
  --using mode as insert & update
7.create proc spSavePolicyPlansData(@policypid int,@policypname varchar(30),@planpid int,@planpname varchar(30),@mode varchar(30)) 
as 
begin
if @mode='insert'
begin 
insert into policyplans(policypid,policypname,planpid,planpname) values(@policypid,@policypname,@planpid,@planpname)
end
else if @mode='update'
begin
update policyplans set planpname=@planpname,policypname=@policypname  where policypid=@policypid and planpid=@planpid
end
end

No comments:

Post a Comment