-
Notifications
You must be signed in to change notification settings - Fork 0
/
Methods.cs
600 lines (507 loc) · 19.3 KB
/
Methods.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Web.UI;
using StringHelpers;
using BooleanHelpers;
namespace BusinessRules
{
public class Methods
{
/// <summary>
/// CorrectDBNull - Convert null value to string
/// </summary>
/// <param name="oValue">Object</param>
/// <returns>String</returns>
public static string CorrectDBNull(object oValue)
{
if (Convert.IsDBNull(oValue))
{
return "";
}
else
{
return oValue.ToString() ;
}
}
/// <summary>
/// SendMail - send mail by using input given
/// </summary>
/// <param name="fromAddress">string</param>
/// <param name="toAddress">string</param>
/// <param name="sSubject">string</param>
/// <param name="sMessage">string</param>
/// <param name="bHTML">bool</param>
/// <returns>bool</returns>
public static bool SendMail(string fromAddress, string toAddress, string sSubject, string sMessage, bool bHTML)
{
MailMessage objMessage = new MailMessage();
SmtpClient SmtpMail = new SmtpClient();
try
{
objMessage.From = new MailAddress(fromAddress);
objMessage.To.Add(new MailAddress(toAddress));
objMessage.Subject = sSubject;
objMessage.Body = sMessage;
if (bHTML == true)
{
objMessage.IsBodyHtml = true;
}
else
{
objMessage.IsBodyHtml = false;
}
SmtpMail.Host = ((System.Collections.Specialized.NameValueCollection)System.Configuration.ConfigurationSettings.GetConfig("appSettings"))["SmtpServer"].ToString();
SmtpMail.Send(objMessage);
}
catch
{
return false;
}
return true;
}
public static string doInsertable(string sValue)
{
string str = "";
if (Convert.IsDBNull(sValue))
{
return str;
}
if (sValue != null)
{
str = sValue.Replace("'", "''").Trim() ;
}
return str;
}
/// <summary>
/// doInt - Replace NULL to Integer value
/// </summary>
/// <param name="oValue">Object</param>
/// <returns>Integer</returns>
public static int doInt(object oValue)
{
int functionReturnValue = 0;
if ((oValue != null) && oValue != DBNull.Value)
{
if (oValue.ToString() != "")
{
if (IsNumeric(oValue.ToString()))
{
functionReturnValue = Convert.ToInt32(oValue);
}
else
{
functionReturnValue = 0;
}
}
else
{
functionReturnValue = 0;
}
}
return functionReturnValue;
}
/// <summary>
/// doLong - Replace NULL to Long value
/// </summary>
/// <param name="oValue">Object</param>
/// <returns>Long</returns>
public static long doLong(object oValue)
{
long functionReturnValue = 0;
if ((oValue != null) && oValue != DBNull.Value)
{
if (oValue.ToString() != "")
{
if (IsNumeric(oValue.ToString()))
{
functionReturnValue = Convert.ToInt64(oValue);
}
else
{
functionReturnValue = 0;
}
}
else
{
functionReturnValue = 0;
}
}
return functionReturnValue;
}
/// <summary>
/// IsNumeric - check whether the value is numeric or not
/// </summary>
/// <param name="theValue">string</param>
/// <returns>bool</returns>
public static bool IsNumeric(string theValue)
{
Regex _isNumber = new Regex(@"^\d+$");
Match m = _isNumber.Match(theValue);
return m.Success;
}
/// <summary>
/// IsEmail - check whther the value passed is email or not
/// </summary>
/// <param name="theValue">string</param>
/// <returns>bool</returns>
public static bool IsEmail(string theValue)
{
Regex _IsEmail = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
Match m = _IsEmail.Match(theValue);
return m.Success;
}
/// <summary>
/// IsDouble - check whther the value passed is double or not
/// </summary>
/// <param name="theValue">string</param>
/// <returns>bool</returns>
public static bool IsDouble(string theValue)
{
Regex _IsDouble = new Regex(@"\d[\d\,\.]*");
Match m = _IsDouble.Match(theValue);
return m.Success;
}
/// <summary>
/// IsPhone - check whther the value passed is email or not
/// </summary>
/// <param name="theValue">string</param>
/// <returns>bool</returns>
public static bool IsPhone(string theValue)
{
Regex _IsPhone = new Regex(@"^((\(?0\d{4}\)?\s?\d{3}\s?\d{3})|(\(?0\d{3}\)?\s?\d{3}\s?\d{4})|(\(?0\d{2}\)?\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$");
Match m = _IsPhone.Match(theValue);
return m.Success;
}
/// <summary>
/// doBoolean - Replace NULL to boolean value
/// </summary>
/// <param name="oValue">Object</param>
/// <returns>boolean</returns>
public static bool doBoolean(object oValue)
{
bool functionReturnValue = false;
if ((oValue != null) && (oValue != DBNull.Value))
{
if ((oValue.ToString() != "") && (Convert.ToBoolean(oValue) == true))
{
functionReturnValue = true;
}
else
{
functionReturnValue = false;
}
}
return functionReturnValue;
}
/// <summary>
/// doDouble - Replace NULL to Double value
/// </summary>
/// <param name="oValue">Object</param>
/// <returns>Double</returns>
public static double doDouble(object oValue)
{
double functionReturnValue = 0;
if ((oValue != null) && (oValue != DBNull.Value))
{
if (oValue.ToString() != "")
{
if (IsDouble(oValue.ToString()))
{
functionReturnValue = Convert.ToDouble(oValue);
}
else
{
functionReturnValue = 0;
}
}
else
{
functionReturnValue = 0;
}
}
return functionReturnValue;
}
/// <summary>
/// DayCount - number of days between two days
/// </summary>
/// <param name="dFirstdate">DateTime</param>
/// <param name="dLastDate">DateTime</param>
/// <returns>Integer</returns>
public static int DayCount(DateTime dFirstdate, DateTime dLastDate)
{
int nDays;
TimeSpan ts = new TimeSpan();
ts = dFirstdate - dLastDate;
nDays = ts.Days;
return nDays;
}
/// <summary>
/// IsMobileNo - It checks whther the given input string is valid Mobile number or not
/// </summary>
/// <param name="theValue">string</param>
/// <returns>bool</returns>
public static bool IsMobileNo(string theValue)
{
Regex _IsMobileNo = new Regex(@"^((\(?0\d{4}\)?\s?\d{3}\s?\d{3})|(\(?0\d{3}\)?\s?\d{3}\s?\d{4})|(\(?0\d{2}\)?\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$");
Match m = _IsMobileNo.Match(theValue);
return m.Success;
}
/// <summary>
/// IsPostalCode - It checks whther the given input string is valid postal code or not
/// </summary>
/// <param name="theValue">string</param>
/// <returns>bool</returns>
public static bool IsPostalCode(string theValue)
{
Regex _IsPostalCode = new Regex(@"^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$");
Match m = _IsPostalCode.Match(theValue);
return m.Success;
}
//============================================================================
//Method Name : GetFileExt
//Description : get ext of file
//Parameter : 1. sFileName (String)
//Return : String
//============================================================================
public static string GetFileExt(string sFileName)
{
return System.IO.Path.GetExtension(sFileName);
}
//============================================================================
//Method Name : Base64Encode
//Description : Encode string value
//Parameter : 1. sValue (String)
//Return : String
//============================================================================
public static string Base64Encode(string sValue)
{
try
{
byte[] strBytes = System.Text.Encoding.UTF8.GetBytes(sValue);
return System.Convert.ToBase64String(strBytes);
}
catch (Exception ex)
{
return sValue;
}
}
//============================================================================
//Method Name : Base64Decode
//Description : Decode string value
//Parameter : 1. sValue (String)
//Return : String
//============================================================================
public static string Base64Decode(string sValue)
{
try
{
byte[] strBytes = System.Convert.FromBase64String(sValue);
return System.Text.Encoding.UTF8.GetString(strBytes);
}
catch (Exception ex)
{
return sValue;
}
}
public static Bitmap GetThumbnail(ref System.Drawing.Bitmap source, int maxWidth, int maxHeight)
{
//Dim iHeight, iWidth As Integer
//iHeight = maxHeight
//iWidth = maxWidth
//Dim dest As Bitmap = New Bitmap(source, iWidth, iHeight)
//Return dest
int iHeight = 0;
int iWidth = 0;
iHeight = source.Height;
iWidth = source.Width;
if (source.Width > maxWidth)
{
if (source.Width > source.Height)
{
iWidth = maxWidth;
iHeight = source.Height * maxWidth / source.Width;
}
else
{
iHeight = maxHeight;
iWidth = source.Width * maxHeight / source.Height;
}
}
else if (source.Height > maxHeight)
{
if (source.Width > source.Height)
{
iWidth = maxWidth;
iHeight = source.Height * maxWidth / source.Width;
}
else
{
iHeight = maxHeight;
iWidth = source.Width * maxHeight / source.Height;
}
}
Bitmap dest = new Bitmap(source, iWidth, iHeight);
return dest;
}
//============================================================================
//Method Name : FillYearDropDown
//Description : Fill year data
//Parameter : 1. DropDown (DropDownList)
//Return : ---
//============================================================================
public static void FillYearDropDown(ref DropDownList DropDown)
{
try
{
for (int intI = 1900; intI <= System.DateTime.Now.Year; intI++)
{
ListItem lstItem = new ListItem(intI.ToString (), intI.ToString ());
DropDown.Items.Add(lstItem);
}
DropDown.Items.Insert(0, new ListItem("Year", "0"));
}
catch (Exception ex)
{
}
}
//============================================================================
//Method Name : FillMonthDropDown
//Description : Fill month data
//Parameter : 1. DropDown (DropDownList)
//Return : ---
//============================================================================
public static void FillMonthDropDown(ref DropDownList DropDown)
{
try
{
string[] sArrMonth = { "Jan", "Fab", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
"Nov", "Dec" };
int intI = 1;
foreach (string strI in sArrMonth)
{
ListItem lstItem = new ListItem(strI.ToString (), intI.ToString());
DropDown.Items.Add(lstItem);
intI += 1;
}
DropDown.Items.Insert(0, new ListItem("Month", "0"));
}
catch (Exception ex)
{
}
}
//============================================================================
//Method Name : FillDayDropDown
//Description : Fill day data
//Parameter : 1. DropDown (DropDownList)
//Return : ---
//============================================================================
public static void FillDayDropDown(ref DropDownList DropDown)
{
try
{
int intI;
for (intI = 1; intI <= 31; intI++)
{
ListItem lstItem = new ListItem(intI.ToString (), intI.ToString ());
DropDown.Items.Add(lstItem);
}
DropDown.Items.Insert(0, new ListItem("Day","1" ));
}
catch (Exception ex)
{
throw ex;
}
}
public static void disableEnterKey_TextBox(TextBox textControl)
{
textControl.Attributes.Add("onKeyPress", "return disableEnterKey(event)");
}
public static void disableEnterKey_Dropdown(DropDownList drpControl)
{
drpControl.Attributes.Add("onKeyPress", "return disableEnterKey(event)");
}
public static void SetDefaultButton(Page page, TextBox textControl, Button defaultButton)
{
// Sets default buttons.
string theScript = "<SCRIPT language=\"javascript\">" + "function fnTrapKD(btn, event){ if (document.all){ if (event.keyCode == 13){event.returnValue=false; event.cancel = true; btn.click();" + "}} else if (document.getElementById){ if (event.which == 13){event.returnValue=false; event.cancel = true; btn.click();}}" + "else if(document.layers){if(event.which == 13){event.returnValue=false;event.cancel = true;btn.click();}}}</SCRIPT>";
page.ClientScript.RegisterStartupScript(textControl.GetType(), "ForceDefaultToScript", theScript);
textControl.Attributes.Add("onkeydown", "fnTrapKD(" + defaultButton.ClientID + ", event)");
}
public static DropDownList DrpSelect_N(int SelVal, DropDownList drpObj)
{
drpObj.ClearSelection();
int i = 0;
for (i = 0; i <= drpObj.Items.Count - 1; i++)
{
if (Methods.doInt(drpObj.Items[i].Value) == SelVal)
{
drpObj.SelectedIndex = i;
break; // TODO: might not be correct. Was : Exit For
}
}
return drpObj;
}
public static DropDownList DrpSelect_S(string SelVal, DropDownList drpObj)
{
drpObj.ClearSelection();
int i = 0;
for (i = 0; i <= drpObj.Items.Count - 1; i++)
{
if (Methods.CorrectDBNull(drpObj.Items[i].Value) == SelVal)
{
drpObj.SelectedIndex = i;
break; // TODO: might not be correct. Was : Exit For
}
}
return drpObj;
}
public static string MakeURLClickable(string sURL)
{
string sFullURL = "";
if (!string.IsNullOrEmpty(sURL.Trim()))
{
sURL = sURL.Replace("http://", "");
//' remove all extra '/'
int slashIndex = 0;
if (!string.IsNullOrEmpty(sURL.Trim()))
{
while (sURL.Length - 1 == sURL.LastIndexOf("/"))
{
slashIndex = sURL.LastIndexOf("/");
sURL = sURL.Substring(0, slashIndex);
}
}
//'
sFullURL = "http://" + sURL;
}
return sFullURL;
}
public static bool ValidateEmail(string inputEmail, char Type)
{
string strRegex = null;
//strRegex = "^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" & "\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" & ".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"
if (Type == 'B')
{
strRegex = "<(?:[\\w\\!\\#\\$\\%\\&\\'\\*\\+\\-\\/\\=\\?\\^\\`\\{\\|\\}\\~]+\\.)*[\\w\\!\\#\\$\\%\\&\\'\\*\\+\\-\\/\\=\\?\\^\\`\\{\\|\\}\\~]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9\\-](?!\\.)){0,61}[a-zA-Z0-9]?\\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\\-](?!$)){0,61}[a-zA-Z0-9]?)|(?:\\[(?:(?:[01]?\\d{1,2}|2[0-4]\\d|25[0-5])\\.){3}(?:[01]?\\d{1,2}|2[0-4]\\d|25[0-5])\\]))>";
}
else
{
strRegex = "^(?:[\\w\\!\\#\\$\\%\\&\\'\\*\\+\\-\\/\\=\\?\\^\\`\\{\\|\\}\\~]+\\.)*[\\w\\!\\#\\$\\%\\&\\'\\*\\+\\-\\/\\=\\?\\^\\`\\{\\|\\}\\~]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9\\-](?!\\.)){0,61}[a-zA-Z0-9]?\\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\\-](?!$)){0,61}[a-zA-Z0-9]?)|(?:\\[(?:(?:[01]?\\d{1,2}|2[0-4]\\d|25[0-5])\\.){3}(?:[01]?\\d{1,2}|2[0-4]\\d|25[0-5])\\]))$";
}
if (Regex.IsMatch(inputEmail, strRegex))
{
return (true);
}
else
{
return (false);
}
}
}
}