-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDistricts.aspx.cs
172 lines (157 loc) · 6.63 KB
/
Districts.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
namespace DisconnectedExample
{
public partial class Districts : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
PanelSearch.Visible = false;
if (Session["id"] == null)
Response.Redirect("~\\Index.aspx");
}
protected void LoadFromCacheDistricts()
{
if (Cache["DS"] == null)
this.LoadFromDb();
DataSet ds = (DataSet)Cache["DS"];
GridViewDistricts.DataSource = ds.Tables["Districts"];
GridViewDistricts.DataBind();
}
protected void LoadFromDb()
{
string query1 = "select * from Districts";
string query2 = "select * from Routes";
string connString = ConfigurationManager.ConnectionStrings["DBPath"].ConnectionString;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = connString;
conn.Open();
SqlDataAdapter districtAdapter = new SqlDataAdapter(query1, conn);
DataSet ds = new DataSet();
districtAdapter.Fill(ds, "Districts");
SqlCommandBuilder builder = new SqlCommandBuilder(districtAdapter);
conn.Close();
conn.Open();
SqlDataAdapter routeAdapter = new SqlDataAdapter(query2, conn);
routeAdapter.Fill(ds, "Routes");
builder = new SqlCommandBuilder(routeAdapter);
ds.Tables["Districts"].PrimaryKey = new DataColumn[] { ds.Tables["Districts"].Columns["DistrictId"] };
ds.Tables["Routes"].PrimaryKey = new DataColumn[] { ds.Tables["Routes"].Columns["RouteId"] };
ds.Tables["Routes"].Columns["RouteId"].AutoIncrement = true;
ds.Tables["Districts"].Columns["DistrictId"].AutoIncrement = true;
ds.Tables["Districts"].Columns["DistrictName"].Unique = true;
Cache["DS"] = ds;
Cache["DistrictAdapter"] = districtAdapter;
Cache["RouteAdapter"] = routeAdapter;
}
protected void GridViewDistricts_RowEditing(object sender, GridViewEditEventArgs e)
{
GridViewDistricts.EditIndex = e.NewEditIndex;
if (Convert.ToInt32(Session["flag"]) == 1)
this.LoadFromCacheDistricts();
else
this.LoadByPattern();
}
protected void GridViewDistricts_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
if (Cache["DS"] == null)
this.LoadFromDb();
DataSet ds = (DataSet)Cache["DS"];
DataRow rowToUpdate = ds.Tables["Districts"].Rows.Find(e.Keys["DistrictId"]);
rowToUpdate["DistrictName"] = e.NewValues["DistrictName"];
Cache["DS"] = ds;
//ds.AcceptChanges();
SqlDataAdapter adapter = (SqlDataAdapter)Cache["DistrictAdapter"];
adapter.Update(ds.Tables["Districts"]);
GridViewDistricts.EditIndex = -1;
if (Convert.ToInt32(Session["flag"]) == 1)
this.LoadFromCacheDistricts();
else
this.LoadByPattern();
}
protected void GridViewDistricts_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridViewDistricts.EditIndex = -1;
if (Convert.ToInt32(Session["flag"]) == 1)
this.LoadFromCacheDistricts();
else
this.LoadByPattern();
}
protected void GridViewDistricts_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
if (Cache["DS"] == null)
this.LoadFromDb();
DataSet ds = (DataSet)Cache["DS"];
DataRow rowToDelete = ds.Tables["Districts"].Rows.Find(e.Keys["DistrictId"]);
rowToDelete.Delete();
Cache["DS"] = ds;
//ds.AcceptChanges();
SqlDataAdapter adapter = (SqlDataAdapter)Cache["DistrictAdapter"];
adapter.Update(ds.Tables["Districts"]);
if (Convert.ToInt32(Session["flag"]) == 1)
this.LoadFromCacheDistricts();
else
this.LoadByPattern();
}
protected void btnAddDistrict_Click(object sender, EventArgs e)
{
PanelSearch.Visible = false;
//validate txtDistrict
if (txtDistrict.Text.Trim().Length > 0)
{
if (Cache["DS"] == null)
this.LoadFromDb();
DataSet ds = (DataSet)Cache["DS"];
DataRow dr = ds.Tables["Districts"].NewRow();
dr["DistrictName"] = txtDistrict.Text;
dr["DistrictId"] = 0;
ds.Tables["Districts"].Rows.Add(dr);
Cache["DS"] = ds;
//ds.AcceptChanges();
SqlDataAdapter adapter = (SqlDataAdapter)Cache["DistrictAdapter"];
adapter.Update(ds.Tables["Districts"]);
this.LoadFromCacheDistricts();
this.LoadFromDb();
}
else
{
lblerrormsg.Text = " * District Can not be Empty!";
}
}
protected void btnLoadDistricts_Click(object sender, EventArgs e)
{
PanelSearch.Visible = false;
Session["flag"] = 1;
this.LoadFromCacheDistricts();
}
protected void btnGoback_Click(object sender, EventArgs e)
{
Response.Redirect("~\\Admin.aspx");
}
protected void btnGO_Click(object sender, EventArgs e)
{
Session["flag"] = 2;
this.LoadByPattern();
}
protected void LoadByPattern()
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["DBPath"].ConnectionString;
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = string.Format("select * from Districts where DistrictName like '%{0}%'", txtSearch.Text);
cmd.Connection = conn;
SqlDataReader reader = cmd.ExecuteReader();
GridViewDistricts.DataSource = reader;
GridViewDistricts.DataBind();
}
protected void btnSearch_Click(object sender, EventArgs e)
{
PanelSearch.Visible = !PanelSearch.Visible;
}
}
}