-
Notifications
You must be signed in to change notification settings - Fork 0
/
Checkout.aspx.cs
142 lines (107 loc) · 4.24 KB
/
Checkout.aspx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
namespace Ecommerce_Smartdeal
{
public partial class Checkout : System.Web.UI.Page
{
String mycon = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
string selectData, Output = "";
Double cartTotal= 0.00;
Double discountPercentage = 0.00;
bool Exists = false;
SqlConnection con1 = new SqlConnection(mycon);
SqlCommand cmd = null;
SqlDataReader dataReader = null;
try
{
selectData = "Select sum(quantity * price) from shopping_cart where session_id = '" + Session["id"] + "'";
con1.Open();
cmd= new SqlCommand(selectData,con1);
dataReader = cmd.ExecuteReader();
cmd.CommandText = selectData;
while(dataReader.Read())
{
Output = Output + dataReader.GetValue(0) + "\n";
}
Exists = String.IsNullOrEmpty(Output);
cartTotal = int.Parse(Output);
if(RdoOffers.SelectedIndex == 0)
{
discountPercentage = 0.10;
}
else if(RdoOffers.SelectedIndex == 1)
{
discountPercentage = 0.03;
}
else if(RdoOffers.SelectedIndex == 2 && cartTotal >= 1000)
{
discountPercentage = 0.05;
}
total.Text = cartTotal.ToString();
LblOff.Text = (cartTotal - (cartTotal * discountPercentage)).ToString();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
dataReader.Close();
cmd.Dispose();
con1.Close();
}
if (Session["id"] != null)
{
if (!Exists)
{
btn_placeorder.Visible = true;
total.Visible = true;
LblOff.Visible = true;
}
else
{
btn_placeorder.Visible = false;
total.Visible = false;
LblOff.Visible = false;
}
}
}
protected void Btn_placeorder_Click(object sender, EventArgs e)
{
SqlConnection con1 = new SqlConnection(mycon);
SqlCommand cmd = new SqlCommand();
try
{
string deleteData = "delete from shopping_cart where session_id = '" + Session["id"] + "'";
string insertData = "Insert into order_details(user_name,product_id,product_name,description,quantity,price) SELECT session_id,product_id,product_name,description,quantity,price FROM shopping_cart where session_id = '" + Session["id"] + "'";
con1.Open();
//delete the shopping cart
cmd.CommandText = insertData;
cmd.Connection = con1;
cmd.ExecuteNonQuery();
//Inserted to Order tables
cmd.CommandText = deleteData;
cmd.Connection = con1;
cmd.ExecuteNonQuery();
Response.Redirect("OrderSuccess.aspx");
}
catch(Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
cmd.Dispose();
con1.Close();
}
}
}
}