0% found this document useful (0 votes)
16 views

Program List 3

This document contains code for two .NET web application assignments. The first assignment involves creating a basic calculator application with operations of addition, subtraction, multiplication and division. The second assignment calculates an employee's salary based on their present days, leave days, fixed salary amount and overtime hours. Both assignments utilize MVC framework and include controller, model and view code to create the applications.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Program List 3

This document contains code for two .NET web application assignments. The first assignment involves creating a basic calculator application with operations of addition, subtraction, multiplication and division. The second assignment calculates an employee's salary based on their present days, leave days, fixed salary amount and overtime hours. Both assignments utilize MVC framework and include controller, model and view code to create the applications.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Marwadi University

Faculty of Engineering and Technology


Department of Information and Communication Technology
Subject: .NET Technology Aim: Various Task
(01CT1518)
Assignment No: 03 Date: 05/11/2023 Enrolment No: 92100133031

1. Write a program to show Calculator in Web Application

HomeController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApplication2.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
[HttpPost]
public ActionResult Index(string num1, string num2, string operation)
{
try
{
double a = double.Parse(num1);
double b = double.Parse(num2);
double c = 0;
if (operation == "Addition")
{
c = a + b;
}
else if (operation == "Subtraction")
{
c = a - b;
}
else if (operation == "Multiplication")
{
c = a * b;
}
else
{
if (b == 0)
{
ViewBag.Result = "Divide by zero";
return View();
Marwadi University
Faculty of Engineering and Technology
Department of Information and Communication Technology
Subject: .NET Technology Aim: Various Task
(01CT1518)
Assignment No: 03 Date: 05/11/2023 Enrolment No: 92100133031

}
else
{
c = a / b;
}

}
ViewBag.Result = c;
return View();
}
catch (Exception e)
{
ViewBag.Result = "Divide by zero";
return View();
}
}
}

}
Inder.cshtml:
@model WebApplication2.Controllers.Calculator
@{
ViewBag.Title = "Assignment-3";
}
<h2>Index</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
<h4>Calculator</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.num1, htmlAttributes: new { @class = "control-label col-md-
2" })
<div class="col-md-10">
@Html.EditorFor(model => model.num1, new
{
htmlAttributes = new
{
@class =
"form-control"
}
})
@Html.ValidationMessageFor(model => model.num1, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.num2, htmlAttributes: new { @class = "control-label col-md-
2" })
<div class="col-md-10">
@Html.EditorFor(model => model.num2, new
Marwadi University
Faculty of Engineering and Technology
Department of Information and Communication Technology
Subject: .NET Technology Aim: Various Task
(01CT1518)
Assignment No: 03 Date: 05/11/2023 Enrolment No: 92100133031

{
htmlAttributes = new
{
@class =
"form-control"
}
})
@Html.ValidationMessageFor(model => model.num2, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Addition" name="operation" class="btn btn_default" />
<input type="submit" value="Subtraction" name="operation" class="btn btn_default" />
<input type="submit" value="Division" name="operation" class="btn btn_default" />
<input type="submit" value="Multiplication" name="operation" class="btn
btn-default" />
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2">Result</label>
<input type="text" class="form-control" value="@ViewBag.Result" />
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>

Calculator.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace WebApplication2.Controllers
{
public class Calculator
{
[Required]
public double num1 { get; set; }
[Required]
public double num2 { get; set; }
}
}

Output:
Marwadi University
Faculty of Engineering and Technology
Department of Information and Communication Technology
Subject: .NET Technology Aim: Various Task
(01CT1518)
Assignment No: 03 Date: 05/11/2023 Enrolment No: 92100133031

2. Write a program to calculate employee’s salary based on present days, leave days, fixed
salary amount, overtime hours.

HomeController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication2.Controllers;
Marwadi University
Faculty of Engineering and Technology
Department of Information and Communication Technology
Subject: .NET Technology Aim: Various Task
(01CT1518)
Assignment No: 03 Date: 05/11/2023 Enrolment No: 92100133031

namespace WebApplication1.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}

[HttpPost]
public ActionResult Index(Calculator calculator)
{
double perDaySalary = calculator.FixedSalary / 30;
double salary = (calculator.PresentDays - calculator.LeaveDays) * perDaySalary +
calculator.OvertimeHours * perDaySalary * 2;
ViewBag.Salary = salary;
return View();
}
}
}

Index.cshtml:
@model WebApplication2.Controllers.Calculator

@{
ViewBag.Title = "Employee Salary Calculator";
}

<h2>Employee Salary Calculator</h2>

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
<h4>Employee Details</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.PresentDays, htmlAttributes: new { @class = "control-label col-
md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PresentDays, new { htmlAttributes = new { @class = "form-
control" } })
@Html.ValidationMessageFor(model => model.PresentDays, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.LeaveDays, htmlAttributes: new { @class = "control-label col-
md-2" })
<div class="col-md-10">
Marwadi University
Faculty of Engineering and Technology
Department of Information and Communication Technology
Subject: .NET Technology Aim: Various Task
(01CT1518)
Assignment No: 03 Date: 05/11/2023 Enrolment No: 92100133031

@Html.EditorFor(model => model.LeaveDays, new { htmlAttributes = new { @class = "form-


control" } })
@Html.ValidationMessageFor(model => model.LeaveDays, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.FixedSalary, htmlAttributes: new { @class = "control-label col-
md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FixedSalary, new { htmlAttributes = new { @class = "form-
control" } })
@Html.ValidationMessageFor(model => model.FixedSalary, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.OvertimeHours, htmlAttributes: new { @class = "control-label
col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OvertimeHours, new { htmlAttributes = new { @class =
"form-control" } })
@Html.ValidationMessageFor(model => model.OvertimeHours, "", new { @class = "text-danger"
})
</div>
</div>

<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Calculate Salary" class="btn btn-default" />
</div>
</div>

<div class="form-group">
<label class="control-label col-md-2">Salary</label>
<input type="text" class="form-control" value="@ViewBag.Salary" />
</div>
</div>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>
Controller.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace WebApplication2.Controllers
{
Marwadi University
Faculty of Engineering and Technology
Department of Information and Communication Technology
Subject: .NET Technology Aim: Various Task
(01CT1518)
Assignment No: 03 Date: 05/11/2023 Enrolment No: 92100133031

public class Calculator


{
[Required]
public double PresentDays { get; set; }

[Required]
public double LeaveDays { get; set; }

[Required]
public double FixedSalary { get; set; }

[Required]
public double OvertimeHours { get; set; }
}

Output:

3. Write a program to download (excel) calculated salary in program 10.

HomeController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication2.Controllers;

namespace WebApplication1.Controllers
{
public class HomeController : Controller
{
Marwadi University
Faculty of Engineering and Technology
Department of Information and Communication Technology
Subject: .NET Technology Aim: Various Task
(01CT1518)
Assignment No: 03 Date: 05/11/2023 Enrolment No: 92100133031

public ActionResult DownloadExcel(Calculator calculator)


{
double perDaySalary = calculator.FixedSalary / 30;
double salary = (calculator.PresentDays - calculator.LeaveDays) * perDaySalary +
calculator.OvertimeHours * perDaySalary * 2;

// Create a DataTable to store the parameters


DataTable parameterTable = new DataTable();
parameterTable.Columns.Add("Fixed Salary");
parameterTable.Columns.Add("Present Days");
parameterTable.Columns.Add("Leave Days");
parameterTable.Columns.Add("Overtime Hours");

// Add data to the table


parameterTable.Rows.Add(calculator.FixedSalary, calculator.PresentDays, calculator.LeaveDays,
calculator.OvertimeHours);

// Create an Excel file from the DataTable


var stream = new MemoryStream();
var workbook = new XLWorkbook();
var worksheet = workbook.Worksheets.Add("Parameters");
worksheet.Cell(1, 1).Value = "Fixed Salary";
worksheet.Cell(1, 2).Value = "Present Days";
worksheet.Cell(1, 3).Value = "Leave Days";
worksheet.Cell(1, 4).Value = "Overtime Hours";
worksheet.Cell(2, 1).Value = calculator.FixedSalary;
worksheet.Cell(2, 2).Value = calculator.PresentDays;
worksheet.Cell(2, 3).Value = calculator.LeaveDays;
worksheet.Cell(2, 4).Value = calculator.OvertimeHours;
workbook.SaveAs(stream);

// Set the response headers


Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=Parameters.xlsx");

// Write the Excel file to the response stream


Response.BinaryWrite(stream.ToArray());
Response.End();

return new EmptyResult();


}
}
}

Index.cshtml: Just Adding action button to Download the file.

<div>
@Html.ActionLink("Back to List", "Index")
@Html.ActionLink("Download Parameters", "DownloadExcel", "Home", new { calculator = Model },
new { @class = "btn btn-primary" })
</div>
Marwadi University
Faculty of Engineering and Technology
Department of Information and Communication Technology
Subject: .NET Technology Aim: Various Task
(01CT1518)
Assignment No: 03 Date: 05/11/2023 Enrolment No: 92100133031

Output:

4. Write a program to generate captcha and validate it.

Index.cshtml:
@{
ViewBag.Title = "CAPTCHA";
}

< h2 > CAPTCHA </ h2 >

@using(Html.BeginForm("ValidateCaptcha", "Captcha", FormMethod.Post))


{
< label for= "userInput" > Enter CAPTCHA Text:</ label >

< input type = "text" id = "userInput" name = "userInput" required >

< input type = "submit" value = "Submit" >

< img src = "@Url.Action("Index", "Captcha")" alt = "CAPTCHA Image" />

< br />

@if(!string.IsNullOrEmpty(ViewBag.Message))
{
< p > @ViewBag.Message </ p >
}
Marwadi University
Faculty of Engineering and Technology
Department of Information and Communication Technology
Subject: .NET Technology Aim: Various Task
(01CT1518)
Assignment No: 03 Date: 05/11/2023 Enrolment No: 92100133031

ValidationResult.cshtml:
@{
ViewBag.Title = "Validation Result";
}

< h2 > Validation Result </ h2 >

@if(!string.IsNullOrEmpty(ViewBag.Message))
{
< p > @ViewBag.Message </ p >
}

CaptchaController.cs:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Web.Mvc;

public class CaptchaController : Controller


{
public ActionResult Index()
{
string randomText = GenerateRandomText();
TempData["Captcha"] = randomText;

byte[] captchaBytes = GenerateCaptchaImage(randomText);

return File(captchaBytes, "image/jpeg");


}
private string GenerateRandomText()
{
Random random = new Random();
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, 5).Select(s => s[random.Next(s.Length)]).ToArray());
}
private byte[] GenerateCaptchaImage(string text)
{
using (Bitmap bitmap = new Bitmap(130, 50))
{
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

graphics.Clear(Color.LightGray); // Change the background color here

Random random = new Random();

// Drawing random lines on the image for additional security


Marwadi University
Faculty of Engineering and Technology
Department of Information and Communication Technology
Subject: .NET Technology Aim: Various Task
(01CT1518)
Assignment No: 03 Date: 05/11/2023 Enrolment No: 92100133031

for (int i = 0; i < 10; i++)


{
int x1 = random.Next(0, bitmap.Width);
int y1 = random.Next(0, bitmap.Height);
int x2 = random.Next(0, bitmap.Width);
int y2 = random.Next(0, bitmap.Height);

graphics.DrawLine(new Pen(Color.LightBlue), x1, y1, x2, y2); // Change the line color here
}

Font font = new Font("Arial", 20);


SolidBrush brush = new SolidBrush(Color.Black); // Change the text color here

graphics.DrawString(text, font, brush, 10, 10);

using (MemoryStream stream = new MemoryStream())


{
bitmap.Save(stream, ImageFormat.Jpeg);
return stream.ToArray();
}
}
}
}

[HttpPost]
public ActionResult ValidateCaptcha(string userInput)
{
string captchaText = TempData["Captcha"] as string;

if (userInput.Equals(captchaText, StringComparison.OrdinalIgnoreCase))
{
ViewBag.Message = "CAPTCHA validation successful!";
}
else
{
ViewBag.Message = "CAPTCHA validation failed!";
}

return View("ValidationResult");
}
}

Output:
Marwadi University
Faculty of Engineering and Technology
Department of Information and Communication Technology
Subject: .NET Technology Aim: Various Task
(01CT1518)
Assignment No: 03 Date: 05/11/2023 Enrolment No: 92100133031

5. Write a program to allow user to provide inputs and send email to specified address in
inputs which as follows :

A) To Email :

B) CC Email :

C) BCC Email :

D) Subject :

E) Body :

Note : Buttons to be provided to Send, Reset inputs.

HomeController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
namespace WebApplication2.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(FormCollection formCollection)
{
string subject = formCollection["subject"];
string toemail = formCollection["useremail"];
string ccemail = formCollection["ccemail"];
string bccemail = formCollection["bccemail"];
Marwadi University
Faculty of Engineering and Technology
Department of Information and Communication Technology
Subject: .NET Technology Aim: Various Task
(01CT1518)
Assignment No: 03 Date: 05/11/2023 Enrolment No: 92100133031

string body = formCollection["text_body"];


WebMail.Send(toemail, subject, body, null, ccemail, null, true, null,
bccemail, null, null, null, null);
ViewBag.msg = "Email Sent sucessfully";
return View();
}

}
}

Index.cshtml:
@{
ViewBag.Title = "Index";
}
@if (ViewBag.msg != null)
{
<div class="alert-success">@ViewBag.msg</div>
}
<h2>Index</h2>
@using (Html.BeginForm())
{
<div class="form-group">
<label class="control-label col-md-2">To Email: </label>
<input type="text" name="useremail" class="form-control" />
</div>
<div class="form-group">
<label class="control-label col-md-2">CC Email: </label>
<input type="text" name="ccemail" class="form-control" />
</div>
<div class="form-group">
<label class="control-label col-md-2">BCC Email: </label>
<input type="text" name="bccemail" class="form-control" />
</div>
<div class="form-group">
<label class="control-label col-md-2">Subject : </label>
<input type="text" name="subject" class="form-control" />
</div>
<div class="form-group">
<label class="control-label col-md-2">To Email: </label>
<textarea name="text_body" class="form-control"></textarea>
</div>
<div class="form-group">
<div class="input-group-btn">
<button type="submit" class="btn btn-success">Send Email</button>
</div>
<div class="input-group-btn form-group">
<input type="reset" value="Reset" class="btn btn-default" />
</div>
</div>
}
Marwadi University
Faculty of Engineering and Technology
Department of Information and Communication Technology
Subject: .NET Technology Aim: Various Task
(01CT1518)
Assignment No: 03 Date: 05/11/2023 Enrolment No: 92100133031

Output:

6. Write a program to perform Insert update delete operations on Tables specified in SQL
assignments.

Index.cshtml:
@model IEnumerable<Product>

<h2>Products</h2>

<table class= "table" >


< tr >
< th > Product ID </ th >

< th > Product Name </ th >

< th > Price </ th >

< th ></ th >

</ tr >
@foreach(var item in Model)
{
< tr >
< td > @item.ProductID </ td >
< td > @item.ProductName </ td >
< td > @item.Price </ td >
< td >
@Html.ActionLink("Edit", "Edit", new { id = item.ProductID }) |
@Html.ActionLink("Details", "Details", new { id = item.ProductID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.ProductID })
</ td >
</ tr >
Marwadi University
Faculty of Engineering and Technology
Department of Information and Communication Technology
Subject: .NET Technology Aim: Various Task
(01CT1518)
Assignment No: 03 Date: 05/11/2023 Enrolment No: 92100133031

}
</ table >
<p>
@Html.ActionLink("Create New", "Create")
</ p >

Edit.cshtml:
@model Product

<h2>Edit</h2>

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

< div class= "form-group" >


@Html.LabelFor(model => model.ProductName)
@Html.EditorFor(model => model.ProductName, new { htmlAttributes = new { @class = "form-
control" } })
</ div >

< div class= "form-group" >


@Html.LabelFor(model => model.Price)
@Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
</ div >

< input type = "submit" value = "Save" class= "btn btn-default" />
}

create.cshtml
@model Product

<h2>Create</h2>

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

< div class= "form-group" >


@Html.LabelFor(model => model.ProductName)
@Html.EditorFor(model => model.ProductName, new { htmlAttributes = new { @class = "form-
control" } })
</ div >

< div class= "form-group" >


@Html.LabelFor(model => model.Price)
@Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
</ div >

< input type = "submit" value = "Create" class= "btn btn-default" />
}

delete.cshtml:
Marwadi University
Faculty of Engineering and Technology
Department of Information and Communication Technology
Subject: .NET Technology Aim: Various Task
(01CT1518)
Assignment No: 03 Date: 05/11/2023 Enrolment No: 92100133031

@model Product

<h2>Delete</h2>

<h3>Are you sure you want to delete this?</h3>


<div>
<h4>Product</h4>
<hr />
<dl class= "dl-horizontal" >
< dt >
@Html.DisplayNameFor(model => model.ProductName)
</ dt >
< dd >
@Html.DisplayFor(model => model.ProductName)
</ dd >

< dt >
@Html.DisplayNameFor(model => model.Price)
</ dt >
< dd >
@Html.DisplayFor(model => model.Price)
</ dd >
</ dl >
@using(Html.BeginForm())
{
@Html.AntiForgeryToken()
<p>

< input type = "submit" value = "Delete" class= "btn btn-default" />

</ p >
}
</ div >

dbcontext.cs:
using System.Collections.Generic;
using System.Data.Entity;

public class YourDbContext : DbContext


{
public YourDbContext() : base("name=DefaultConnection")
{
}

public DbSet<Product> Products { get; set; }


}

product.cs
using System.ComponentModel.DataAnnotations;

public class Product


{
public int ProductID { get; set; }
Marwadi University
Faculty of Engineering and Technology
Department of Information and Communication Technology
Subject: .NET Technology Aim: Various Task
(01CT1518)
Assignment No: 03 Date: 05/11/2023 Enrolment No: 92100133031

[Required]
public string ProductName { get; set; }

[Required]
public decimal Price { get; set; }
}

ProductController.cs:
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web.Mvc;

public class ProductController : Controller


{
private YourDbContext db = new YourDbContext();

public ActionResult Index()


{
// Retrieve products from the database
var products = db.Products.ToList();
return View(products);
}

public ActionResult Create()


{
return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Product product)
{
if (ModelState.IsValid)
{
// Save the product to the database
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("Index");
}

return View(product);
}

public ActionResult Edit(int id)


{
Product product = db.Products.Find(id);
if (product == null)
{
return HttpNotFound();
}
Marwadi University
Faculty of Engineering and Technology
Department of Information and Communication Technology
Subject: .NET Technology Aim: Various Task
(01CT1518)
Assignment No: 03 Date: 05/11/2023 Enrolment No: 92100133031

return View(product);
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Product product)
{
if (ModelState.IsValid)
{

db.Entry(product).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}

return View(product);
}

public ActionResult Delete(int id)


{
Product product = db.Products.Find(id);
if (product == null)
{
return HttpNotFound();
}

return View(product);
}

[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Product product = db.Products.Find(id);
db.Products.Remove(product);
db.SaveChanges();
return RedirectToAction("Index");
}
}
Marwadi University
Faculty of Engineering and Technology
Department of Information and Communication Technology
Subject: .NET Technology Aim: Various Task
(01CT1518)
Assignment No: 03 Date: 05/11/2023 Enrolment No: 92100133031

Output:

You might also like