We are providing online training of realtime Live project on Asp.Net MVC with Angular and Web API. For more information click here. If you have any query then drop the messase in CONTACT FORM

Wednesday, May 27, 2015

How to implement Calculation of Sum, Subtraction, Multiplication, Division with store all the value in database in 3 tier Architecture .


Step1:
->Create a Database(Ex:SelfTest),
->Create a Table(Ex: Calculation),
See below

Step2:
->Open VS,
->Go to file->New->Website
->Give any name(Ex:CalWithNtier)
->Click ok
Step3:
->Right click project and web form,
->Again Add a class(Ex: DOL (Data Object Layer))
->Again Add a class(Ex: DAL (Data Access Layer))
->Again Add a class(Ex: BOL(business Access Layer))
Step3:
->Write Code DOL.cs)
See below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for DOL
/// </summary>
public class DOL
{
public int Value1 { get; set; }
public int Value2 { get; set; }
public int Sum { get; set; }
public int Sub { get; set; }
public int Mul { get; set; }
public int Div { get; set; }
//we can sam variable for cal

}
->Write code DAL.cs
see below
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>
///
namespace Cal
{
public class DAL
{
public int AddRecord(DOL objdol)
{
SqlConnection con = new SqlConnection(@"Data Source=mithilesh;database=SelfTest;User Id=sa;Password=12345");
string strCommand = "Insert into Calculation values('" + objdol.Value1 + "','" + objdol.Value2 + "','" + objdol.Sum + "')";
SqlCommand cmd = new SqlCommand(strCommand, con);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
return i;
}
}
}
->Write code BOL.cs
see below
using Cal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for BOL
/// </summary>
///
public class BOL
{
public int AddMethod(DOL objdol)
objdol.Sum = objdol.Value1 + objdol.Value2;
DAL objdal = new DAL();
int i = objdal.AddRecord(objdol);
return I;
}
ublic int SubMethod(DOL objdol)
{
objdol.Sub = objdol.Value1 - objdol.Value2;
DAL objdal = new DAL();
int i = objdal.AddRecord(objdol);
return i;
}
public int MulMethod(DOL objdol)
{
objdol.Mul = objdol.Value1 * objdol.Value2;
DAL objdal = new DAL();
int i = objdal.AddRecord(objdol);
return I;
}
public int DivMethod(DOL objdol)
{
objdol.Div = objdol.Value1 / objdol.Value2;
DAL objdal = new DAL();
int i = objdal.AddRecord(objdol);
return i;
}
}
->Go to Default.aspx and write the code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<div align="center">
<h1>Calculation</h1>
</div>
<asp:Label ID="lblmessage" runat="server"></asp:Label>
<table style="font-size:20px;">
<tr>
<th>Enter First value</th>
<th>:</th>
<td>
<asp:TextBox ID="txtValue1" runat="server"></asp:TextBox><br />
</td>
</tr>
<tr>
<th>Enter Second value</th>
<th>:</th>
<td>
<asp:TextBox ID="txtValue2" runat="server"></asp:TextBox></td>
</tr>
<tr>
<th>Result</th>
<th>:</th>
<td>
<asp:Label ID="lblResult" runat="server"></asp:Label></td>
</tr>
<tr>
<td colspan="3">
<br />
<asp:Button ID="btnAdd" runat="server" Font-Bold="true" Text="Add" OnClick="btnAdd_Click" />&nbsp;
<asp:Button ID="btnSub" runat="server" Font-Bold="true" Text="Sub" OnClick="btnSub_Click" />&nbsp;
<asp:Button ID="btnMul" runat="server" Font-Bold="true" Text="Mul" OnClick="btnMul_Click" />&nbsp;
<asp:Button ID="btnDiv" runat="server" Font-Bold="true" Text="Div" OnClick="btnDiv_Click" />&nbsp;
<asp:Button ID="btnClear" runat="server" Font-Bold="true" Text="Reset" OnClick="btnClear_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
 ->Go to the Default.aspx.cs and write the 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
{
 
protected void Page_Load(object sender, EventArgs e)

{
}

public void method()

{ if (txtValue1.Text == "")


{
 
lblmessage.Text = "<b style='color:red'>Value1 must be not empty</b>";

txtValue1.Focus();

}
 
else if (txtValue2.Text == "")


{
lblmessage.Text = "<b style='color:red'>value2 must be not empty</b>";

txtValue2.Focus();
 }
}
 
protected void btnAdd_Click(object sender, EventArgs e)


{
if (txtValue1.Text == "" || txtValue2.Text == "")
 {
 method();
 }else
{

DOL objdol = new DOL();

objdol.Value1 = int.Parse(txtValue1.Text);

objdol.Value2 = int.Parse(txtValue2.Text);

BOL objbol = new BOL();

int i = objbol.AddMethod(objdol);


lblResult.Text = (objdol.Sum).ToString();
if (i == 1)
{
  lblmessage.Text = "<b style='color:green'>Record is inserted</b>";
 }
else
 {lblmessage.Text = "<b style='color:red'>Insertion Failed</b>";

}

}

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

if (txtValue1.Text == "" || txtValue2.Text == "")

{
 
method();

}
 
else

{
 
DOL objdol = new DOL();

objdol.Value1 = int.Parse(txtValue1.Text);

objdol.Value2 = int.Parse(txtValue2.Text);

BOL objbol = new BOL();

int i = objbol.SubMethod(objdol);



lblResult.Text = (objdol.Sub).ToString();
 
if (i == 1)


{
 
lblmessage.Text = "<b style='color:green'>Record is subracted</b>";
 } else
{
 lblmessage.Text = "<b style='color:red'>Insertion Failed</b>";


}

}

}
 
 
protected void btnMul_Click(object sender, EventArgs e)

{ 
if (txtValue1.Text == "" || txtValue2.Text == "")
 {
 method();
 }
else
{
 
DOL objdol = new DOL();

objdol.Value1 = int.Parse(txtValue1.Text);

objdol.Value2 = int.Parse(txtValue2.Text);

BOL objbol = new BOL();

int i = objbol.MulMethod(objdol);


lblResult.Text = (objdol.Mul).ToString();

if (i == 1)
{
  lblmessage.Text = "<b style='color:green'>Record is multiplied</b>";
 
}

else


{lblmessage.Text = "<b style='color:red'>multiply Failed</b>";
}
}
}
 
protected void btnDiv_Click(object sender, EventArgs e)


{
 
if (txtValue1.Text == "" || txtValue2.Text == "")

{method();
 }
else
{
 
 DOL objdol = new DOL();

objdol.Value1 = int.Parse(txtValue1.Text);

objdol.Value2 = int.Parse(txtValue2.Text);

BOL objbol = new BOL();

int i = objbol.DivMethod(objdol);


lblResult.Text = (objdol.Div).ToString();
 
f (i == 1)

{
lblmessage.Text = "<b style='color:green'>Record is divided</b>";

}
else
{


lblmessage.Text = "<b style='color:red'>division Failed</b>";

}

}

}
 
protected void btnClear_Click(object sender, EventArgs e)


{
 
txtValue1.Text = "";

txtValue2.Text = "";
 }
} 
->Run the program
and see the out put
->Check the validation


->Click add
 
 ->Click Sub
 ->Click Mul
 
 ->Click Div
 ->Click Reset Button


No comments: