-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.aspx.cs
396 lines (319 loc) · 18.3 KB
/
search.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace authwebpart
{
public partial class search : System.Web.UI.Page
{
public static string cons = WebConfigurationManager.ConnectionStrings["v0scon"].ConnectionString;
SqlConnection scon = new SqlConnection(cons);
protected void Page_Load(object sender, EventArgs e)
{
if (Session["suser"] == null)
{
Response.Redirect("login.aspx");
}
GridView1.Visible = false;
GridView2.Visible = false;
GridView3.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)//search button
{
bool found = false;
if(TextBox1.Text!=null && TextBox1!=null)
{
using (scon)
{
scon.Open();
//check for numeric or alphabat
long i = 0;
string findnum = TextBox1.Text.ToString();
bool result = long.TryParse(findnum, out i);
if (result)//for numeric
{
if (findnum.Length == 10)
{
string mobquery = "select * FROM customer where mobile_no=@mobile_no";
SqlCommand ccmd = new SqlCommand(mobquery, scon);
ccmd.Parameters.AddWithValue("@mobile_no", i);
SqlDataReader reader = ccmd.ExecuteReader();
if (reader.HasRows)
{
searchnotID.Text="Search BY Mobile No:";
GridView1.Enabled = true;
GridView1.DataSource = reader;
GridView1.DataBind();
GridView1.Visible = true;
}
else
{
searchnotID.Text="No Search Result Found For Mobile No: "+i;
searchnotID.Visible = true;
}
reader.Close();
}
else
{
string query = "select * FROM customer where c_id=@c_id ORDER BY create_date DESC";
SqlCommand ccmd = new SqlCommand(query, scon);
ccmd.Parameters.AddWithValue("@c_id", i);
SqlDataReader reader = ccmd.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
Response.Redirect("~/view.aspx?c_id=" + reader["c_id"]);
}
else
{
searchnotID.Text="No Search Result Found For: "+i;
searchnotID.Visible = true;
}
reader.Close();
}
}
else//for Alphanumeric
{
//check for city
string ctquery = "select * FROM customer where city=@city ORDER BY create_date DESC";
SqlCommand ctcmd = new SqlCommand(ctquery, scon);
ctcmd.Parameters.AddWithValue("@city", findnum);
SqlDataReader reader2 = ctcmd.ExecuteReader();
if (reader2.HasRows)
{
searchnotID.Text="Search By City:";
searchnotID.Visible = true;
found = true;
GridView2.DataSource = reader2;
GridView2.DataBind();
GridView2.Enabled = true;
GridView2.Visible = true;
}
else
{
searchnotID.Text= "No Search Result Found For: "+findnum;// in city checking for name....."
searchnotID.Visible = true;
}
reader2.Close();
if (!found) /*check per NAME*/
{
// string nmquery = "select * FROM customer where (c_mname like "+"'%'"+"@c_mname"+"'%'"+" OR c_fname"+" like "+"'%'"+"@c_fname"+"'%'"+ " OR c_lname like"+"'%'"+"@c_lname"+"'%'"+")";
// string nmquery = "select * FROM customer where c_mname like " + "'%'" + "@c_mname" + "'%'";
string nmquery = " SELECT * FROM customer WHERE(c_fname LIKE '%' + @c_fname + '%' OR c_mname LIKE '%' + @c_mname + '%' OR c_lname LIKE '%' + @c_lname + '%') ORDER BY create_date DESC";
SqlCommand nmcmd = new SqlCommand(nmquery, scon);
findnum=findnum.Trim();//trim enter string
string[] sfind = findnum.Split(null);
if (sfind.Length == 3 && sfind.Length <= 3)//for three words
{
// Response.Write("checking for split condition-3");
nmcmd.Parameters.AddWithValue("@c_fname", sfind[0]);
nmcmd.Parameters.AddWithValue("@c_mname", sfind[1]);
nmcmd.Parameters.AddWithValue("@c_lname", sfind[2]);
SqlDataReader reader1 = nmcmd.ExecuteReader();
if (reader1.HasRows)
{
searchnotID.Text= "Search By Name:";
searchnotID.Visible = true;
found = true;
reader1.Close();
string addgq = " SELECT * FROM customer WHERE(c_fname LIKE '%' + @c_fname + '%' AND c_mname LIKE '%' + @c_mname + '%' AND c_lname LIKE '%' + @c_lname + '%') ORDER BY create_date DESC";
SqlCommand sqc = new SqlCommand(addgq, scon);
sqc.Parameters.AddWithValue("@c_fname", sfind[0]);
sqc.Parameters.AddWithValue("@c_mname", sfind[1]);
sqc.Parameters.AddWithValue("@c_lname", sfind[2]);
GridView3.DataSource = sqc.ExecuteReader();
GridView3.DataBind();
GridView3.Enabled = true;
GridView3.Visible = true;
}
else
{
searchnotID.Text = "No Search Result Found For: " + findnum;
searchnotID.Visible = true;
//Response.Write("\nrecord not found in name and city both ... split condition1");
reader1.Close();
}
}
else if (sfind.Length == 2)
{
nmquery = " SELECT * FROM customer WHERE(" +
"(c_fname LIKE '%' + @c_fname1+ '%' AND c_mname LIKE '%' + @c_mname2 + '%') OR" +
"(c_fname LIKE '%' + @c_fname2+ '%' AND c_mname LIKE '%' + @c_mname1 + '%') OR" +
" (c_fname LIKE '%' + @c_fname1+ '%' AND c_lname LIKE '%' + @c_lname2 + '%') OR" +
" (c_fname LIKE '%' + @c_fname2+ '%' AND c_lname LIKE '%' + @c_lname1 + '%') OR" +
" (c_mname LIKE '%' + @c_mname1+ '%' AND c_lname LIKE '%' + @c_lname2 + '%') OR" +
" (c_mname LIKE '%' + @c_mname2+ '%' AND c_lname LIKE '%' + @c_lname1 + '%')) ORDER BY create_date DESC";
nmcmd = new SqlCommand(nmquery, scon);
// Response.Write("checking for split condition-2");
nmcmd.Parameters.AddWithValue("@c_fname1", sfind[0]);
nmcmd.Parameters.AddWithValue("@c_fname2", sfind[1]);
nmcmd.Parameters.AddWithValue("@c_mname1", sfind[0]);
nmcmd.Parameters.AddWithValue("@c_mname2", sfind[1]);
nmcmd.Parameters.AddWithValue("@c_lname1", sfind[0]);
nmcmd.Parameters.AddWithValue("@c_lname2", sfind[1]);
SqlDataReader reader1 = nmcmd.ExecuteReader();
if (reader1.HasRows)
{
searchnotID.Text = "Search By Name:";
searchnotID.Visible = true;
found = true;
GridView3.DataSource = reader1;
GridView3.DataBind();
GridView3.Enabled = true;
GridView3.Visible = true;
reader1.Close();
}
else
{
//Response.Write("\nrecord not found in name and city both ...split condition2");
searchnotID.Text = "No Search Result Found For: "+findnum;
searchnotID.Visible = true;
reader1.Close();
}
}
else if (sfind.Length == 1)
{
// Response.Write("checking for split condition-1");
nmcmd.Parameters.AddWithValue("@c_fname", sfind[0]);
nmcmd.Parameters.AddWithValue("@c_mname", sfind[0]);
nmcmd.Parameters.AddWithValue("@c_lname", sfind[0]);
SqlDataReader reader1 = nmcmd.ExecuteReader();
if (reader1.HasRows)
{
searchnotID.Text = "Search By Name:";
searchnotID.Visible = true;
found = true;
GridView3.DataSource = reader1;
GridView3.DataBind();
GridView3.Enabled = true;
GridView3.Visible = true;
reader1.Close();
}
else
{
searchnotID.Text = "No Search Result Found For: " + findnum; ;
searchnotID.Visible = true;
//Response.Write("\nrecord not found in name and city both ...split condition2");
reader1.Close();
}
}
else
{
// Response.Write("input search error ----attempt to search record not present in db");
searchnotID.Text = "No Search Result Found For: " + findnum; ;
searchnotID.Visible = true;
}
}
if(!found)
{
// Response.Write("\nnot found");
searchnotID.Text = "No Search Result Found For: " + findnum;
searchnotID.Visible = true;
}
}
}
}
else
{
searchnotID.Text = "Empty Search Field...";
searchnotID.Visible = true;
}
}
//reditected to view of customer for each gridview
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Redirect("~/view.aspx?c_id=" + ((LinkButton)sender).Text);
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
Response.Redirect("~/view.aspx?c_id=" + ((LinkButton)sender).Text);
}
protected void LinkButton3_Click(object sender, EventArgs e)
{
Response.Redirect("~/view.aspx?c_id=" + ((LinkButton)sender).Text);
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)//mobile no
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
using (SqlConnection scon = new SqlConnection(cons))
{
scon.Open();
int rowcid = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "c_id"));
// Response.Write("hear idd"+rowcid);
string tdquery = "SELECT SUM(t_amount) FROM transection where (c_id=@c_id AND t_type=@t_type) GROUP BY c_id ";
SqlCommand tdmycmd = new SqlCommand(tdquery, scon);
tdmycmd.Parameters.AddWithValue("@c_id", rowcid);
tdmycmd.Parameters.AddWithValue("@t_type", "d");
object totald = tdmycmd.ExecuteScalar();
string ss = Convert.ToString(totald);
string tcquery = "SELECT SUM(t_amount) FROM transection where (c_id=@c_id AND t_type=@t_type) GROUP BY c_id ";
SqlCommand tcmycmd = new SqlCommand(tcquery, scon);
tcmycmd.Parameters.AddWithValue("@c_id", rowcid);
tcmycmd.Parameters.AddWithValue("@t_type", "c");
object totalc = tcmycmd.ExecuteScalar();
string ss1 = Convert.ToString(totalc);
e.Row.Cells[15].Text = ss;
e.Row.Cells[14].Text = ss1;
}
}
}
protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)//name
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
using (SqlConnection scon = new SqlConnection(cons))
{
scon.Open();
int rowcid = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "c_id"));
// Response.Write("hear idd"+rowcid);
string tdquery = "SELECT SUM(t_amount) FROM transection where (c_id=@c_id AND t_type=@t_type) GROUP BY c_id ";
SqlCommand tdmycmd = new SqlCommand(tdquery, scon);
tdmycmd.Parameters.AddWithValue("@c_id", rowcid);
tdmycmd.Parameters.AddWithValue("@t_type", "d");
object totald = tdmycmd.ExecuteScalar();
string ss = Convert.ToString(totald);
string tcquery = "SELECT SUM(t_amount) FROM transection where (c_id=@c_id AND t_type=@t_type) GROUP BY c_id ";
SqlCommand tcmycmd = new SqlCommand(tcquery, scon);
tcmycmd.Parameters.AddWithValue("@c_id", rowcid);
tcmycmd.Parameters.AddWithValue("@t_type", "c");
object totalc = tcmycmd.ExecuteScalar();
string ss1 = Convert.ToString(totalc);
e.Row.Cells[15].Text = ss;
e.Row.Cells[14].Text = ss1;
}
}
}
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)//city
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
using (SqlConnection scon = new SqlConnection(cons))
{
scon.Open();
int rowcid = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "c_id"));
// Response.Write("hear idd"+rowcid);
string tdquery = "SELECT SUM(t_amount) FROM transection where (c_id=@c_id AND t_type=@t_type) GROUP BY c_id ";
SqlCommand tdmycmd = new SqlCommand(tdquery, scon);
tdmycmd.Parameters.AddWithValue("@c_id", rowcid);
tdmycmd.Parameters.AddWithValue("@t_type", "d");
object totald = tdmycmd.ExecuteScalar();
string ss = Convert.ToString(totald);
string tcquery = "SELECT SUM(t_amount) FROM transection where (c_id=@c_id AND t_type=@t_type) GROUP BY c_id ";
SqlCommand tcmycmd = new SqlCommand(tcquery, scon);
tcmycmd.Parameters.AddWithValue("@c_id", rowcid);
tcmycmd.Parameters.AddWithValue("@t_type", "c");
object totalc = tcmycmd.ExecuteScalar();
string ss1 = Convert.ToString(totalc);
e.Row.Cells[15].Text = ss;
e.Row.Cells[14].Text = ss1;
}
}
}
}
}