Loading, please wait...

A to Z Full Forms and Acronyms

How to create a calculator in ASP.NET using Session

Jun 24, 2021 Calculator, ASP.NET, SessionTechnique, 12551 Views
In this article , we will learn how we can build our own customized calculator using ASP.NET with Session Technique.

How to create a calculator in ASP.NET using Session

ASP.NET is an open-source web development platform that comes under the .NET framework, which provides a programming model, a comprehensive software infrastructure, and various services required to build web applications.

ASP.NET works on the HTTP protocol and uses the HTTP commands and policies to build web applications.

ASP.NET is a part of the Microsoft .Net platform. ASP.NET applications are compiled codes, written using the extensible and reusable components or objects present in the .Net framework.

So let us quickly get started to make our own basic calculator in ASP.NET.

STEP 1:  Open Visual Studio or the IDE which you are comfortable with and create a new project. Select the ASP.NET Web Application that comes under the .net framework template.

Click on create and then name the project as per your choice, for eg. “Calculate” and then press OK.

 

 

Select “Empty” from the options present on the next screen and then you can add folders and core references also. Here select WebForms for the calculator.

 

 

The main screen  on your workspace will appear like this:

STEP 2:  Right-click on the Project name,i.e. Calculate and then click on “add” in the dropdown list. A new list will appear out of which we have to select “new item” and finally select “WebForms”.

You will come across the below-given screen.

Now, click on the Design button. A clear web form will appear on the screen. Now, you can simply Drag and Drop the controls from the toolbox as per your choice.

You can take the help of the design shown below in the picture.

STEP 3: A basic calculator will contain numerical digits from 0 to 9, + , - , * , /, . , = .One additional button namely “clear” is added if any value is to be removed from the textbox .

Therefore, we will be adding 16 buttons in the form.

Now, label the names on the buttons according to their respective functionalities as following.

 

 

STEP 4: Once it is done, you will have to specify the action that has to be performed by any button when it is pressed. Double click on the button you will come across the coding section of that button. You have to specify the functionality of each button one by one in the code section.

The coding should look like this:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;




namespace calculator

{

    public partial class WebForm1 : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {




        }

        result r = new result();




        protected void btnOne_Click(object sender, EventArgs e)

        {

            txtanswer.Text = txtanswer.Text + "1";




        }




        protected void btnTwo_Click(object sender, EventArgs e)

        {

            txtanswer.Text = txtanswer.Text + "2";

          

        }




        protected void btnThree_Click(object sender, EventArgs e)

        {

            txtanswer.Text = txtanswer.Text + "3";




        }




        protected void btnFour_Click(object sender, EventArgs e)

        {




            txtanswer.Text = txtanswer.Text + "4";




        }




        protected void btnFive_Click(object sender, EventArgs e)

        {

            txtanswer.Text = txtanswer.Text + "5";

           

        }




        protected void btnSix_Click(object sender, EventArgs e)

        {

            txtanswer.Text = txtanswer.Text + "6";




        }




        protected void btnSeven_Click(object sender, EventArgs e)

        {

            txtanswer.Text = txtanswer.Text + "7";




        }




        protected void btnNine_Click(object sender, EventArgs e)

        {

            txtanswer.Text = txtanswer.Text + "9";




        }




        protected void btnZero_Click(object sender, EventArgs e)

        {

            txtanswer.Text = txtanswer.Text + "0";

           

        }




        protected void Add_Click(object sender, EventArgs e)

        {

           

            if (txtanswer.Text == string.Empty)

            {

                Response.Write("<script>alert('No input values provided')</script>");

            }




            else

            {

                Session["Val"] = txtanswer.Text;

                Session["Sign"] = "add";

                txtanswer.Text = string.Empty;

            }

        }




        protected void Sub_Click(object sender, EventArgs e)

        {

           

            if (txtanswer.Text == string.Empty)

            {

                Response.Write("<script>alert('No inpt values provided')</script>");

            }




            else

            {

                Session["Val"] = txtanswer.Text;

                Session["Sign"] = "subtract";

                txtanswer.Text = string.Empty;

            }

        }




        protected void Multiply_Click(object sender, EventArgs e)

        {

         

            if (txtanswer.Text == string.Empty)

            {

                Response.Write("<script>alert('No input values provided')</script>");

            }




            else

            {

                Session["Val"] = txtanswer.Text;

                Session["Sign"] = "multiply";

                txtanswer.Text = string.Empty;

            }

        }




        protected void Divide_Click(object sender, EventArgs e)

        {

            if (txtanswer.Text == string.Empty)

            {

                Response.Write("<script>alert('No input values provided')</script>");

            }




            else

            {

                Session["Val"] = txtanswer.Text;

                Session["Sign"] = "divide";

                txtanswer.Text = string.Empty;

            }

        }




        protected void btnOk_Click(object sender, EventArgs e)

        {

            if (txtanswer.Text == string.Empty)

            {

                Response.Write("<script>alert('You did not specified inputs')</script>");

            }

            else

            {

                Session["Val1"] = txtanswer.Text;

                txtanswer.Text = string.Empty;

            

                if (Session["Sign"].ToString() == "add")

                {

                    txtanswer.Text = r.add(Convert.ToInt32(Session["Val"]), Convert.ToInt32(Session["Val1"])).ToString();

                }

                else if (Session["Sign"].ToString() == "subtract")

                {

                    txtanswer.Text = r.subtract(Convert.ToInt32(Session["Val"]), Convert.ToInt32(Session["Val1"])).ToString();

                 

                }

                else if (Session["Sign"].ToString() == "multiply")

                {

                                     

                        txtanswer.Text = r.multiply(Convert.ToInt32(Session["Val"]), Convert.ToInt32(Session["Val1"])).ToString();

                 

                }

                else if (Session["Sign"].ToString() == "divide")

                {

                    txtanswer.Text = r.divide(Convert.ToInt32(Session["Val"]), Convert.ToInt32(Session["Val1"])).ToString();

                

                }

                else

                {

                    Response.Write("<script>alert('No operation was recorded')</script>");

                }

            }

    }




        protected void btnEight_Click(object sender, EventArgs e)

        {

            txtanswer.Text = txtanswer.Text + "8";

        }




        protected void btnClear_Click(object sender, EventArgs e)

        {

            txtanswer.Text = string.Empty;

        }

    }

}

Please note that we have used the Session technique for saving the previous value of the operand and a class named result has been derived here to define the functionality of the operators.

The coding part for the class goes somewhat like this:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;




namespace calculator

{

    public class result

    {

        public int add(int value1, int value2)

        {

            return( value1 + value2);

        }

        public int subtract(int value1, int value2)

        {

            return (value1 - value2);

        }

        public int multiply(int value1, int value2)

        {

            return (value1 * value2);

        }

        public int divide(int value1, int value2)

        {

            return (value1 / value2);

        }

    }

}

The code can be simply understood and implemented.

 Now, our application is ready to run on the web browser.

OUTPUT:

Performing a simple calculation of 2 * 5.

1. 

 

2.

 

 

3.

 

 

 

4.

 

A to Z Full Forms and Acronyms

Related Article