Whole project is in here:
https://github.com/LEOHAPPY/ASP.NET-Sample-Paper
WebForm1.aspx.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class WebForm1 : System.Web.UI.Page
{
static ArrayList receipts = new ArrayList();
string vehicle;
protected void Page_Load(object sender, EventArgs e)
{
vehicle = tb_vn.Text.ToString();
//clear history in the receipts when first load in this page
if (!IsPostBack)
{
receipts.Clear();
}
}
protected void add_btn_Click(object sender, EventArgs e)
{
//Bind to GridView1
Receipt r = new Receipt();
r.Shop = tb_receipt.Text.ToString();
r.Receipt_sn = tb_sn.Text.ToString();
r.Amount = Convert.ToDouble(tb_amount.Text.ToString());
receipts.Add(r);
GridView1.DataSource = receipts;
GridView1.DataBind();
//get dataset
dsVehicleRb ds = new dsVehicleRb();
dsVehicleRbTableAdapters.vehicleRbTableAdapter ad =
new dsVehicleRbTableAdapters.vehicleRbTableAdapter();
ad.Fill(ds.vehicleRb);
//update existingDebate
for (int i = 0; i < ds.vehicleRb.Count; i++)
{
if (vehicle == ds.vehicleRb.Rows[i][0].ToString())
{
ds.vehicleRb.Rows[i][1] =
Convert.ToDouble(ds.vehicleRb.Rows[i][1].ToString()) + r.Amount;
ad.Update(ds.vehicleRb);
break;
}
}
//refresh existingRebate textbox
tb_er.Text = findExistingRebate().ToString();
tb_rt.Text = null;
}
protected void btn_apply_Click(object sender, EventArgs e)
{
//calculate current rebate tickets
int currentRebateTickets = applyRebates(vehicle, receipts);
//dispaly on the rebate tickets textbox
tb_rt.Text = currentRebateTickets.ToString();
//refresh exiting rebate
tb_er.Text = findExistingRebate().ToString();
//refresh list and gridview
applyRefresh(currentRebateTickets);
}
public void applyRefresh(int currentRebateTickets)
{
if (currentRebateTickets != 0)
{
receipts.Clear();
GridView1.DataSource = receipts;
GridView1.DataBind();
tb_receipt.Text = null;
tb_sn.Text = null;
tb_amount.Text = null;
}
}
public int applyRebates(string vehicle, ArrayList receipts)
{
//logic: ½ hour car-parking is credited to the shopper’s vehicle for every $25 spent on shopping accumulated in shopping receipts
double appliedRebates = 0.0;
//get existingRebate
double exitingRebate = findExistingRebate();
//calculate rebate tickets
int currentRebateTickets = Convert.ToInt16(Math.Floor(exitingRebate / 25));
//apply to database
//get dataset
dsVehicleRb ds = new dsVehicleRb();
dsVehicleRbTableAdapters.vehicleRbTableAdapter ad =
new dsVehicleRbTableAdapters.vehicleRbTableAdapter();
ad.Fill(ds.vehicleRb);
//update existingDebate
for (int i = 0; i < ds.vehicleRb.Count; i++)
{
if (vehicle == ds.vehicleRb.Rows[i][0].ToString())
{
appliedRebates = exitingRebate - currentRebateTickets * 25;
ds.vehicleRb.Rows[i][1] = appliedRebates;
ad.Update(ds.vehicleRb);
break;
}
}
return currentRebateTickets;
}
public dsVehicleRb useDataset()
{
//fill dataset
dsVehicleRb ds = new dsVehicleRb();
dsVehicleRbTableAdapters.vehicleRbTableAdapter ad =
new dsVehicleRbTableAdapters.vehicleRbTableAdapter();
ad.Fill(ds.vehicleRb);
return ds;
}
protected void bt_find_Click(object sender, EventArgs e)
{
tb_er.Text = findExistingRebate().ToString();
}
public double findExistingRebate()
{
dsVehicleRb ds = useDataset();
//find and return
double existingRebate = 0.0;
for (int i = 0; i < ds.vehicleRb.Count; i++)
{
if (vehicle == ds.vehicleRb.Rows[i][0].ToString())
{
existingRebate = (double)ds.vehicleRb.Rows[i][1];
break;
}
}
return existingRebate;
//another way to find
// var q = ds.vehicleRb.Where(x => x.vehicleNum == vehicle).ToList();
////double stingRb= q.Select(y => y.existingRebates);
//GridView2.DataSource = q;
//GridView2.DataBind();
}
}
Picture of webForm1
WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebForm1.aspx.cs" Inherits="WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Parking Rebate system"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text="Vehicle number:"></asp:Label>
<asp:TextBox ID="tb_vn" runat="server"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label3" runat="server" Text="Parking Rebate system"></asp:Label>
<br />
<asp:Label runat="server" Text="Receipt s/n : "></asp:Label>
<asp:TextBox ID="tb_receipt" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label5" runat="server" Text="Shop name : "></asp:Label>
<asp:TextBox ID="tb_sn" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label6" runat="server" Text="Amount : "></asp:Label>
<asp:TextBox ID="tb_amount" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btn_add" runat="server" OnClick="add_btn_Click" Text="Add Receipt" />
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<br />
<br />
<asp:Button ID="btn_apply" runat="server" Text="Apply Rebates" OnClick="btn_apply_Click" />
<br />
<asp:Label runat="server" Text="Rebate Tickets : " ID="Label7"></asp:Label>
<asp:TextBox ID="tb_rt" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="bt_find" runat="server" OnClick="bt_find_Click" Text="Find Exsiting Rebates" />
<br />
<asp:Label runat="server" Text="Existing Rebates : " ID="Label8"></asp:Label>
<asp:TextBox ID="tb_er" runat="server"></asp:TextBox>
<br />
</div>
</form>
</body>
</html>