-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAvailable.aspx.cs
151 lines (124 loc) · 5.76 KB
/
Available.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
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Drawing;
namespace HiralMehta_Project
{
public partial class WebForm2 : System.Web.UI.Page
{
string cs = ConfigurationManager.ConnectionStrings["ToyDB"].ConnectionString;
//On page load event, if page is not postback from server, dropdownlist will be populated with following data
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ddlType.Items.Add("Soft toy");
ddlType.Items.Add("Doll");
ddlType.Items.Add("Board game");
ddlType.Items.Add("Lego");
ddlType.Items.Add("Plastic toys");
ddlType.Items.Add("Automobile");
ddlType.Items.Add("VideoGame");
ListItem liSelect = new ListItem("Select a type", "-1");
ddlType.Items.Insert(0, liSelect);
}
}
/*Author of the below method is Hiral Mehta. It is used to submit data into toy database based on data input by advertiser. Some fields are compulsory while others are not, so those data will be inserted as NULL values in database*/
protected void btnSubmitAvail_Click(object sender, EventArgs e)
{
string selectQuery = "SELECT ClientName,ClientCity,ClientPhone,ClientEmail,ToyType,ToyDesc,AgeGroup,AvailabilityWeeks FROM ToyData;";
SqlConnection conn = new SqlConnection(cs);//Establish connection
SqlDataAdapter da = new SqlDataAdapter(selectQuery, conn);//Instantiating new data adapter
DataSet ds = new DataSet();
da.Fill(ds, "ToyData");//New dataset ds filled with ToyData table
DataTable tblToyData = ds.Tables["ToyData"];
DataRow newRow = tblToyData.NewRow();
newRow["ClientName"] = txtName.Text.Trim();
newRow["ClientCity"] = txtCity.Text.Trim().ToUpper();
newRow["ClientPhone"] = txtPhone.Text.Trim();
newRow["ClientEmail"] = txtEmail.Text.Trim();
newRow["ToyType"] = ddlType.Text;
newRow["ToyDesc"] = txtDesc.Text.Trim();
newRow["AgeGroup"] = rblAvailAgeGroup.Text;
newRow["AvailabilityWeeks"] = txtAvail.Text.Trim();
tblToyData.Rows.Add(newRow);//Insert a new row based on field values
SqlCommandBuilder builder = new SqlCommandBuilder(da);
int rowsInserted = da.Update(ds, "ToyData");
if (rowsInserted > 0)//If row will be inserted, a message will be populated.
{
lblAvailable.Text = "Toy successfully inserted to database";
lblAvailable.ForeColor = Color.Green;
}
else
{
lblAvailable.Text = "Toy could not be inserted to database, please try again!!";
lblAvailable.ForeColor = Color.Red;
}
//All fields will be set to blank after data insertion
txtName.Text = "";
txtCity.Text = "";
txtPhone.Text = "";
txtEmail.Text = "";
txtDesc.Text = "";
txtAvail.Text = "";
ddlType.SelectedIndex = -1;
}
/*Author of the below method is Hiral Mehta. It is used to search existing toys from toy database based on client email provided initially when advertising a toy*/
private bool searchToy()
{
string selectQueryDel =
"Select ClientName,ToyType,ToyDesc,AgeGroup,AvailabilityWeeks from ToyData where ClientEmail=@ClientEmail1;";
SqlConnection conn = new SqlConnection(cs);
SqlDataAdapter da1 = new SqlDataAdapter(selectQueryDel, conn);
da1.SelectCommand.Parameters.AddWithValue("@ClientEmail1", txtEmailDel.Text);
DataSet ds1 = new DataSet();
Cache["DataSet"] = ds1;
da1.Fill(ds1, "ToyData");
DataTable tblToy = ds1.Tables["ToyData"];
if (tblToy.Rows.Count > 0)
{
gvDel.DataSource = ds1;
gvDel.DataBind();
return true;
}
else
{
return false;
}
}
/*The below method uses search toydatabase method and if toy found, a lable will display the message*/
protected void btnLoad_Click(object sender, EventArgs e)
{
if (searchToy())
{
lblDel.Text = "Here are your toys in the database";
lblDel.ForeColor = System.Drawing.Color.DarkGreen;
}
else
{
lblDel.Text = "No toy found with matching your email";
lblDel.ForeColor = System.Drawing.Color.Red;
}
}
/*The below method uses search toydatabase method and if toy found, the method will delete that toy from database and display message*/
protected void btnDelete_Click(object sender, EventArgs e)
{
if (searchToy())
{
DataSet ds1 = (DataSet)Cache["DataSet"];
DataRow dr = ds1.Tables["ToyData"].Rows[0];
dr.Delete();
lblDel.Text = "Toy deleted";
lblDel.ForeColor = System.Drawing.Color.Red;
gvDel.Visible = false;
txtEmailDel.Text = "";
}
}
}
}