)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » Default (Ia.Cl.Models)

Public general use code classes and xml files that we've compiled and used over the years:

General use static class of common functions used by most applications.

    1: using Microsoft.AspNetCore.Http;
    2: using System;
    3: using System.Collections;
    4: using System.Collections.Generic;
    5: using System.ComponentModel;
    6: using System.Configuration;
    7: using System.Data;
    8: using System.Globalization;
    9: using System.IO;
   10: using System.Linq;
   11: using System.Net;
   12: using System.Net.Http;
   13: using System.Net.Sockets;
   14: using System.Reflection;
   15: using System.Text;
   16: using System.Text.RegularExpressions;
   17: using System.Threading.Tasks;
   18:  
   19: //#if WFA
   20: //#else
   21: using System.Xml;
   22: using System.Xml.Linq;
   23: using static System.Net.WebRequestMethods;
   24: //#endif
   25:  
   26: namespace Ia.Cl.Models
   27: {
   28:     ////////////////////////////////////////////////////////////////////////////
   29:  
   30:     /// <summary publish="true">
   31:     /// General use static class of common functions used by most applications.
   32:     /// 
   33:     /// </summary>
   34:     /// <remarks> 
   35:     /// Copyright © 2001-2019 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   36:     ///
   37:     /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
   38:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   39:     ///
   40:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   41:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   42:     /// 
   43:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   44:     /// 
   45:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   46:     /// </remarks> 
   47:     public static class Default
   48:     {
   49:         private static readonly HttpClient httpClient = new HttpClient();
   50:  
   51:         private static Random random = new Random();
   52:         // very important that you declare random here then use random.Next() elsewhere, otherwise you will have duplicate results
   53:  
   54:         ////////////////////////////////////////////////////////////////////////////
   55:  
   56:         /// <summary>
   57:         /// Splits CSV file lines
   58:         /// </summary>
   59:         public static IEnumerable<string> CsvLineSplitter(string line)
   60:         {
   61:             int fieldStart = 0;
   62:  
   63:             for (int i = 0; i < line.Length; i++)
   64:             {
   65:                 if (line[i] == ',')
   66:                 {
   67:                     yield return line.Substring(fieldStart, i - fieldStart);
   68:  
   69:                     fieldStart = i + 1;
   70:                 }
   71:  
   72:                 if (line[i] == '"') for (i++; line[i] != '"'; i++) { }
   73:             }
   74:         }
   75:  
   76:         ////////////////////////////////////////////////////////////////////////////
   77:  
   78:         /// <summary>
   79:         ///
   80:         /// </summary>
   81:         public static string Substring(string str, int len)
   82:         {
   83:             if (str.Length >= len - 3) str = str.Substring(0, len - 3) + "... ";
   84:             else if (str.Length > 0) str = str.Substring(0, str.Length);
   85:             else str = "";
   86:  
   87:             return str;
   88:         }
   89:  
   90:         ////////////////////////////////////////////////////////////////////////////
   91:  
   92:         /// <summary>
   93:         ///
   94:         /// </summary>
   95:         public static string Url(string text, string url)
   96:         {
   97:             return "<a href=" + url + ">" + text + "</a>";
   98:         }
   99:  
  100:         ////////////////////////////////////////////////////////////////////////////
  101:  
  102:         /// <summary>
  103:         ///
  104:         /// </summary>
  105:         public static string YesNo(bool checkValue)
  106:         {
  107:             string text;
  108:  
  109:             if (checkValue) text = "<span class=\"yes\">Yes</span>";
  110:             else text = "<span class=\"no\">No</span>";
  111:  
  112:             return text;
  113:         }
  114:  
  115:         ////////////////////////////////////////////////////////////////////////////
  116:  
  117:         /// <summary>
  118:         ///
  119:         /// </summary>
  120:         public static string YesNo(bool? checkValue)
  121:         {
  122:             string text;
  123:  
  124:             if (checkValue == null) text = "<span class=\"na\">NA</span>";
  125:             else if (checkValue == true) text = "<span class=\"yes\">Yes</span>";
  126:             else text = "<span class=\"no\">No</span>";
  127:  
  128:             return text;
  129:         }
  130:  
  131:         ////////////////////////////////////////////////////////////////////////////
  132:  
  133:         /// <summary>
  134:         ///
  135:         /// </summary>
  136:         public static string YesNoText(bool checkValue)
  137:         {
  138:             string text;
  139:  
  140:             if (checkValue) text = "Yes";
  141:             else text = "No";
  142:  
  143:             return text;
  144:         }
  145:  
  146:         ////////////////////////////////////////////////////////////////////////////
  147:  
  148:         /// <summary>
  149:         ///
  150:         /// </summary>
  151:         public static string YesNoInArabic(bool checkValue)
  152:         {
  153:             string text;
  154:  
  155:             if (checkValue) text = "<span class=\"yes\">نعم</span>";
  156:             else text = "<span class=\"no\">لا</span>";
  157:  
  158:             return text;
  159:         }
  160:  
  161:         ////////////////////////////////////////////////////////////////////////////
  162:  
  163:         /// <summary>
  164:         ///
  165:         /// </summary>
  166:         public static string YesNoInArabic(bool? checkValue)
  167:         {
  168:             string text;
  169:  
  170:             if (checkValue == null) text = "<span class=\"na\">لا ينطبق</span>";
  171:             else if (checkValue == true) text = "<span class=\"yes\">نعم</span>";
  172:             else text = "<span class=\"no\">لا</span>";
  173:  
  174:             return text;
  175:         }
  176:  
  177:         ////////////////////////////////////////////////////////////////////////////
  178:  
  179:         /// <summary>
  180:         ///
  181:         /// </summary>
  182:         public static string ActiveIdle(object o)
  183:         {
  184:             bool b;
  185:             string s;
  186:  
  187:             if (o != null && o.ToString().Length > 0)
  188:             {
  189:                 b = (bool)o;
  190:  
  191:                 if (b) s = "<span style=\"color:Green\">Active</span>";
  192:                 else s = "<span style=\"color:DarkGoldenRod\">Idle</span>";
  193:             }
  194:             else s = "";
  195:  
  196:             return s;
  197:         }
  198:  
  199:         ////////////////////////////////////////////////////////////////////////////
  200:  
  201:         /// <summary>
  202:         ///
  203:         /// </summary>
  204:         public static string ActiveIdleInArabic(object o)
  205:         {
  206:             bool b;
  207:             string s;
  208:  
  209:             if (o != null && o.ToString().Length > 0)
  210:             {
  211:                 b = (bool)o;
  212:  
  213:                 if (b) s = "<span style=\"color:Green\">فعال</span>";
  214:                 else s = "<span style=\"color:DarkGoldenRod\">مطفأ</span>";
  215:             }
  216:             else s = "";
  217:  
  218:             return s;
  219:         }
  220:  
  221:         ////////////////////////////////////////////////////////////////////////////
  222:         ////////////////////////////////////////////////////////////////////////////
  223:  
  224:         /// <summary>
  225:         /// Return a random number n where maxValue > n >= 0
  226:         /// <param name="maxValue">integer</param>
  227:         /// <returns>int where maxValue > n >= 0</returns>
  228:         /// </summary>
  229:         public static int Random(int maxValue)
  230:         {
  231:             return random.Next(maxValue);
  232:         }
  233:  
  234:         ////////////////////////////////////////////////////////////////////////////
  235:  
  236:         /// <summary>
  237:         /// Return a random number n, with seed, where ra > num >= 0
  238:         /// <param name="seed">integer</param>
  239:         /// <param name="ra">integer</param>
  240:         /// <returns>int where ra > num >=0</returns>
  241:         /// </summary>
  242:         public static int RandomWithSeed(int seed, int ra)
  243:         {
  244:             // return a random number num: ra > num >= 0
  245:             int num;
  246:  
  247:             num = random.Next(ra);
  248:  
  249:             return num;
  250:         }
  251:  
  252:         ////////////////////////////////////////////////////////////////////////////
  253:  
  254:         /// <summary>
  255:         /// Return a random number n where r1 > num >= r0
  256:         /// <param name="r1">integer max</param>
  257:         /// <param name="r0">integer min</param>
  258:         /// <returns>int where r1 >= num >= r0</returns>
  259:         /// </summary>
  260:         public static int Random(int r0, int r1)
  261:         {
  262:             // return a random number num: r1 >= num >= r0
  263:             int num;
  264:  
  265:             num = random.Next(r1) + r0;
  266:  
  267:             return num;
  268:         }
  269:  
  270:         ////////////////////////////////////////////////////////////////////////////
  271:  
  272:         /// <summary>
  273:         /// Return a pseudo-random item from list
  274:         /// </summary>
  275:         public static string Random(string s)
  276:         {
  277:             // take a list of comma seperated values in s and return one pseudo-random value
  278:             int n;
  279:             string[] sp;
  280:  
  281:             sp = s.Split(',');
  282:  
  283:             n = Random(sp.Length);
  284:  
  285:             return sp[n].ToString();
  286:         }
  287:  
  288:         ////////////////////////////////////////////////////////////////////////////
  289:  
  290:         /// <summary>
  291:         ///
  292:         /// </summary>
  293:         public static bool RandomBool
  294:         {
  295:             get
  296:             {
  297:                 bool b;
  298:                 int n;
  299:  
  300:                 n = random.Next(2);
  301:  
  302:                 if (n == 1) b = true;
  303:                 else b = false;
  304:  
  305:                 return b;
  306:             }
  307:         }
  308:  
  309:         ////////////////////////////////////////////////////////////////////////////
  310:  
  311:         /// <summary>
  312:         /// Return a random weighted bool value between 0 and 100
  313:         /// </summary>
  314:         public static bool RandomPercentWeightedBool(int weight)
  315:         {
  316:             bool b;
  317:  
  318:             weight = weight > 100 ? 100 : weight;
  319:             weight = weight < 0 ? 0 : weight;
  320:  
  321:             b = (random.Next(0, 100) < weight);
  322:  
  323:             return b;
  324:         }
  325:  
  326:         ////////////////////////////////////////////////////////////////////////////
  327:  
  328:         /// <summary>
  329:         /// Return a random percent value between 0 and 100
  330:         /// </summary>
  331:         public static int RandomPercent()
  332:         {
  333:             return random.Next(0, 100);
  334:         }
  335:  
  336:         ////////////////////////////////////////////////////////////////////////////
  337:  
  338:         /// <summary>
  339:         ///
  340:         /// </summary>
  341:         public static string RandomAlphanumeric(int length)
  342:         {
  343:             const string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  344:  
  345:             return new string(Enumerable.Repeat(chars, length).Select(s => s[random.Next(s.Length)]).ToArray());
  346:         }
  347:  
  348:         ////////////////////////////////////////////////////////////////////////////
  349:  
  350:         /// <summary>
  351:         ///
  352:         /// </summary>
  353:         public static string RandomNumber(int length)
  354:         {
  355:             const string chars = "0123456789";
  356:  
  357:             return new string(Enumerable.Repeat(chars, length).Select(s => s[random.Next(s.Length)]).ToArray());
  358:         }
  359:  
  360:         ////////////////////////////////////////////////////////////////////////////
  361:         ////////////////////////////////////////////////////////////////////////////
  362:  
  363:         /// <summary>
  364:         ///
  365:         /// </summary>
  366:         public static List<int> RandomList(int start, int end, int count)
  367:         {
  368:             int n;
  369:             List<int> list;
  370:  
  371:             list = new List<int>();
  372:  
  373:             while (list.Count < count)
  374:             {
  375:                 n = random.Next(start, end);
  376:  
  377:                 list.Add(n);
  378:             }
  379:  
  380:             return list;
  381:         }
  382:  
  383:         ////////////////////////////////////////////////////////////////////////////
  384:  
  385:         /// <summary>
  386:         /// Return at tick + count long
  387:         /// </summary>
  388:         public static long TickAndCountId(int count)
  389:         {
  390:             return DateTime.UtcNow.AddHours(3).Ticks + count;
  391:         }
  392:  
  393:         ////////////////////////////////////////////////////////////////////////////
  394:  
  395:         /// <summary>
  396:         /// Return at second + count long
  397:         /// </summary>
  398:         public static long TickSecondAndCountId(int count)
  399:         {
  400:             return DateTimeSecond() + count;
  401:         }
  402:  
  403:         ////////////////////////////////////////////////////////////////////////////
  404:  
  405:         /// <summary>
  406:         ///
  407:         /// </summary>
  408:         public static string TickDateTime(string tick)
  409:         {
  410:             // reads a tick count and returns the date time as string
  411:             string line;
  412:  
  413:             try
  414:             {
  415:                 DateTime t = new DateTime(long.Parse(tick));
  416:                 line = t.ToString("dd/MM/yyyy HH:mm");
  417:             }
  418:             catch (Exception)
  419:             {
  420:                 line = "";
  421:             }
  422:  
  423:             return line;
  424:         }
  425:  
  426:         ////////////////////////////////////////////////////////////////////////////
  427:  
  428:         /// <summary>
  429:         ///
  430:         /// </summary>
  431:         public static long DateTimeSecond(DateTime dt)
  432:         {
  433:             // return the number of seconds (total) in datetime
  434:             long l;
  435:  
  436:             // A single tick represents one hundred nanoseconds or one ten-millionth of a second
  437:  
  438:             l = dt.Ticks / 10000000;
  439:  
  440:             return l;
  441:         }
  442:  
  443:         ////////////////////////////////////////////////////////////////////////////
  444:  
  445:         /// <summary>
  446:         /// Return the number of seconds (total) in datetime. A single tick represents one hundred nanoseconds or one ten-millionth of a second.
  447:         /// </summary>
  448:         public static int DateTimeSecond()
  449:         {
  450:             return (int)(DateTime.UtcNow.AddHours(3).Ticks / 10000000);
  451:         }
  452:  
  453: #if WFA
  454: #else
  455:         /*
  456:         ////////////////////////////////////////////////////////////////////////////
  457: 
  458:         /// <summary>
  459:         ///
  460:         /// </summary>
  461:         public static void InitializeDropDownList(DropDownList ddl, DataTable source_dt, string text_field, string value_field, int selected_index)
  462:         {
  463:             int index;
  464: 
  465:             if (selected_index == -1) index = ddl.SelectedIndex;
  466:             else index = selected_index;
  467: 
  468:             ddl.Items.Clear();
  469:             //ddl.Items.Add(new ListItem("XXX","0"));
  470:             ddl.DataSource = source_dt;
  471:             ddl.DataTextField = text_field;
  472:             ddl.DataValueField = value_field;
  473:             ddl.DataBind();
  474:             ddl.SelectedIndex = index;
  475:         }
  476:         */
  477: #endif
  478:         ////////////////////////////////////////////////////////////////////////////
  479:  
  480:         /// <summary>
  481:         ///
  482:         /// </summary>
  483:         public static bool IsFloat(string line)
  484:         {
  485:             // this check if line is floating point number. 
  486:             bool ni = false;
  487:  
  488:             try { float x = float.Parse(line); ni = true; }
  489:             catch (Exception) { ni = false; }
  490:  
  491:             return ni;
  492:         }
  493:  
  494:         ////////////////////////////////////////////////////////////////////////////
  495:  
  496:         /// <summary>
  497:         ///
  498:         /// </summary>
  499:         public static bool IsDecimal(string line)
  500:         {
  501:             // this check if line is a decimal number. 
  502:             bool ni = false;
  503:  
  504:             try { decimal x = decimal.Parse(line); ni = true; }
  505:             catch (Exception) { ni = false; }
  506:  
  507:             return ni;
  508:         }
  509:  
  510:         ////////////////////////////////////////////////////////////////////////////
  511:  
  512:         /// <summary>
  513:         ///
  514:         /// </summary>
  515:         public static bool IsInt(string line)
  516:         {
  517:             // this check if line is an integer
  518:             bool ni = false;
  519:  
  520:             try { int x = int.Parse(line); ni = true; }
  521:             catch (Exception) { ni = false; }
  522:  
  523:             return ni;
  524:         }
  525:  
  526:         ////////////////////////////////////////////////////////////////////////////
  527:  
  528:         /// <summary>
  529:         ///
  530:         /// </summary>
  531:         public static string ByteToHex(byte[] bytes)
  532:         {
  533:             StringBuilder sb = new StringBuilder(2500);
  534:  
  535:             UTF8Encoding utf8 = new UTF8Encoding();
  536:             foreach (byte b in bytes) sb.Append(b.ToString("x2"));
  537:  
  538:             return sb.ToString();
  539:         }
  540:  
  541:         ////////////////////////////////////////////////////////////////////////////
  542:  
  543:         /// <summary>
  544:         ///
  545:         /// </summary>
  546:         public static byte[] HexToByte(string hex_str)
  547:         {
  548:             int i, n;
  549:             byte[] bytes;
  550:             char[] chars;
  551:             string c_str = "";
  552:             UTF8Encoding utf8 = new UTF8Encoding();
  553:  
  554:             chars = hex_str.ToCharArray();
  555:  
  556:             bytes = new byte[chars.Length / 2];  // since hex_str has two chars for every byte
  557:  
  558:             n = 0;
  559:             for (i = 0; i < chars.Length; i += 2)
  560:             {
  561:                 c_str = chars[i].ToString() + chars[i + 1].ToString();
  562:                 bytes[n++] = (byte)Convert.ToInt32(c_str, 16);
  563:             }
  564:  
  565:             return bytes;
  566:         }
  567:  
  568:         ////////////////////////////////////////////////////////////////////////////
  569:  
  570:         /// <summary>
  571:         ///
  572:         /// </summary>
  573:         public static string DecToHex(int dec)
  574:         {
  575:             uint uiDecimal = 0;
  576:             string hex;
  577:  
  578:             try
  579:             {
  580:                 // convert text string to unsigned integer
  581:                 uiDecimal = checked((uint)System.Convert.ToUInt32(dec));
  582:  
  583:                 hex = String.Format("{0:x2}", uiDecimal);
  584:             }
  585:             catch (System.OverflowException)
  586:             {
  587:                 // Show overflow message and return
  588:                 hex = "";
  589:             }
  590:  
  591:             return hex;
  592:         }
  593:  
  594:         ////////////////////////////////////////////////////////////////////////////
  595:  
  596:         /// <summary>
  597:         ///
  598:         /// </summary>
  599:         public static int HexToDec(string hex)
  600:         {
  601:             // To hold our converted unsigned integer32 value
  602:             uint dec;
  603:  
  604:             try
  605:             {
  606:                 // Convert hex string to unsigned integer
  607:                 dec = System.Convert.ToUInt32(hex, 16);
  608:             }
  609:             catch (System.OverflowException)
  610:             {
  611:                 // Show overflow message and return
  612:                 return 0;
  613:             }
  614:  
  615:             return (int)dec;
  616:         }
  617:  
  618:         ////////////////////////////////////////////////////////////////////////////
  619:  
  620:         /// <summary>
  621:         /// http://stackoverflow.com/questions/3702216/how-to-convert-integer-to-binary-string-in-c
  622:         /// </summary>
  623:         public static string IntegerToBinaryString(int x)
  624:         {
  625:             return Convert.ToString(x, 2);
  626:         }
  627:  
  628:         ////////////////////////////////////////////////////////////////////////////
  629:  
  630:         /// <summary>
  631:         /// pos 0 is least significant bit, pos 7 is most
  632:         /// http://stackoverflow.com/questions/2431732/checking-if-a-bit-is-set-or-not
  633:         /// </summary>
  634:         public static bool IsBitSet(byte b, int pos)
  635:         {
  636:             return (b & (1 << pos)) != 0;
  637:         }
  638:  
  639:         ////////////////////////////////////////////////////////////////////////////
  640:  
  641:         /// <summary>
  642:         ///
  643:         /// </summary>
  644:         public static DataTable GenerateEmptyDataTable(DataTable dt)
  645:         {
  646:             // this function is used to produce a single empty DataTable line to make the GridView look nicer.
  647:             // this will simply clone the in_dt and create a completly empty line
  648:             DataRow dr;
  649:  
  650:             if (dt.Rows.Count == 0)
  651:             {
  652:  
  653:                 try
  654:                 {
  655:                     dr = dt.NewRow();
  656:  
  657:                     foreach (DataColumn dc in dt.Columns)
  658:                     {
  659:                         dr[dc.ColumnName] = DBNull.Value;
  660:                     }
  661:  
  662:                     dt.Rows.Add(dr);
  663:                 }
  664:                 catch (Exception)
  665:                 {
  666:                     return null;
  667:                 }
  668:             }
  669:  
  670:             return dt;
  671:         }
  672:  
  673: #if WFA
  674: #else
  675:         /*
  676:         ////////////////////////////////////////////////////////////////////////////
  677: 
  678:         /// <summary>
  679:         /// 
  680:         /// </summary>
  681:         public static string GetPostBackControl(Page page)
  682:         {
  683:             // return the name of control that fired the postback
  684:             // this is strange, sometimes it fills the ID with real name of control and sometimes the TEXT
  685: 
  686:             string s = "";
  687:             Control c = null;
  688: 
  689:             string ctrlname = page.Request.Params.Get("__EVENTTARGET");
  690:             if (ctrlname != null && ctrlname != string.Empty)
  691:             {
  692:                 c = page.FindControl(ctrlname);
  693:             }
  694:             else
  695:             {
  696:                 foreach (string ctl in page.Request.Form)
  697:                 {
  698:                     Control ci = page.FindControl(ctl);
  699:                     if (ci is System.Web.UI.WebControls.Button)
  700:                     {
  701:                         c = ci;
  702:                         break;
  703:                     }
  704:                 }
  705:             }
  706: 
  707:             if (c != null)
  708:             {
  709:                 if (c.GetType().ToString() == "System.Web.UI.WebControls.Button")
  710:                 {
  711:                     s = ((System.Web.UI.WebControls.Button)c).ID;
  712:                 }
  713:                 else if (c.GetType().ToString() == "System.Web.UI.WebControls.DropDownList")
  714:                 {
  715:                     s = ((System.Web.UI.WebControls.DropDownList)c).ID;
  716:                 }
  717:                 else s = "";
  718:             }
  719:             else s = "";
  720: 
  721:             return s;
  722:         }
  723:         */
  724: #endif
  725:  
  726:         ////////////////////////////////////////////////////////////////////////////
  727:  
  728:         /// <summary>
  729:         ///
  730:         /// </summary>
  731:         public static string Color(int count, int index)
  732:         {
  733:             string s;
  734:  
  735:             // rainbow:
  736:             string[] color = { "#f00", "#f10", "#f20", "#f30", "#f40", "#f50", "#f60", "#f70", "#f80", "#f90", "#fa0", "#fb0", "#fc0", "#fd0", "#fe0", "#ff0", "#ef0", "#df0", "#cf0", "#bf0", "#af0", "#9f0", "#8f0", "#7f0", "#6f0", "#5f0", "#4f0", "#3f0", "#2f0", "#1f0", "#0f0", "#0f1", "#0f2", "#0f3", "#0f4", "#0f5", "#0f6", "#0f7", "#0f8", "#0f9", "#0fa", "#0fb", "#0fc", "#0fd", "#0fe", "#0ff", "#0ef", "#0df", "#0cf", "#0bf", "#0af", "#09f", "#08f", "#07f", "#06f", "#05f", "#04f", "#03f", "#02f", "#01f", "#00f", "#10f", "#20f", "#30f", "#40f", "#50f", "#60f", "#70f", "#80f", "#90f", "#a0f", "#b0f", "#c0f", "#d0f", "#e0f" };
  737:  
  738:             // random clear
  739:             //string[] color = {"Black","Blue","BlueViolet","Brown","CadetBlue","CornFlowerBlue","Crimson","DarkBlue","DarkCyan","DarkGoldenRod","DarkGreen","DarkMagenta","DarkOliveGreen","DarkOrange","DarkOrchid","DarkRed","DarkSlateBlue","DarkSlateGray","DarkViolet","Firebrick","ForestGreen","Green","IndianRed","Indigo","LightGray","LightSeaGreen","LightSkyBlue","LightSlateGray","Maroon","MediumBlue","MediumOrchid","MediumPurple","MediumSeaGreen","MediumSlateBlue","MediumVioletRed","MidnightBlue","Navy","Olive","OliveDrab"," Orange","OrangeRed","Orchid","Purple","Red","RosyBrown","RoyalBlue","SaddleBrown","SlateBlue","SlateGray","Teal","Tomato","Transparent" };
  740:  
  741:             if (index > 0) index--;
  742:  
  743:             s = color[color.Length / count * index];
  744:  
  745:             return s;
  746:         }
  747:  
  748:         ////////////////////////////////////////////////////////////////////////////
  749:  
  750:         /// <summary>
  751:         ///
  752:         /// </summary>
  753:         public static List<string> PaintColorList(int count, int index)
  754:         {
  755:             var s = new List<string>() { "000000", "7F7F7F", "880015", "ED1C24", "FF7F27", "FFF200", "22B14C", "00A2E8", "3F48CC", "A349A4", "FFFFFF", "C3C3C3", "B97A57", "FFAEC9", "FFC90E", "EFE4B0", "B5E61D", "99D9EA", "7092BE", "C8BFE7" };
  756:  
  757:             return s;
  758:         }
  759:  
  760:         ////////////////////////////////////////////////////////////////////////////
  761:  
  762:         /// <summary>
  763:         ///
  764:         /// </summary>
  765:         public static string FormatHtml(string text)
  766:         {
  767:             text = Regex.Replace(text, @"[\n|\r]+", "</p><p>");
  768:             text = "<p>" + text + "</p>";
  769:  
  770:             return text;
  771:         }
  772:  
  773:         ////////////////////////////////////////////////////////////////////////////
  774:         ////////////////////////////////////////////////////////////////////////////
  775:  
  776:         /// <summary>
  777:         ///
  778:         /// <see href="http://stackoverflow.com/questions/11412956/what-is-the-best-way-of-validating-an-ip-address"/>
  779:         /// </summary>
  780:         public static bool IsValidIpv4(string ipString)
  781:         {
  782:             bool isValid;
  783:             byte tempForParsing;
  784:             string[] splitValues;
  785:  
  786:             if (!string.IsNullOrWhiteSpace(ipString) && !string.IsNullOrEmpty(ipString))
  787:             {
  788:                 splitValues = ipString.Split('.');
  789:  
  790:                 if (splitValues.Length != 4) isValid = false;
  791:                 else
  792:                 {
  793:                     isValid = splitValues.All(r => byte.TryParse(r, out tempForParsing));
  794:                 }
  795:             }
  796:             else isValid = false;
  797:  
  798:             return isValid;
  799:         }
  800:  
  801:         ////////////////////////////////////////////////////////////////////////////
  802:  
  803:         /// <summary>
  804:         ///
  805:         /// <see href="https://stackoverflow.com/questions/7578857/how-to-check-whether-a-string-is-a-valid-http-url"/>
  806:         /// </summary>
  807:         public static bool IsValidUrl(string uriName)
  808:         {
  809:             var isValid = Uri.TryCreate(uriName, UriKind.Absolute, out Uri uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
  810:  
  811:             return isValid;
  812:         }
  813:  
  814:         ////////////////////////////////////////////////////////////////////////////
  815:  
  816:         /// <summary>
  817:         ///
  818:         /// </summary>
  819:         public static uint IpToUint(string ip)
  820:         {
  821:             uint uintAddress;
  822:  
  823:             IPAddress address = IPAddress.Parse(ip);
  824:             byte[] bytes = address.GetAddressBytes();
  825:             Array.Reverse(bytes); // flip big-endian(network order) to little-endian
  826:             uintAddress = BitConverter.ToUInt32(bytes, 0);
  827:  
  828:             return uintAddress;
  829:  
  830:             /*
  831:             string delimStr = ".";
  832:             char[] delimiter = delimStr.ToCharArray();
  833:             string[] ip_addr = null;
  834:             long ip_num = 0;
  835: 
  836:             try
  837:             {
  838:                 ip_addr = ip.Split(delimiter, 4);
  839:                 ip_num = (long.Parse(ip_addr[0]) * 16777216) + (long.Parse(ip_addr[1]) * 65536) + (long.Parse(ip_addr[2]) * 256) + (long.Parse(ip_addr[3]));
  840:             }
  841:             catch (Exception)
  842:             {
  843:             }
  844: 
  845:             return ip_num;
  846:             */
  847:  
  848:             /*
  849:             long l;
  850:             string r;
  851:             Match m;
  852: 
  853:             m = Regex.Match(ip, @"(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})");
  854: 
  855:             if (m.Success)
  856:             {
  857:                 r = m.Groups[1].Captures[0].Value.PadLeft(3, '0') + m.Groups[2].Captures[0].Value.PadLeft(3, '0') + m.Groups[3].Captures[0].Value.PadLeft(3, '0') + m.Groups[4].Captures[0].Value.PadLeft(3, '0');
  858:                 l = long.Parse(r);
  859:             }
  860:             else l = 0;
  861: 
  862:             return l;
  863:             */
  864:         }
  865:  
  866:         ////////////////////////////////////////////////////////////////////////////
  867:  
  868:         /// <summary>
  869:         ///
  870:         /// </summary>
  871:         public static string UintToIp(uint ui)
  872:         {
  873:             string ipAddress;
  874:  
  875:             byte[] bytes = BitConverter.GetBytes(ui);
  876:             Array.Reverse(bytes); // flip little-endian to big-endian(network order)
  877:             ipAddress = new IPAddress(bytes).ToString();
  878:  
  879:             return ipAddress;
  880:  
  881:             /*
  882:             string s, s1, s2, s3, s4;
  883: 
  884:             s = l.ToString();
  885:             s = s.PadLeft(12, '0');
  886: 
  887:             s1 = s.Substring(0, 3);
  888:             s2 = s.Substring(3, 3);
  889:             s3 = s.Substring(6, 3);
  890:             s4 = s.Substring(9, 3);
  891: 
  892:             s = s1 + "." + s2 + "." + s3 + "." + s4;
  893: 
  894:             s = Regex.Replace(s, @"\.0+", ".");
  895:             s = Regex.Replace(s, @"^0+", "");
  896:             if (s[s.Length - 1] == '.') s = s + "0";
  897: 
  898:             return s;
  899:             */
  900:         }
  901:  
  902:         ////////////////////////////////////////////////////////////////////////////
  903:  
  904:         /// <summary>
  905:         ///
  906:         /// </summary>
  907:         public static string IpToHex(string ip)
  908:         {
  909:             string h;
  910:             Match m;
  911:  
  912:             h = "";
  913:  
  914:             m = Regex.Match(ip, @"(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})");
  915:  
  916:             if (m.Success)
  917:             {
  918:                 h = DecToHex(int.Parse(m.Groups[1].Captures[0].Value)) + DecToHex(int.Parse(m.Groups[2].Captures[0].Value)) + DecToHex(int.Parse(m.Groups[3].Captures[0].Value)) + DecToHex(int.Parse(m.Groups[4].Captures[0].Value));
  919:             }
  920:             else h = "";
  921:  
  922:             return h;
  923:         }
  924:  
  925:         ////////////////////////////////////////////////////////////////////////////
  926:  
  927:         /// <summary>
  928:         ///
  929:         /// </summary>
  930:         public static string HexToIp(string h)
  931:         {
  932:             string s, s1, s2, s3, s4;
  933:  
  934:             h = h.PadLeft(8, '0');
  935:  
  936:             s1 = h.Substring(0, 2);
  937:             s2 = h.Substring(2, 2);
  938:             s3 = h.Substring(4, 2);
  939:             s4 = h.Substring(6, 2);
  940:  
  941:             s = HexToDec(s1) + "." + HexToDec(s2) + "." + HexToDec(s3) + "." + HexToDec(s4);
  942:  
  943:             return s;
  944:         }
  945:  
  946:         ////////////////////////////////////////////////////////////////////////////
  947:  
  948:         /// <summary>
  949:         ///
  950:         /// </summary>
  951:         public static string DecToIp(int i)
  952:         {
  953:             return HexToIp(DecToHex(i));
  954:         }
  955:  
  956:         ////////////////////////////////////////////////////////////////////////////
  957:  
  958:         /// <summary>
  959:         ///
  960:         /// </summary>
  961:         public static int IpToDec(string s)
  962:         {
  963:             return HexToDec(IpToHex(s));
  964:         }
  965:  
  966:         ////////////////////////////////////////////////////////////////////////////
  967:  
  968:         /// <summary>
  969:         /// Check a string to see if all characters are hexadecimal values
  970:         /// <see href="https://stackoverflow.com/questions/223832/check-a-string-to-see-if-all-characters-are-hexadecimal-values"/>
  971:         /// </summary>
  972:         public static bool IsHex(string term)
  973:         {
  974:             // For C-style hex notation (0xFF) you can use @"\A\b(0[xX])?[0-9a-fA-F]+\b\Z"
  975:             return System.Text.RegularExpressions.Regex.IsMatch(term, @"\A\b[0-9a-fA-F]+\b\Z");
  976:         }
  977:  
  978:         ////////////////////////////////////////////////////////////////////////////
  979:  
  980:         /// <summary>
  981:         ///
  982:         /// </summary>
  983:         public static string NetworkNumberFromIp(string ip)
  984:         {
  985:             string[] ipp;
  986:             string networkNumber;
  987:  
  988:             networkNumber = "";
  989:  
  990:             if (IsValidIpv4(ip))
  991:             {
  992:                 ipp = ip.Split('.');
  993:  
  994:                 networkNumber = (ipp[0] + "." + ipp[1] + "." + ipp[2] + ".0");
  995:             }
  996:  
  997:             return networkNumber;
  998:         }
  999:  
 1000:         ////////////////////////////////////////////////////////////////////////////
 1001:  
 1002:         /// <summary>
 1003:         ///
 1004:         /// </summary>
 1005:         public static List<string> NetworkNumberStrippedList(string startIp, string endIp)
 1006:         {
 1007:             int startIpI, endIpI;
 1008:             string s;
 1009:             List<long> li;
 1010:             List<string> list;
 1011:  
 1012:             li = new List<long>();
 1013:             list = new List<string>();
 1014:  
 1015:             // change network number to a real IP
 1016:             if (Regex.IsMatch(startIp, @"^.+\.0$")) startIp = Regex.Replace(startIp, @"^(.+)\.0$", "$1.1");
 1017:             if (Regex.IsMatch(endIp, @"^.+\.0$")) endIp = Regex.Replace(endIp, @"^(.+)\.0$", "$1.1");
 1018:  
 1019:             startIpI = IpToDec(startIp);
 1020:             endIpI = IpToDec(endIp);
 1021:  
 1022:             for (int i = startIpI; i <= endIpI; i += 256)
 1023:             {
 1024:                 s = DecToIp(i);
 1025:                 s = NetworkNumberFromIp(s);
 1026:  
 1027:                 s = Regex.Replace(s, @"^(.+)\.0$", "$1");
 1028:  
 1029:                 list.Add(s);
 1030:             }
 1031:  
 1032:             return list;
 1033:         }
 1034:  
 1035:         ////////////////////////////////////////////////////////////////////////////
 1036:  
 1037:         /// <summary>
 1038:         /// Check if the passed IP address belongs to Kuwait
 1039:         /// </summary>
 1040:         public static bool IsKuwaitIp(string ip)
 1041:         {
 1042:             bool b;
 1043:             uint ui;
 1044:  
 1045:             ui = Ia.Cl.Models.Default.IpToUint(ip);
 1046:  
 1047:             if (
 1048:             (ui >= 1044742144 && ui <= 1044750335)
 1049:             || (ui >= 1050017792 && ui <= 1050083327)
 1050:             || (ui >= 1054277632 && ui <= 1054343167)
 1051:             || (ui >= 1075513152 && ui <= 1075513183)
 1052:             || (ui >= 1125110400 && ui <= 1125110463)
 1053:             || (ui >= 1161630912 && ui <= 1161630919)
 1054:             || (ui >= 1161641888 && ui <= 1161641911)
 1055:             || (ui >= 1163558016 && ui <= 1163558028)
 1056:             || (ui >= 1308094464 && ui <= 1308096511)
 1057:             || (ui >= 1314455552 && ui <= 1314521087)
 1058:             || (ui >= 1318764544 && ui <= 1318780927)
 1059:             || (ui >= 1319084032 && ui <= 1319092223)
 1060:             || (ui >= 1347219456 && ui <= 1347223551)
 1061:             || (ui >= 1347294424 && ui <= 1347294431)
 1062:             || (ui >= 1347321856 && ui <= 1347323775)
 1063:             || (ui >= 1347323904 && ui <= 1347325183)
 1064:             || (ui >= 1347325440 && ui <= 1347325951)
 1065:             || (ui >= 1354235904 && ui <= 1354301439)
 1066:             || (ui >= 1361035904 && ui <= 1361035907)
 1067:             || (ui >= 1361036764 && ui <= 1361036767)
 1068:             || (ui >= 1361036792 && ui <= 1361036795)
 1069:             || (ui >= 1365220792 && ui <= 1365220799)
 1070:             || (ui >= 1383273984 && ui <= 1383276543)
 1071:             || (ui >= 1383367168 && ui <= 1383367679)
 1072:             || (ui >= 1383368848 && ui <= 1383368895)
 1073:             || (ui >= 1383369120 && ui <= 1383369231)
 1074:             || (ui >= 1383369248 && ui <= 1383369535)
 1075:             || (ui >= 1383369600 && ui <= 1383370751)
 1076:             || (ui >= 1383371776 && ui <= 1383374591)
 1077:             || (ui >= 1385283584 && ui <= 1385291775)
 1078:             || (ui >= 1397006336 && ui <= 1397014527)
 1079:             || (ui >= 1398800384 && ui <= 1398833151)
 1080:             || (ui >= 1403658528 && ui <= 1403658559)
 1081:             || (ui >= 1410013696 && ui <= 1410013727)
 1082:             || (ui >= 1410013920 && ui <= 1410013951)
 1083:             || (ui >= 1410014016 && ui <= 1410014047)
 1084:             || (ui >= 1410014464 && ui <= 1410014495)
 1085:             || (ui >= 1410027008 && ui <= 1410027263)
 1086:             || (ui >= 1410035200 && ui <= 1410035231)
 1087:             || (ui >= 1410035264 && ui <= 1410035295)
 1088:             || (ui >= 1425426432 && ui <= 1425428479)
 1089:             || (ui >= 1441726464 && ui <= 1441729023)
 1090:             || (ui >= 1441729536 && ui <= 1441734655)
 1091:             || (ui >= 1475115008 && ui <= 1475117055)
 1092:             || (ui >= 1500186624 && ui <= 1500188671)
 1093:             || (ui >= 1506476032 && ui <= 1506508799)
 1094:             || (ui >= 1509642240 && ui <= 1509644351)
 1095:             || (ui >= 1509644384 && ui <= 1509646335)
 1096:             || (ui >= 1533419520 && ui <= 1533419775)
 1097:             || (ui >= 1533420032 && ui <= 1533420287)
 1098:             || (ui >= 1533420544 && ui <= 1533421567)
 1099:             || (ui >= 1533448192 && ui <= 1533450239)
 1100:             || (ui >= 1533470720 && ui <= 1533472767)
 1101:             || (ui >= 1533513728 && ui <= 1533515775)
 1102:             || (ui >= 1535934464 && ui <= 1535967231)
 1103:             || (ui >= 1536660016 && ui <= 1536660019)
 1104:             || (ui >= 1536663424 && ui <= 1536663551)
 1105:             || (ui >= 1539227648 && ui <= 1539229695)
 1106:             || (ui >= 1539466752 && ui <= 1539467263)
 1107:             || (ui >= 1539473920 && ui <= 1539474431)
 1108:             || (ui >= 1539737088 && ui <= 1539737343)
 1109:             || (ui >= 1540410112 && ui <= 1540410367)
 1110:             || (ui >= 1540467712 && ui <= 1540467967)
 1111:             || (ui >= 1540622336 && ui <= 1540622591)
 1112:             || (ui >= 1540628480 && ui <= 1540628735)
 1113:             || (ui >= 1540790272 && ui <= 1540791295)
 1114:             || (ui >= 1572814848 && ui <= 1572816895)
 1115:             || (ui >= 1578991616 && ui <= 1579024383)
 1116:             || (ui >= 1585446912 && ui <= 1585577983)
 1117:             || (ui >= 1589346304 && ui <= 1589379071)
 1118:             || (ui >= 1598160896 && ui <= 1598193663)
 1119:             || (ui >= 1603137536 && ui <= 1603141631)
 1120:             || (ui >= 1605320704 && ui <= 1605328895)
 1121:             || (ui >= 1925638656 && ui <= 1925638911)
 1122:             || (ui >= 1925639680 && ui <= 1925639935)
 1123:             || (ui >= 2341273600 && ui <= 2341339135)
 1124:             || (ui >= 2717646848 && ui <= 2717712383)
 1125:             || (ui >= 2830827520 && ui <= 2830893055)
 1126:             || (ui >= 3169255424 && ui <= 3169271807)
 1127:             || (ui >= 3169275904 && ui <= 3169278991)
 1128:             || (ui >= 3169279008 && ui <= 3169279231)
 1129:             || (ui >= 3169279256 && ui <= 3169279263)
 1130:             || (ui >= 3169279296 && ui <= 3169279303)
 1131:             || (ui >= 3169279320 && ui <= 3169279743)
 1132:             || (ui >= 3169279760 && ui <= 3169281023)
 1133:             || (ui >= 3169281280 && ui <= 3169288191)
 1134:             || (ui >= 3239285760 && ui <= 3239286783)
 1135:             || (ui >= 3239488512 && ui <= 3239488767)
 1136:             || (ui >= 3240222720 && ui <= 3240223231)
 1137:             || (ui >= 3240812288 && ui <= 3240812543)
 1138:             || (ui >= 3245088256 && ui <= 3245088511)
 1139:             || (ui >= 3249111552 && ui <= 3249112063)
 1140:             || (ui >= 3250335744 && ui <= 3250339839)
 1141:             || (ui >= 3250359808 && ui <= 3250362879)
 1142:             || (ui >= 3250364416 && ui <= 3250372607)
 1143:             || (ui >= 3251120128 && ui <= 3251120639)
 1144:             || (ui >= 3252483072 && ui <= 3252483583)
 1145:             || (ui >= 3252484096 && ui <= 3252486143)
 1146:             || (ui >= 3258368000 && ui <= 3258384383)
 1147:             || (ui >= 3262477220 && ui <= 3262477223)
 1148:             || (ui >= 3262478601 && ui <= 3262478601)
 1149:             || (ui >= 3263045632 && ui <= 3263046847)
 1150:             || (ui >= 3263046912 && ui <= 3263047935)
 1151:             || (ui >= 3263048192 && ui <= 3263053823)
 1152:             || (ui >= 3266341888 && ui <= 3266342143)
 1153:             || (ui >= 3274145792 && ui <= 3274162175)
 1154:             || (ui >= 3276114944 && ui <= 3276115967)
 1155:             || (ui >= 3276687872 && ui <= 3276688383)
 1156:             || (ui >= 3276858112 && ui <= 3276858367)
 1157:             || (ui >= 3277381120 && ui <= 3277381631)
 1158:             || (ui >= 3280580096 && ui <= 3280580351)
 1159:             || (ui >= 3285922816 && ui <= 3285923327)
 1160:             || (ui >= 3286425600 && ui <= 3286433791)
 1161:             || (ui >= 3286566656 && ui <= 3286567423)
 1162:             || (ui >= 3286568192 && ui <= 3286568703)
 1163:             || (ui >= 3286571008 && ui <= 3286571775)
 1164:             || (ui >= 3288417536 && ui <= 3288418047)
 1165:             || (ui >= 3340584704 && ui <= 3340584959)
 1166:             || (ui >= 3350042880 && ui <= 3350043135)
 1167:             || (ui >= 3453376848 && ui <= 3453376887)
 1168:             || (ui >= 3487969792 && ui <= 3487970047)
 1169:             || (ui >= 3509522432 && ui <= 3509522687)
 1170:             || (ui >= 3509559040 && ui <= 3509559295)
 1171:             || (ui >= 3512891232 && ui <= 3512891263)
 1172:             || (ui >= 3517438880 && ui <= 3517438911)
 1173:             || (ui >= 3518894096 && ui <= 3518894103)
 1174:             || (ui >= 3523593216 && ui <= 3523593231)
 1175:             || (ui >= 3559587840 && ui <= 3559596031)
 1176:             || (ui >= 3561742336 && ui <= 3561750527)
 1177:             || (ui >= 3575824384 && ui <= 3575832575)
 1178:             || (ui >= 3582255104 && ui <= 3582263295)
 1179:             || (ui >= 3585949696 && ui <= 3585957887)
 1180:             || (ui >= 3628153088 && ui <= 3628153343)
 1181:             || (ui >= 3630097664 && ui <= 3630098175)
 1182:             || (ui >= 3630100224 && ui <= 3630100479)
 1183:             || (ui >= 3632481760 && ui <= 3632481767)
 1184:             || (ui >= 3645222912 && ui <= 3645227007)
 1185:             ) b = true;
 1186:             else b = false;
 1187:  
 1188:             return b;
 1189:         }
 1190:  
 1191:         ////////////////////////////////////////////////////////////////////////////
 1192:         ////////////////////////////////////////////////////////////////////////////
 1193:  
 1194:         // see: https://stackoverflow.com/questions/8113546/how-to-determine-whether-an-ip-address-is-private
 1195:  
 1196:         private static readonly IPNetwork IpV4BlockLoopback = new(IPAddress.Parse("127.0.0.0"), 8);
 1197:         private static readonly IPNetwork IpV4Block24Bits = new(IPAddress.Parse("10.0.0.0"), 8);
 1198:         private static readonly IPNetwork IpV4Block20Bits = new(IPAddress.Parse("172.16.0.0"), 12);
 1199:         private static readonly IPNetwork IpV4Block16Bits = new(IPAddress.Parse("192.168.0.0"), 16);
 1200:         private static readonly IPNetwork IpV6Block = new(IPAddress.Parse("fc00::"), 7);
 1201:  
 1202:         ////////////////////////////////////////////////////////////////////////////
 1203:  
 1204:         /// <summary>
 1205:         ///
 1206:         /// </summary>
 1207:         public static bool IsPrivateIp(IPAddress address)
 1208:         {
 1209:             if (address.IsIPv4MappedToIPv6)
 1210:             {
 1211:                 address = address.MapToIPv4();
 1212:             }
 1213:  
 1214:             return address.AddressFamily switch
 1215:             {
 1216:                 AddressFamily.InterNetwork => IsPrivateIpV4(address),
 1217:                 AddressFamily.InterNetworkV6 => IsPrivateIpV6(address),
 1218:                 _ => throw new ArgumentException(null, nameof(address)),
 1219:             };
 1220:         }
 1221:  
 1222:         private static bool IsPrivateIpV4(IPAddress address)
 1223:         {
 1224:             return IpV4BlockLoopback.Contains(address)
 1225:                    || IpV4Block24Bits.Contains(address)
 1226:                    || IpV4Block20Bits.Contains(address)
 1227:                    || IpV4Block16Bits.Contains(address);
 1228:         }
 1229:  
 1230:         private static bool IsPrivateIpV6(IPAddress address)
 1231:         {
 1232:             return address.Equals(IPAddress.IPv6Loopback)
 1233:                    || address.IsIPv6LinkLocal
 1234:                    || IpV6Block.Contains(address);
 1235:         }
 1236:  
 1237:         ////////////////////////////////////////////////////////////////////////////
 1238:  
 1239:         /// <summary>
 1240:         ///
 1241:         /// </summary>
 1242:         public static bool IsIaHostAddress(IPAddress iPAddress)
 1243:         {
 1244:             bool b;
 1245:  
 1246:             var hostAddress = Ia.Cl.Models.ApplicationConfiguration.GetSetting("AppSettings:IaHostAddress");
 1247:  
 1248:             if (!string.IsNullOrEmpty(hostAddress))
 1249:             {
 1250:                 if (iPAddress.ToString() == hostAddress || iPAddress.ToString() == "::1") b = true;
 1251:                 else b = false;
 1252:             }
 1253:             else b = false;
 1254:  
 1255:             return b;
 1256:         }
 1257:  
 1258:         ////////////////////////////////////////////////////////////////////////////
 1259:         ////////////////////////////////////////////////////////////////////////////
 1260:  
 1261:         /// <summary>
 1262:         ///
 1263:         /// </summary>
 1264:         public static Hashtable DataTableToHashtable(DataTable dt)
 1265:         {
 1266:             // put the datatable first row value into a hashtable key, and second as value. if the table has only one column we will add it only to keys with 0 value
 1267:             Hashtable ht;
 1268:  
 1269:             if (dt != null)
 1270:             {
 1271:                 if (dt.Rows.Count > 0)
 1272:                 {
 1273:  
 1274:                     ht = new Hashtable(dt.Rows.Count);
 1275:  
 1276:                     if (dt.Columns.Count == 1) foreach (DataRow r in dt.Rows) ht[r[0].ToString()] = "0";
 1277:                     else if (dt.Columns.Count > 1) foreach (DataRow r in dt.Rows) ht[r[0].ToString()] = r[1].ToString();
 1278:                 }
 1279:                 else ht = new Hashtable(1);
 1280:             }
 1281:             else ht = null;
 1282:  
 1283:             return ht;
 1284:         }
 1285:  
 1286:         ////////////////////////////////////////////////////////////////////////////
 1287:  
 1288:         /// <summary>
 1289:         ///
 1290:         /// </summary>
 1291:         public static string DataTableToString(DataTable dataTable)
 1292:         {
 1293:             var output = new StringBuilder();
 1294:  
 1295:             // write Column titles
 1296:             for (int i = 0; i < dataTable.Columns.Count; i++)
 1297:             {
 1298:                 var text = dataTable.Columns[i].ColumnName;
 1299:                 output.Append("\t" + text);
 1300:             }
 1301:  
 1302:             output.Append("|\n" + new string('=', output.Length) + "\n");
 1303:  
 1304:             // write rows
 1305:             foreach (DataRow row in dataTable.Rows)
 1306:             {
 1307:                 for (int i = 0; i < dataTable.Columns.Count; i++)
 1308:                 {
 1309:                     var text = row[i].ToString();
 1310:                     output.Append("\t" + text);
 1311:                 }
 1312:  
 1313:                 output.Append("|\n");
 1314:             }
 1315:  
 1316:             return output.ToString();
 1317:         }
 1318:  
 1319:         /*
 1320:         ////////////////////////////////////////////////////////////////////////////
 1321: 
 1322:         /// <summary>
 1323:         ///
 1324:         /// </summary>
 1325:         public static ArrayList ArrayList_Limit_Randomize(ArrayList in_al, int max)
 1326:         {
 1327:             // 
 1328:             // parameter: ArrayList with any number of entries, an integer value indicating the max possible number of returned values
 1329:             // procedure: randomly select upto max values from al and return max >= num >= 0
 1330: 
 1331:             int n, o;
 1332:             ArrayList al;
 1333:             Hashtable ht;
 1334: 
 1335:             if (max > 0)
 1336:             {
 1337:                 al = new ArrayList(max);
 1338:                 ht = new Hashtable(max);
 1339: 
 1340:                 o = 0;
 1341: 
 1342:                 while (o < in_al.Count - 1 && o < max)
 1343:                 {
 1344:                     foreach (string s in in_al)
 1345:                     {
 1346:                         n = r.Next(max);
 1347: 
 1348:                         if (!ht.ContainsKey(n))
 1349:                         {
 1350:                             al.Add(s);
 1351:                             ht[n] = 1;
 1352:                             o++;
 1353:                         }
 1354:                     }
 1355:                 }
 1356: 
 1357:             }
 1358:             else al = null;
 1359: 
 1360:             return al;
 1361:         }
 1362:         */
 1363:  
 1364:         ////////////////////////////////////////////////////////////////////////////
 1365:  
 1366:         /// <summary>
 1367:         ///
 1368:         /// </summary>
 1369:         public static ArrayList SublistArrayList(ArrayList in_al, int n)
 1370:         {
 1371:             // return the first n values from all
 1372:             ArrayList al;
 1373:  
 1374:             if (n > 0)
 1375:             {
 1376:                 al = new ArrayList(n);
 1377:  
 1378:                 for (int i = 0; i < in_al.Count - 1 && i < n; i++)
 1379:                 {
 1380:                     al.Add(in_al[i]);
 1381:                 }
 1382:             }
 1383:             else al = null;
 1384:  
 1385:             return al;
 1386:         }
 1387:  
 1388:         ////////////////////////////////////////////////////////////////////////////
 1389:  
 1390:         /// <summary>
 1391:         ///
 1392:         /// </summary>
 1393:         public static ArrayList ShuffleAndSublistArrayList(ArrayList in_al, int n)
 1394:         {
 1395:             // 
 1396:  
 1397:             ShuffleArrayList(in_al);
 1398:  
 1399:             return SublistArrayList(in_al, n);
 1400:         }
 1401:  
 1402:         ////////////////////////////////////////////////////////////////////////////
 1403:  
 1404:         /// <summary>
 1405:         ///
 1406:         /// </summary>
 1407:         public static ArrayList KeyArrayHashtableToList(Hashtable ht)
 1408:         {
 1409:             // 
 1410:             ArrayList al;
 1411:  
 1412:             if (ht != null)
 1413:             {
 1414:                 if (ht.Count > 0)
 1415:                 {
 1416:                     al = new ArrayList(ht.Count);
 1417:  
 1418:                     foreach (string s in ht.Keys) al.Add(s);
 1419:                 }
 1420:                 else al = new ArrayList(1);
 1421:             }
 1422:             else al = null;
 1423:  
 1424:             al.Sort();
 1425:  
 1426:             return al;
 1427:         }
 1428:  
 1429:         ////////////////////////////////////////////////////////////////////////////
 1430:  
 1431:         /// <summary>
 1432:         ///
 1433:         /// </summary>
 1434:         public static ArrayList KeyIntegerHashtableToArrayList(Hashtable ht)
 1435:         {
 1436:             // 
 1437:             ArrayList al;
 1438:  
 1439:             if (ht != null)
 1440:             {
 1441:                 if (ht.Count > 0)
 1442:                 {
 1443:                     al = new ArrayList(ht.Count);
 1444:  
 1445:                     foreach (int i in ht.Keys) al.Add(i);
 1446:                 }
 1447:                 else al = new ArrayList(1);
 1448:             }
 1449:             else al = null;
 1450:  
 1451:             al.Sort();
 1452:  
 1453:             return al;
 1454:         }
 1455:  
 1456:         ////////////////////////////////////////////////////////////////////////////
 1457:  
 1458:         /// <summary>
 1459:         ///
 1460:         /// </summary>
 1461:         public static Hashtable ReverseKeyValueInHashtable(Hashtable in_ht)
 1462:         {
 1463:             // 
 1464:             Hashtable ht;
 1465:  
 1466:             if (in_ht != null)
 1467:             {
 1468:                 if (in_ht.Count > 0)
 1469:                 {
 1470:                     ht = new Hashtable(in_ht.Count);
 1471:  
 1472:                     foreach (string s in in_ht.Keys) ht[in_ht[s].ToString()] = s;
 1473:                 }
 1474:                 else ht = new Hashtable(1);
 1475:             }
 1476:             else ht = null;
 1477:  
 1478:             return ht;
 1479:         }
 1480:  
 1481:         ////////////////////////////////////////////////////////////////////////////
 1482:  
 1483:         /// <summary>
 1484:         ///
 1485:         /// </summary>
 1486:         public static ArrayList HashtableValueToArrayList(Hashtable ht)
 1487:         {
 1488:             // 
 1489:             ArrayList al;
 1490:  
 1491:             if (ht != null)
 1492:             {
 1493:                 if (ht.Count > 0)
 1494:                 {
 1495:                     al = new ArrayList(ht.Count);
 1496:  
 1497:                     foreach (string s in ht.Values) al.Add(s);
 1498:                 }
 1499:                 else al = new ArrayList(1);
 1500:             }
 1501:             else al = null;
 1502:  
 1503:             al.Sort();
 1504:  
 1505:             return al;
 1506:         }
 1507:  
 1508:         ////////////////////////////////////////////////////////////////////////////
 1509:  
 1510:         /// <summary>
 1511:         ///
 1512:         /// </summary>
 1513:         public static string HashtableKeyString(Hashtable ht)
 1514:         {
 1515:             // 
 1516:             string si;
 1517:             StringBuilder sb;
 1518:  
 1519:             if (ht != null)
 1520:             {
 1521:                 if (ht.Count > 0)
 1522:                 {
 1523:                     sb = new StringBuilder(ht.Count);
 1524:                     sb.Length = 0;
 1525:  
 1526:                     foreach (string s in ht.Keys) sb.Append(s + "|");
 1527:                 }
 1528:                 else sb = new StringBuilder(1);
 1529:             }
 1530:             else sb = null;
 1531:  
 1532:             si = sb.ToString();
 1533:             si = si.Remove(si.Length - 1, 1);
 1534:  
 1535:             return si;
 1536:         }
 1537:  
 1538:         ////////////////////////////////////////////////////////////////////////////
 1539:  
 1540:         /// <summary>
 1541:         ///
 1542:         /// </summary>
 1543:         public static Hashtable SortHashtableKey(Hashtable in_ht)
 1544:         {
 1545:             // sort the hashtable keys alphabetically
 1546:  
 1547:             ArrayList al;
 1548:             Hashtable ht;
 1549:  
 1550:             if (in_ht != null)
 1551:             {
 1552:                 if (in_ht.Count > 0)
 1553:                 {
 1554:                     al = new ArrayList(in_ht.Count + 1);
 1555:                     ht = new Hashtable(in_ht.Count + 1);
 1556:  
 1557:                     al.Clear();
 1558:                     foreach (string s in in_ht.Keys) al.Add(s);
 1559:                     al.Sort();
 1560:                     foreach (string s in al) ht.Add(s, in_ht[s].ToString());
 1561:                 }
 1562:                 else ht = in_ht;
 1563:             }
 1564:             else ht = in_ht;
 1565:  
 1566:             return ht;
 1567:         }
 1568:  
 1569:         ////////////////////////////////////////////////////////////////////////////
 1570:  
 1571:         /// <summary>
 1572:         ///
 1573:         /// </summary>
 1574:         public static string HashtableValueString(Hashtable ht)
 1575:         {
 1576:             // 
 1577:             string si;
 1578:             StringBuilder sb;
 1579:  
 1580:             if (ht != null)
 1581:             {
 1582:                 if (ht.Count > 0)
 1583:                 {
 1584:                     sb = new StringBuilder(ht.Count);
 1585:                     sb.Length = 0;
 1586:  
 1587:                     foreach (string s in ht.Keys) sb.Append(ht[s].ToString() + "|");
 1588:                 }
 1589:                 else sb = new StringBuilder(1);
 1590:             }
 1591:             else sb = null;
 1592:  
 1593:             si = sb.ToString();
 1594:             si = si.Remove(si.Length - 1, 1);
 1595:  
 1596:             return si;
 1597:         }
 1598:  
 1599:         ////////////////////////////////////////////////////////////////////////////
 1600:  
 1601:         /// <summary>
 1602:         /// 
 1603:         /// </summary>
 1604:         public static string Match(string text, string regex)
 1605:         {
 1606:             string matchedText;
 1607:             Match m;
 1608:  
 1609:             m = Regex.Match(text, regex);
 1610:  
 1611:             if (m.Groups[1].Success) matchedText = m.Groups[1].Captures[0].Value;
 1612:             else matchedText = null;
 1613:  
 1614:             return matchedText;
 1615:         }
 1616:  
 1617:         ////////////////////////////////////////////////////////////////////////////
 1618:  
 1619:         /// <summary>
 1620:         /// 
 1621:         /// </summary>
 1622:         public static string MatchToLower(string s, string regex)
 1623:         {
 1624:             string t;
 1625:             Match m;
 1626:  
 1627:             m = Regex.Match(s, regex);
 1628:             if (m.Groups[1].Success) t = m.Groups[1].Captures[0].Value.ToLower();
 1629:             else t = null;
 1630:  
 1631:             return t;
 1632:         }
 1633:  
 1634:         ////////////////////////////////////////////////////////////////////////////
 1635:  
 1636:         /// <summary>
 1637:         ///
 1638:         /// </summary>
 1639:         public static bool IsRegexPatternValid(string pattern)
 1640:         {
 1641:             bool b;
 1642:  
 1643:             try
 1644:             {
 1645:                 new Regex(pattern);
 1646:  
 1647:                 b = true;
 1648:             }
 1649:             catch
 1650:             {
 1651:                 b = false;
 1652:             }
 1653:  
 1654:             return b;
 1655:         }
 1656:  
 1657: #if WFA
 1658: #else
 1659:         /*
 1660:         ////////////////////////////////////////////////////////////////////////////
 1661: 
 1662:         /// <summary>
 1663:         /// Store the current time in a cache variable
 1664:         /// </summary>
 1665:         public static void SetTimeSpan()
 1666:         {
 1667:             HttpRuntime httpRT = new HttpRuntime();
 1668:             Cache cache = HttpRuntime.Cache;
 1669: 
 1670:             cache["TimeSpan_Set"] = DateTime.UtcNow.AddHours(3).Ticks;
 1671:         }
 1672:         */
 1673:  
 1674:         /*
 1675:         ////////////////////////////////////////////////////////////////////////////
 1676: 
 1677:         /// <summary>
 1678:         /// Check if sec seconds had passed since timespan_set
 1679:         /// </summary>
 1680:         public static bool CheckTimeSpan(int sec)
 1681:         {
 1682:             bool b;
 1683:             long l;
 1684:             HttpRuntime httpRT = new HttpRuntime();
 1685:             Cache cache = HttpRuntime.Cache;
 1686: 
 1687:             if (cache["TimeSpan_Set"] == null) b = true;
 1688:             else
 1689:             {
 1690:                 l = (long)cache["TimeSpan_Set"];
 1691: 
 1692:                 if (DateTime.UtcNow.AddHours(3).AddSeconds(-sec).Ticks > l) b = true;
 1693:                 else b = false;
 1694:             }
 1695: 
 1696:             return b;
 1697:         }
 1698:         */
 1699:  
 1700:         /*
 1701:         ////////////////////////////////////////////////////////////////////////////
 1702: 
 1703:         /// <summary>
 1704:         /// Check if 1 sec seconds had passed since timespan_set
 1705:         /// </summary>
 1706:         public static bool CheckTimeSpan()
 1707:         {
 1708:             return CheckTimeSpan(1);
 1709:         }
 1710:         */
 1711:  
 1712: #endif
 1713:         ////////////////////////////////////////////////////////////////////////////
 1714:  
 1715:         /// <summary>
 1716:         /// Return the absolute path
 1717:         /// </summary>
 1718:         public static string AbsolutePath()
 1719:         {
 1720:             return AbsolutePath(false);
 1721:         }
 1722:  
 1723:         ////////////////////////////////////////////////////////////////////////////
 1724:  
 1725:         /// <summary>
 1726:         /// Return the absolute path to temp folder
 1727:         /// </summary>
 1728:         public static string AbsoluteTempPath()
 1729:         {
 1730:             return AbsolutePath(true);
 1731:         }
 1732:  
 1733:         ////////////////////////////////////////////////////////////////////////////
 1734:  
 1735:         /// <summary>
 1736:         /// Return the absolute path
 1737:         /// </summary>
 1738:         public static string AbsolutePath(bool temp_folder)
 1739:         {
 1740:             string path;
 1741:  
 1742: #if WFA
 1743:             if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed) path = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.DataDirectory + @"\";
 1744:             else path = AppDomain.CurrentDomain.BaseDirectory;
 1745: #else
 1746:             if (temp_folder) path = AppDomain.CurrentDomain.BaseDirectory.ToString() + @"app_data\temp\";
 1747:             else path = AppDomain.CurrentDomain.BaseDirectory.ToString();
 1748: #endif
 1749:  
 1750:             //if (path.IndexOf(@"\bin") >= 0) path = path.Remove(path.IndexOf(@"\bin"), path.Length - path.IndexOf(@"\bin"));
 1751:  
 1752:             return path;
 1753:         }
 1754:  
 1755:         ////////////////////////////////////////////////////////////////////////////
 1756:  
 1757:         /// <summary>
 1758:         /// Return the absolute path parent directory
 1759:         /// </summary>
 1760:         public static string AbsolutePathParent()
 1761:         {
 1762:             string s;
 1763:  
 1764:             s = AbsolutePath(false);
 1765:  
 1766:             s = s.Remove(s.Length - 1, 1);
 1767:  
 1768:             s = s.Substring(0, s.LastIndexOf(@"\")) + @"\";
 1769:  
 1770:             return s;
 1771:         }
 1772:  
 1773:         ////////////////////////////////////////////////////////////////////////////
 1774:  
 1775:         /// <summary>
 1776:         /// Return domain name from page Uri
 1777:         /// </summary>
 1778:         public static string BasicHost(Uri uri)
 1779:         {
 1780:             string url, domain, puny, tld;
 1781:  
 1782:             url = uri.Host;
 1783:             url = url.ToLower();
 1784:  
 1785:             if (uri.Host == "localhost")
 1786:             {
 1787:                 url = uri.Segments[1].Replace("/", "");
 1788:                 url = url.ToLower();
 1789:                 url = url.Replace("http://", "");
 1790:                 url = url.Replace("https://", "");
 1791:                 url = url.Replace("www.", "");
 1792:                 url = url.Replace("m.", "");
 1793:             }
 1794:  
 1795:             if (url.Contains("xn--"))
 1796:             {
 1797:                 // URL is punycode
 1798:                 if (url.Contains(".com")) tld = "com";
 1799:                 else if (url.Contains(".net")) tld = "net";
 1800:                 else if (url.Contains(".org")) tld = "org";
 1801:                 else tld = "?";
 1802:  
 1803:                 url = url.Replace(".com", "");
 1804:                 url = url.Replace(".net", "");
 1805:                 url = url.Replace(".org", "");
 1806:  
 1807:                 domain = Ia.Cl.Models.Punycode.Decode(url) + "." + tld;
 1808:                 puny = url + "." + tld;
 1809:             }
 1810:             else
 1811:             {
 1812:                 // URL is not punycode
 1813:                 domain = url;
 1814:                 puny = url;
 1815:             }
 1816:  
 1817:             return domain;
 1818:         }
 1819:  
 1820:         /*
 1821:         ////////////////////////////////////////////////////////////////////////////
 1822: 
 1823:         /// <summary>
 1824:         /// Return the absolute URL
 1825:         /// </summary>
 1826:         public static string AbsoluteUrl(Page p)
 1827:         {
 1828:             string url;
 1829:             Uri uri;
 1830: 
 1831:             uri = p.Request.Url;
 1832: 
 1833:             if (uri.Host == "localhost")
 1834:             {
 1835:                 url = uri.Authority; // +@"/" + uri.Segments[1].Replace("/", "");
 1836:                 url = url.ToLower();
 1837:                 url = url.Replace("http://", "");
 1838:                 url = url.Replace("www.", "");
 1839:             }
 1840:             else
 1841:             {
 1842:                 url = uri.Host;
 1843:                 url = url.ToLower();
 1844:                 url = url.Replace("http://", "");
 1845:                 url = url.Replace("www.", "");
 1846:             }
 1847: 
 1848:             return url;
 1849:         }
 1850:         */
 1851:  
 1852:         /*
 1853:         ////////////////////////////////////////////////////////////////////////////
 1854: 
 1855:         /// <summary>
 1856:         /// Return the URL of a file from the path
 1857:         /// </summary>
 1858:         public static string AbsolutePathUrl(Page page, string file)
 1859:         {
 1860:             string s, absoluteUrl, absolutePath;
 1861: 
 1862:             absoluteUrl = AbsoluteUrl(page) + "/";
 1863:             absolutePath = AbsolutePath();
 1864: 
 1865:             s = file.Replace(absolutePath, absoluteUrl);
 1866: 
 1867:             s = s.Replace(@"\", @"/");
 1868: 
 1869:             return page.ResolveClientUrl("~/" + s);
 1870:         }
 1871:         */
 1872:  
 1873:         /*
 1874:         ////////////////////////////////////////////////////////////////////////////
 1875:         ////////////////////////////////////////////////////////////////////////////
 1876: 
 1877:         /// <summary>
 1878:         /// Shows a client-side JavaScript alert in the browser.
 1879:         /// </summary>
 1880:         public static void JavasciptSrc(Page p, string relative_url_file)
 1881:         {
 1882:             // cleans the message to allow single quotation marks
 1883:             string script;
 1884: 
 1885:             relative_url_file = relative_url_file.Replace("'", "\\'");
 1886: 
 1887:             script = "<script type=\"text/javascript\" src=\"" + Ia.Cl.Models.Default.AbsoluteUrl(p) + @"/" + relative_url_file + "\"/>";
 1888: 
 1889:             // gets the executing web page
 1890:             Page page = HttpContext.Current.CurrentHandler as Page;
 1891: 
 1892:             // checks if the handler is a Page and that the script isn't allready on the Page
 1893:             if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
 1894:             {
 1895:                 page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", script);
 1896:             }
 1897:         }
 1898:         */
 1899:  
 1900:         ////////////////////////////////////////////////////////////////////////////
 1901:         ////////////////////////////////////////////////////////////////////////////
 1902:  
 1903:         /// <summary>
 1904:         /// Make line in proper title case
 1905:         /// </summary>
 1906:         public static string ToTitleCase(string input)
 1907:         {
 1908:             // Rules for Capitalization in Titles of Articles
 1909:             // http://grammar.yourdictionary.com/capitalization/rules-for-capitalization-in-titles.html#S9rKxG2G8gp88qcx.99http://grammar.yourdictionary.com/capitalization/rules-for-capitalization-in-titles.html
 1910:  
 1911:             // "Capitalize all words in titles of publications and documents, except a, an, the, at, by, for, in, of, on, to, up, and, as, but, or, and nor.
 1912:  
 1913:             // this is a ChatGPT generated code 2024-06
 1914:  
 1915:             string line;
 1916:  
 1917:             if (!string.IsNullOrEmpty(input))
 1918:             {
 1919:                 var textInfo = new CultureInfo("en-US", false).TextInfo;
 1920:  
 1921:                 string[] lowercaseWordList = { "a", "an", "and", "as", "at", "but", "by", "for", "if", "in", "nor", "of", "on", "or", "so", "the", "to", "up", "yet" };
 1922:  
 1923:                 // regex to match ordinals like 1st, 2nd, 3rd, 4th, etc.
 1924:                 var ordinalPattern = @"(\d+)(st|nd|rd|th)";
 1925:  
 1926:                 // regex for identifying Roman numerals
 1927:                 var romanNumeralPattern = @"\b(?=[MDCLXVI]+\b)M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})\b";
 1928:  
 1929:                 // regex pattern for identifying uppercased abbreviations
 1930:                 var upperAbbreviationPattern = @"\b[A-Z]{2,}\b";
 1931:  
 1932:                 line = Regex.Replace(input, @"\b[\w']+\b",
 1933:                     m =>
 1934:                     {
 1935:                         string word = m.Value;
 1936:  
 1937:                         if (Regex.IsMatch(word, romanNumeralPattern) || Regex.IsMatch(word, upperAbbreviationPattern)) line = word;
 1938:                         else if (lowercaseWordList.Contains(word.ToLower()) && m.Index != 0)
 1939:                         {
 1940:                             if (m.Index >= 2 && input[m.Index - 2] == ':') line = textInfo.ToTitleCase(word);
 1941:                             else line = word.ToLower(); // skip first word
 1942:                         }
 1943:                         else if (Regex.IsMatch(word, ordinalPattern)) line = word.ToLower();
 1944:                         else line = textInfo.ToTitleCase(word);
 1945:  
 1946:                         return line;
 1947:                     });
 1948:             }
 1949:             else line = string.Empty;
 1950:  
 1951:             return line;
 1952:         }
 1953:  
 1954:         ////////////////////////////////////////////////////////////////////////////
 1955:  
 1956:         /// <summary>
 1957:         /// Make the first letter of a word an upper letter
 1958:         /// </summary>
 1959:         public static string FirstLetterToUpper(string s)
 1960:         {
 1961:             string u;
 1962:  
 1963:             u = s.Substring(0, 1);
 1964:             return u.ToUpper() + s.Remove(0, 1);
 1965:         }
 1966:  
 1967:         ////////////////////////////////////////////////////////////////////////////
 1968:  
 1969:         /// <summary>
 1970:         /// Make the first letter of all words an upper letter and remove underscores
 1971:         /// </summary>
 1972:         public static string FirstWordLetterToUpperAndRemoveUnderscore(string line)
 1973:         {
 1974:             string u, v;
 1975:  
 1976:             v = "";
 1977:  
 1978:             line = RemoveUnderscore(line);
 1979:  
 1980:             foreach (string s in line.Split(' '))
 1981:             {
 1982:                 u = s.Substring(0, 1);
 1983:                 v += u.ToUpper() + s.Remove(0, 1) + " ";
 1984:             }
 1985:  
 1986:             if (v.Length > 0) v = v.Remove(v.Length - 1, 1);
 1987:  
 1988:             return v;
 1989:         }
 1990:  
 1991:         ////////////////////////////////////////////////////////////////////////////
 1992:  
 1993:         /// <summary>
 1994:         /// Convert the string to Pascal case.
 1995:         /// </summary>
 1996:         /// <remarks>http://csharphelper.com/blog/2014/10/convert-between-pascal-case-camel-case-and-proper-case-in-c/</remarks>
 1997:         public static string ToPascalCase(this string s)
 1998:         {
 1999:             // If there are 0 or 1 characters, just return the string.
 2000:             if (s == null) return s;
 2001:             if (s.Length < 2) return s.ToUpper();
 2002:  
 2003:             // Split the string into words.
 2004:             string[] words = s.Split(
 2005:                 new char[] { },
 2006:                 StringSplitOptions.RemoveEmptyEntries);
 2007:  
 2008:             // Combine the words.
 2009:             string result = "";
 2010:             foreach (string word in words)
 2011:             {
 2012:                 result +=
 2013:                     word.Substring(0, 1).ToUpper() +
 2014:                     word.Substring(1);
 2015:             }
 2016:  
 2017:             return result;
 2018:         }
 2019:  
 2020:         ////////////////////////////////////////////////////////////////////////////
 2021:  
 2022:         /// <summary>
 2023:         /// Convert the string to camel case.
 2024:         /// </summary>
 2025:         /// <remarks>http://csharphelper.com/blog/2014/10/convert-between-pascal-case-camel-case-and-proper-case-in-c/</remarks>
 2026:         public static string ProperToCamelCase(this string s)
 2027:         {
 2028:             // If there are 0 or 1 characters, just return the string.
 2029:             if (s == null || s.Length < 2)
 2030:                 return s;
 2031:  
 2032:             // Split the string into words.
 2033:             string[] words = s.Split(
 2034:                 new char[] { },
 2035:                 StringSplitOptions.RemoveEmptyEntries);
 2036:  
 2037:             // Combine the words.
 2038:             string result = words[0].ToLower();
 2039:             for (int i = 1; i < words.Length; i++)
 2040:             {
 2041:                 result +=
 2042:                     words[i].Substring(0, 1).ToUpper() +
 2043:                     words[i].Substring(1);
 2044:             }
 2045:  
 2046:             return result;
 2047:         }
 2048:  
 2049:         ////////////////////////////////////////////////////////////////////////////
 2050:  
 2051:         /// <summary>
 2052:         /// Capitalize the first character and add a space before each capitalized letter (except the first character).
 2053:         /// </summary>
 2054:         /// <remarks>http://csharphelper.com/blog/2014/10/convert-between-pascal-case-camel-case-and-proper-case-in-c/</remarks>
 2055:         public static string CamelToProperCase(this string s)
 2056:         {
 2057:             // If there are 0 or 1 characters, just return the string.
 2058:             if (s == null) return s;
 2059:             if (s.Length < 2) return s.ToUpper();
 2060:  
 2061:             // Start with the first character.
 2062:             string result = s.Substring(0, 1).ToUpper();
 2063:  
 2064:             // Add the remaining characters.
 2065:             for (int i = 1; i < s.Length; i++)
 2066:             {
 2067:                 if (char.IsUpper(s[i])) result += " ";
 2068:                 result += s[i];
 2069:             }
 2070:  
 2071:             return result;
 2072:         }
 2073:  
 2074:         ////////////////////////////////////////////////////////////////////////////
 2075:  
 2076:         /// <summary>
 2077:         /// Convert caps delimited to underscore, lower case string
 2078:         /// </summary>
 2079:         /// <remarks>http://stackoverflow.com/questions/5796383/insert-spaces-between-words-on-a-camel-cased-token</remarks>
 2080:         public static string CapsDelimitedToUnderscoreLowercase(this string s)
 2081:         {
 2082:             string u;
 2083:  
 2084:             u = Regex.Replace(s, "(\\B[A-Z])", "_$1");
 2085:  
 2086:             return u.ToLower();
 2087:         }
 2088:  
 2089:         ////////////////////////////////////////////////////////////////////////////
 2090:  
 2091:         /// <summary>
 2092:         /// Remove underscores
 2093:         /// </summary>
 2094:         public static string RemoveUnderscore(string line)
 2095:         {
 2096:             string u;
 2097:  
 2098:             u = line.Replace('_', ' ');
 2099:  
 2100:             return u;
 2101:         }
 2102:  
 2103:         ////////////////////////////////////////////////////////////////////////////
 2104:         ////////////////////////////////////////////////////////////////////////////
 2105:  
 2106:         /// <summary>
 2107:         /// Regex that defines email
 2108:         /// </summary>
 2109:         public static string EmailRegex()
 2110:         {
 2111:             // http://www.regular-expressions.info/email.html
 2112:             string s;
 2113:  
 2114:             s = @"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)\b";
 2115:  
 2116:             // Text="Invalid e-mail" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" runat="server" />
 2117:  
 2118:             return s;
 2119:         }
 2120:  
 2121:         ////////////////////////////////////////////////////////////////////////////
 2122:  
 2123:         /// <summary>
 2124:         ///
 2125:         /// </summary>
 2126:         public static bool IsEmail(string s)
 2127:         {
 2128:             // return true if argument is an email
 2129:             bool b;
 2130:  
 2131:             b = false;
 2132:  
 2133:             if (Regex.IsMatch(s, Ia.Cl.Models.Default.EmailRegex(), RegexOptions.IgnoreCase)) b = true;
 2134:  
 2135:             return b;
 2136:         }
 2137:  
 2138:         ////////////////////////////////////////////////////////////////////////////
 2139:  
 2140:         /// <summary>
 2141:         ///
 2142:         /// </summary>
 2143:         public static string Decimal(double d, int dec)
 2144:         {
 2145:             // 
 2146:             string s;
 2147:  
 2148:             if (dec == 3) s = string.Format("{0:0.000}", d);
 2149:             else if (dec == 2) s = string.Format("{0:0.00}", d);
 2150:             else if (dec == 1) s = string.Format("{0:0.0}", d);
 2151:             else s = d.ToString();
 2152:  
 2153:             return s;
 2154:         }
 2155:  
 2156:         ////////////////////////////////////////////////////////////////////////////
 2157:  
 2158:         /// <summary>
 2159:         /// Reverse a string
 2160:         /// </summary>
 2161:         public static string ReverseString(string t)
 2162:         {
 2163:             string s;
 2164:  
 2165:             s = "";
 2166:  
 2167:             foreach (char c in t.ToCharArray()) s = c + " " + s;
 2168:  
 2169:             return s;
 2170:         }
 2171:  
 2172:         ////////////////////////////////////////////////////////////////////////////
 2173:         ////////////////////////////////////////////////////////////////////////////
 2174:  
 2175:         /// <summary>
 2176:         /// Convent the data content of a DataSet to an XmlDocument object for use in API Services.
 2177:         /// <param name="ds">DataSet to convert to XmlDocument</param>
 2178:         /// <returns>XmlDocument</returns>
 2179:         /// </summary>
 2180:  
 2181:         public static XmlDocument DataSetToXmlDocument(DataSet ds)
 2182:         {
 2183:             return DataSetToXmlDocument(ds, null);
 2184:         }
 2185:  
 2186:         ////////////////////////////////////////////////////////////////////////////
 2187:  
 2188:         /// <summary>
 2189:         /// Convent the data content of a DataSet to an XmlDocument object for use in API Services.
 2190:         /// </summary>
 2191:         public static XmlDocument DataSetToXmlDocument(DataSet ds, string item_name)
 2192:         {
 2193:             XmlText xt;
 2194:             XmlElement set_xe, table_xe, row_xe, xe;
 2195:             XmlDeclaration xde;
 2196:             XmlDocument xd;
 2197:  
 2198:             xd = new XmlDocument();
 2199:  
 2200:             if (ds != null)
 2201:             {
 2202:                 xde = xd.CreateXmlDeclaration("1.0", "utf-8", null);
 2203:  
 2204:                 // create root element
 2205:                 if (ds.DataSetName.Length > 0) set_xe = xd.CreateElement(ds.DataSetName);
 2206:                 else set_xe = xd.CreateElement("set");
 2207:  
 2208:                 xd.InsertBefore(xde, xd.DocumentElement);
 2209:                 xd.AppendChild(set_xe);
 2210:  
 2211:                 if (ds.Tables.Count > 0)
 2212:                 {
 2213:                     foreach (DataTable dt in ds.Tables)
 2214:                     {
 2215:                         // create table element
 2216:                         if (dt.TableName.Length > 0) table_xe = xd.CreateElement(dt.TableName);
 2217:                         else table_xe = xd.CreateElement("table");
 2218:  
 2219:                         set_xe.AppendChild(table_xe);
 2220:  
 2221:                         if (dt.Rows.Count > 0)
 2222:                         {
 2223:                             foreach (DataRow r in dt.Rows)
 2224:                             {
 2225:                                 // create a new row and add it to the root node
 2226:                                 if (item_name == null) item_name = "row";
 2227:                                 row_xe = xd.CreateElement(item_name);
 2228:  
 2229:                                 table_xe.AppendChild(row_xe);
 2230:  
 2231:                                 foreach (DataColumn dc in dt.Columns)
 2232:                                 {
 2233:                                     xe = xd.CreateElement(dc.ColumnName);
 2234:  
 2235:                                     xt = xd.CreateTextNode(r[dc.ColumnName].ToString());
 2236:  
 2237:                                     xe.AppendChild(xt);
 2238:  
 2239:                                     row_xe.AppendChild(xe);
 2240:                                 }
 2241:                             }
 2242:                         }
 2243:                     }
 2244:                 }
 2245:                 else
 2246:                 {
 2247:                 }
 2248:             }
 2249:             else
 2250:             {
 2251:             }
 2252:  
 2253:             return xd;
 2254:         }
 2255:  
 2256:         ////////////////////////////////////////////////////////////////////////////
 2257:  
 2258:         /// <summary>
 2259:         /// Convert the data content of a DataTable to an XmlDocument object for use in API Services.
 2260:         /// <param name="dt">DataTable to convert to XmlDocument</param>
 2261:         /// <returns>XmlDocument</returns>
 2262:         /// </summary>
 2263:  
 2264:         public static XmlDocument DataTableToXmlDocument(DataTable dt)
 2265:         {
 2266:             return DataTableToXmlDocument(dt, null);
 2267:         }
 2268:  
 2269:         ////////////////////////////////////////////////////////////////////////////
 2270:  
 2271:         /// <summary>
 2272:         /// Convert the data content of a DataTable to an XmlDocument object for use in API Services.
 2273:         /// </summary>
 2274:         public static XmlDocument DataTableToXmlDocument(DataTable dt, string itemName)
 2275:         {
 2276:             XmlText xt;
 2277:             XmlElement table_xe, row_xe, xe;
 2278:             XmlDeclaration xde;
 2279:             XmlDocument xd;
 2280:  
 2281:             xd = new XmlDocument();
 2282:  
 2283:             if (dt != null)
 2284:             {
 2285:                 xde = xd.CreateXmlDeclaration("1.0", "utf-8", null);
 2286:  
 2287:                 // create root element
 2288:                 if (dt.TableName.Length > 0) table_xe = xd.CreateElement(dt.TableName);
 2289:                 else table_xe = xd.CreateElement("table");
 2290:  
 2291:                 xd.InsertBefore(xde, xd.DocumentElement);
 2292:                 xd.AppendChild(table_xe);
 2293:  
 2294:                 if (dt.Rows.Count > 0)
 2295:                 {
 2296:                     foreach (DataRow r in dt.Rows)
 2297:                     {
 2298:                         // create a new row and add it to the root node
 2299:                         if (itemName == null) itemName = "row";
 2300:                         row_xe = xd.CreateElement(itemName);
 2301:  
 2302:                         table_xe.AppendChild(row_xe);
 2303:  
 2304:                         foreach (DataColumn dc in dt.Columns)
 2305:                         {
 2306:                             xe = xd.CreateElement(dc.ColumnName);
 2307:  
 2308:                             xt = xd.CreateTextNode(r[dc.ColumnName].ToString());
 2309:  
 2310:                             xe.AppendChild(xt);
 2311:  
 2312:                             row_xe.AppendChild(xe);
 2313:                         }
 2314:                     }
 2315:                 }
 2316:             }
 2317:             else
 2318:             {
 2319:             }
 2320:  
 2321:             return xd;
 2322:         }
 2323:  
 2324:         ////////////////////////////////////////////////////////////////////////////
 2325:  
 2326:         /// <summary>
 2327:         /// Convert the data content of a DataTable to an XDocument object
 2328:         /// <param name="dt">DataTable to convert to XDocument</param>
 2329:         /// <returns>XDocument</returns>
 2330:         /// </summary>
 2331:  
 2332:         public static XDocument DataTableToXDocument(DataTable dt)
 2333:         {
 2334:             return DataTableToXDocument(dt, null);
 2335:         }
 2336:  
 2337:         ////////////////////////////////////////////////////////////////////////////
 2338:  
 2339:         /// <summary>
 2340:         /// Convert the data content of a DataTable to an XDocument object.
 2341:         /// </summary>
 2342:         public static XDocument DataTableToXDocument(DataTable dt, string itemName)
 2343:         {
 2344:             XElement tableXElement, rowXElement, xe;
 2345:             XDeclaration xde;
 2346:             XDocument xd;
 2347:  
 2348:             if (dt != null)
 2349:             {
 2350:                 xde = new XDeclaration("1.0", "utf-8", null);
 2351:  
 2352:                 // create root element
 2353:                 if (dt.TableName.Length > 0) tableXElement = new XElement(dt.TableName);
 2354:                 else tableXElement = new XElement("table");
 2355:  
 2356:                 if (dt.Rows.Count > 0)
 2357:                 {
 2358:                     foreach (DataRow r in dt.Rows)
 2359:                     {
 2360:                         // create a new row and add it to the root node
 2361:                         if (itemName == null) itemName = "row";
 2362:                         rowXElement = new XElement(itemName, "");
 2363:  
 2364:                         tableXElement.Add(rowXElement);
 2365:  
 2366:                         foreach (DataColumn dc in dt.Columns)
 2367:                         {
 2368:                             xe = new XElement(dc.ColumnName, r[dc.ColumnName].ToString());
 2369:  
 2370:                             rowXElement.Add(xe);
 2371:                         }
 2372:                     }
 2373:                 }
 2374:  
 2375:                 xd = new XDocument(xde, tableXElement);
 2376:             }
 2377:             else
 2378:             {
 2379:                 xd = null;
 2380:             }
 2381:  
 2382:             return xd;
 2383:         }
 2384:  
 2385:         ////////////////////////////////////////////////////////////////////////////
 2386:         ////////////////////////////////////////////////////////////////////////////
 2387:  
 2388:         /// <summary>
 2389:         /// Takes an XmlNodeList with text and value, and change them into a DataTable usable for databinding with controls
 2390:         /// </summary>
 2391:         public static DataTable XmlNodeListToDataTable(XmlNodeList xnl, string value, string text)
 2392:         {
 2393:             DataTable dt;
 2394:             DataRow dr;
 2395:  
 2396:             dt = new DataTable();
 2397:             dt.Columns.Add(value);
 2398:  
 2399:             if (value != text) dt.Columns.Add(text);
 2400:  
 2401:             foreach (XmlNode n in xnl)
 2402:             {
 2403:                 dr = dt.NewRow();
 2404:                 dr[value] = n.Attributes[value].Value;
 2405:                 if (value != text) dr[text] = n.Attributes[text].Value;
 2406:                 dt.Rows.Add(dr);
 2407:             }
 2408:  
 2409:             return dt;
 2410:         }
 2411:  
 2412:         ////////////////////////////////////////////////////////////////////////////
 2413:  
 2414:         /// <summary>
 2415:         /// Takes an XmlNodeList with text and value, and change them into a DataTable usable for databinding with controls
 2416:         /// </summary>
 2417:         public static DataTable XmlNodeListToDataTable(XmlNodeList xnl, string id, string text, string url)
 2418:         {
 2419:             DataTable dt;
 2420:             DataRow dr;
 2421:  
 2422:             dt = new DataTable();
 2423:             dt.Columns.Add(id);
 2424:             dt.Columns.Add(text);
 2425:             dt.Columns.Add(url);
 2426:  
 2427:             foreach (XmlNode n in xnl)
 2428:             {
 2429:                 dr = dt.NewRow();
 2430:                 dr[id] = n.Attributes[id].Value;
 2431:                 dr[text] = n.Attributes[text].Value;
 2432:                 dr[url] = n.Attributes[url].Value;
 2433:                 dt.Rows.Add(dr);
 2434:             }
 2435:  
 2436:             return dt;
 2437:         }
 2438:  
 2439:         ////////////////////////////////////////////////////////////////////////////
 2440:  
 2441:         /// <summary>
 2442:         /// Takes an XmlNodeList with text and value, and extra initial entries, and change them into a DataTable usable for databinding with controls
 2443:         /// </summary>
 2444:         public static DataTable XmlNodeListToDataTable(XmlNodeList xnl, string value, string text, string init_value, string init_text)
 2445:         {
 2446:             DataTable dt;
 2447:             DataRow dr;
 2448:  
 2449:             dt = new DataTable();
 2450:             dt.Columns.Add(value);
 2451:             dt.Columns.Add(text);
 2452:  
 2453:             dr = dt.NewRow();
 2454:             dr[value] = init_value;
 2455:             dr[text] = init_text;
 2456:             dt.Rows.Add(dr);
 2457:  
 2458:             foreach (XmlNode n in xnl)
 2459:             {
 2460:                 dr = dt.NewRow();
 2461:                 dr[value] = n.Attributes[value].Value;
 2462:                 dr[text] = n.Attributes[text].Value;
 2463:                 dt.Rows.Add(dr);
 2464:             }
 2465:  
 2466:             return dt;
 2467:         }
 2468:  
 2469:         ////////////////////////////////////////////////////////////////////////////
 2470:  
 2471:         /// <summary>
 2472:         /// Convert XmlDocument to a tab indented simple text format suitable for simple text and emails
 2473:         /// </summary>
 2474:         /// <remarks>http://stackoverflow.com/questions/10980237/xml-to-text-convert</remarks>
 2475:         public static string XmlDocumentToTabIndentedText(XmlDocument xmlDocument)
 2476:         {
 2477:             string s, name, text;
 2478:             StringBuilder stringBuilder = new StringBuilder();
 2479:  
 2480:             stringBuilder.AppendLine("===================");
 2481:  
 2482:             name = CamelToProperCase(xmlDocument.DocumentElement.Name);
 2483:  
 2484:             stringBuilder.AppendLine(name);
 2485:  
 2486:             s = XmlDocumentToTabIndentedTextIterator(xmlDocument.DocumentElement, out text);
 2487:  
 2488:             stringBuilder.AppendLine(s);
 2489:  
 2490:             stringBuilder.AppendLine("===================");
 2491:  
 2492:             return stringBuilder.ToString();
 2493:         }
 2494:  
 2495:         private static string XmlDocumentToTabIndentedTextIterator(XmlNode xmlNode, out string text)
 2496:         {
 2497:             string s, name;
 2498:             StringBuilder stringBuilder = new StringBuilder();
 2499:  
 2500:             text = string.Empty;
 2501:  
 2502:             if (xmlNode.NodeType == XmlNodeType.Element)
 2503:             {
 2504:                 foreach (XmlNode node in xmlNode.ChildNodes)
 2505:                 {
 2506:                     s = XmlDocumentToTabIndentedTextIterator(node, out text);
 2507:  
 2508:                     name = CamelToProperCase(node.Name);
 2509:  
 2510:                     if (!string.IsNullOrEmpty(text))
 2511:                     {
 2512:                         stringBuilder.AppendLine("\t" + name + ": " + text);
 2513:                     }
 2514:                     else stringBuilder.AppendLine("\t" + name);
 2515:                 }
 2516:             }
 2517:             else if (xmlNode.NodeType == XmlNodeType.Text)
 2518:             {
 2519:                 text = xmlNode.InnerText.Trim();
 2520:             }
 2521:             else
 2522:             {
 2523:  
 2524:             }
 2525:  
 2526:             return stringBuilder.ToString().TrimEnd();
 2527:         }
 2528:  
 2529:         ////////////////////////////////////////////////////////////////////////////
 2530:         ////////////////////////////////////////////////////////////////////////////
 2531:  
 2532: #if WFA
 2533:         ////////////////////////////////////////////////////////////////////////////
 2534:  
 2535:         /// <summary>
 2536:         /// Return the MAC Address of the computer
 2537:         /// </summary>
 2538:         public static string Get_MAC_Address()
 2539:         {
 2540:             // This will support IPv4 and IPv6.
 2541:  
 2542:             string mac_address;
 2543:             System.Net.NetworkInformation.NetworkInterface[] nics;
 2544:  
 2545:             nics = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
 2546:  
 2547:             mac_address = string.Empty;
 2548:  
 2549:             foreach (System.Net.NetworkInformation.NetworkInterface adapter in nics)
 2550:             {
 2551:                 if (mac_address == string.Empty)// only return MAC Address from first card  
 2552:                 {
 2553:                     System.Net.NetworkInformation.IPInterfaceProperties properties = adapter.GetIPProperties();
 2554:  
 2555:                     mac_address = adapter.GetPhysicalAddress().ToString();
 2556:                 }
 2557:             }
 2558:  
 2559:             return mac_address;
 2560:         }
 2561: #endif
 2562:         ////////////////////////////////////////////////////////////////////////////
 2563:  
 2564:         /// <summary>
 2565:         ///
 2566:         /// </summary>
 2567:         public static bool DownloadFile(string url, string filePathName)
 2568:         {
 2569:             try
 2570:             {
 2571:                 HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, url);
 2572:  
 2573:                 requestMessage.Headers.Clear();
 2574:                 requestMessage.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
 2575:  
 2576:                 HttpResponseMessage response = httpClient.SendAsync(requestMessage).Result;
 2577:  
 2578:                 //using HttpResponseMessage response = client.GetAsync(url).Result;
 2579:                 response.EnsureSuccessStatusCode();
 2580:                 string responseBody = response.Content.ReadAsStringAsync().Result;
 2581:  
 2582:                 //string responseBody2 = await client.GetStringAsync(uri);
 2583:  
 2584:                 System.IO.File.WriteAllText(filePathName, responseBody);
 2585:                 //Console.WriteLine(responseBody);
 2586:             }
 2587:             catch (HttpRequestException e)
 2588:             {
 2589:                 Console.WriteLine("\nException Caught!");
 2590:                 Console.WriteLine("Message :{0} ", e.Message);
 2591:             }
 2592:  
 2593:             return true;
 2594:         }
 2595:  
 2596:         ////////////////////////////////////////////////////////////////////////////
 2597:  
 2598:         /// <summary>
 2599:         ///
 2600:         /// </summary>
 2601:         public static bool DownloadFile(string url, string userName, string password, string fileName)
 2602:         {
 2603:             bool b;
 2604:  
 2605:             try
 2606:             {
 2607:                 using (System.Net.WebClient webClient = new System.Net.WebClient())
 2608:                 {
 2609:                     webClient.Credentials = new System.Net.NetworkCredential(userName, password);
 2610:  
 2611:                     webClient.DownloadFile(url, fileName);
 2612:                 }
 2613:  
 2614:                 b = true;
 2615:             }
 2616:             catch (Exception)
 2617:             {
 2618:                 b = false;
 2619:             }
 2620:  
 2621:             return b;
 2622:         }
 2623:  
 2624:         ////////////////////////////////////////////////////////////////////////////
 2625:  
 2626:         /// <summary>
 2627:         /// Regular Expression Guid Match
 2628:         /// </summary>
 2629:         /// <remarks> 
 2630:         /// http://www.geekzilla.co.uk/view8AD536EF-BC0D-427F-9F15-3A1BC663848E.htm
 2631:         /// </remarks>
 2632:         public static bool IsGuid(string s)
 2633:         {
 2634:             bool b = false;
 2635:  
 2636:             if (!string.IsNullOrEmpty(s))
 2637:             {
 2638:                 Regex r = new Regex(@"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$");
 2639:  
 2640:                 b = r.IsMatch(s);
 2641:             }
 2642:  
 2643:             return b;
 2644:         }
 2645:  
 2646:         ////////////////////////////////////////////////////////////////////////////
 2647:  
 2648:         /// <summary>
 2649:         /// C# to convert a string to a byte array.
 2650:         /// </summary>
 2651:         private static byte[] ConvertStringToByteArray(string str)
 2652:         {
 2653:             System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
 2654:             return encoding.GetBytes(str);
 2655:  
 2656:             /*
 2657:         // C# to convert a byte array to a string.
 2658:         byte [] dBytes = ...
 2659:         string str;
 2660:         System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
 2661:         str = enc.GetString(dBytes);
 2662:              */
 2663:         }
 2664:  
 2665:         ////////////////////////////////////////////////////////////////////////////
 2666:  
 2667:         /// <summary>
 2668:         /// C# to convert an alphnumeric string to an integer
 2669:         /// </summary>
 2670:         public static int AlphanumericStringToInt(string s)
 2671:         {
 2672:             int i;
 2673:             System.Text.Encoding ascii;
 2674:             Byte[] encodedBytes;
 2675:  
 2676:             i = 0;
 2677:             ascii = System.Text.Encoding.ASCII;
 2678:             encodedBytes = ascii.GetBytes(s);
 2679:  
 2680:             foreach (Byte b in encodedBytes) i += b;
 2681:  
 2682:             return i;
 2683:         }
 2684:  
 2685:         ////////////////////////////////////////////////////////////////////////////
 2686:  
 2687:         /// <summary>
 2688:         ///
 2689:         /// </summary>
 2690:         public static int IncrementArrayListIndexOrRestart(ArrayList list, int currentIndex)
 2691:         {
 2692:             int newIndex;
 2693:  
 2694:             if (currentIndex < list.Count - 1) newIndex = ++currentIndex;
 2695:             else newIndex = 0;
 2696:  
 2697:             return newIndex;
 2698:         }
 2699:  
 2700:         ////////////////////////////////////////////////////////////////////////////
 2701:  
 2702:         /// <summary>
 2703:         ///
 2704:         /// </summary>
 2705:         public static int IncrementListIndexOrRestart<T>(List<T> list, int currentIndex)
 2706:         {
 2707:             int newIndex;
 2708:  
 2709:             if (currentIndex < list.Count - 1) newIndex = ++currentIndex;
 2710:             else newIndex = 0;
 2711:  
 2712:             return newIndex;
 2713:         }
 2714:  
 2715:         ////////////////////////////////////////////////////////////////////////////
 2716:  
 2717:         /// <summary>
 2718:         ///
 2719:         /// </summary>
 2720:         public static void IncrementIndexOrReset(int listCount, ref int currentIndex)
 2721:         {
 2722:             currentIndex = (currentIndex < listCount - 1) ? ++currentIndex : 0;
 2723:         }
 2724:  
 2725:         /*
 2726:         ////////////////////////////////////////////////////////////////////////////
 2727: 
 2728:         /// <summary>
 2729:         ///
 2730:         /// </summary>
 2731:         public static void ShuffleArrayList(ArrayList al)
 2732:         {
 2733:             for (int inx = al.Count - 1; inx > 0; --inx)
 2734:             {
 2735:                 int position = r.Next(inx);
 2736:                 object temp = al[inx];
 2737:                 al[inx] = al[position];
 2738:                 al[position] = temp;
 2739:             }
 2740:         }
 2741:          */
 2742:  
 2743:         ////////////////////////////////////////////////////////////////////////////
 2744:  
 2745:         /// <summary>
 2746:         ///
 2747:         /// </summary>
 2748:         public static ArrayList ShuffleArrayList(ArrayList inputList)
 2749:         {
 2750:             ArrayList randomList = new ArrayList();
 2751:  
 2752:             int randomIndex = 0;
 2753:  
 2754:             while (inputList.Count > 0)
 2755:             {
 2756:                 randomIndex = random.Next(0, inputList.Count); //Choose a random object in the list
 2757:                 randomList.Add(inputList[randomIndex]); //add it to the new, random list
 2758:                 inputList.RemoveAt(randomIndex); //remove to avoid duplicates
 2759:             }
 2760:  
 2761:             return randomList; //return the new random list
 2762:         }
 2763:  
 2764:         ////////////////////////////////////////////////////////////////////////////
 2765:  
 2766:         /// <summary>
 2767:         ///
 2768:         /// </summary>
 2769:         public static ArrayList IntegerListRange(ArrayList a_al)
 2770:         {
 2771:             // this will take an ArrayList of integer, remove duplicates, sort, then construct an ArrayList with ranges defined, if available. For example
 2772:             // a_al = [1,2,4,6,7,15,20,21,22,34,35,36,38]
 2773:             // b_al = [1,2,4,6,7,15,20-22,34-36,38]
 2774:  
 2775:             bool range, range_old;
 2776:             int u, v;
 2777:             int start, end;
 2778:             ArrayList b_al, c_al;
 2779:             Hashtable ht;
 2780:  
 2781:             // a_al = [1,2,4,6,7,15,20,21,22,34,35,36,38]
 2782:             // b_al = [1,2,4,6,7,15,20-22,34-36,38]
 2783:  
 2784:             start = end = 0;
 2785:  
 2786:             b_al = new ArrayList(a_al.Count + 1);
 2787:             c_al = new ArrayList(a_al.Count + 1);
 2788:  
 2789:             if (a_al.Count > 0)
 2790:             {
 2791:                 // remove duplicates
 2792:                 ht = new Hashtable(a_al.Count + 1);
 2793:  
 2794:                 foreach (int i in a_al) ht[i] = 1;
 2795:  
 2796:                 foreach (int i in ht.Keys) b_al.Add(i);
 2797:  
 2798:                 // sort
 2799:                 b_al.Sort();
 2800:  
 2801:                 if (b_al.Count > 0)
 2802:                 {
 2803:                     range = range_old = false;
 2804:                     u = (int)b_al[0];
 2805:  
 2806:                     for (int i = 1; i <= b_al.Count; i++)
 2807:                     {
 2808:                         if (i < b_al.Count) v = (int)b_al[i];
 2809:                         else v = (int)b_al[i - 1];
 2810:  
 2811:                         if (v - u == 1)
 2812:                         {
 2813:                             if (range) end = v;
 2814:                             else
 2815:                             {
 2816:                                 start = u;
 2817:                                 range = true;
 2818:                             }
 2819:                         }
 2820:                         else
 2821:                         {
 2822:                             if (range)
 2823:                             {
 2824:                                 end = u;
 2825:                                 range = false;
 2826:                             }
 2827:                             else c_al.Add(u.ToString());
 2828:                         }
 2829:  
 2830:                         if (range != range_old && range == false)
 2831:                         {
 2832:                             if (end - start == 1)
 2833:                             {
 2834:                                 c_al.Add(start.ToString());
 2835:                                 c_al.Add(end.ToString());
 2836:                             }
 2837:                             else c_al.Add(start + "-" + end);
 2838:                         }
 2839:  
 2840:                         u = v;
 2841:                         range_old = range;
 2842:                     }
 2843:                 }
 2844:                 else
 2845:                 {
 2846:  
 2847:                 }
 2848:             }
 2849:             else
 2850:             {
 2851:             }
 2852:  
 2853:             return c_al;
 2854:         }
 2855:  
 2856:         ////////////////////////////////////////////////////////////////////////////
 2857:  
 2858:         /// <summary>
 2859:         /// Generate a list of start-end count optimized to read around count number of values from passed number string list. 
 2860:         /// </summary>
 2861:         public static List<Tuple<string, string>> OptimizedStartEndRangeTupleList(List<string> list, int count)
 2862:         {
 2863:             bool done;
 2864:             int c;
 2865:             string start, end;
 2866:             Tuple<string, string> tuple;
 2867:             List<Tuple<string, string>> tupleList;
 2868:  
 2869:             tupleList = new List<Tuple<string, string>>();
 2870:  
 2871:             done = false;
 2872:             c = 0;
 2873:             start = string.Empty;
 2874:  
 2875:             if (list.Count > 0 && count > 0)
 2876:             {
 2877:                 foreach (string s in list)
 2878:                 {
 2879:                     if (c == 0)
 2880:                     {
 2881:                         start = s;
 2882:                         done = false;
 2883:                     }
 2884:  
 2885:                     if (c == count - 1)
 2886:                     {
 2887:                         end = s;
 2888:  
 2889:                         tuple = new Tuple<string, string>(start, end);
 2890:  
 2891:                         tupleList.Add(tuple);
 2892:  
 2893:                         done = true;
 2894:                         c = 0;
 2895:                     }
 2896:                     else c++;
 2897:                 }
 2898:  
 2899:                 if (!done)
 2900:                 {
 2901:                     end = list[list.Count - 1];
 2902:  
 2903:                     tuple = new Tuple<string, string>(start, end);
 2904:  
 2905:                     tupleList.Add(tuple);
 2906:                 }
 2907:             }
 2908:             else throw new System.ArgumentException("List empty or range too short. ");
 2909:  
 2910:             tupleList.Sort();
 2911:  
 2912:             return tupleList;
 2913:         }
 2914:  
 2915:         ////////////////////////////////////////////////////////////////////////////
 2916:  
 2917:         /// <summary>
 2918:         /// Generate a list of start-end count optimized to read around count number of values from passed list. 
 2919:         /// </summary>
 2920:         public static List<Tuple<int, int>> OptimizedStartEndRangeTupleList(List<int> list, int count)
 2921:         {
 2922:             bool done;
 2923:             int c, start, end;
 2924:             Tuple<int, int> tuple;
 2925:             List<Tuple<int, int>> tupleList;
 2926:  
 2927:             tupleList = new List<Tuple<int, int>>();
 2928:  
 2929:             done = false;
 2930:             c = start = 0;
 2931:  
 2932:             if (list.Count > 0 && count > 0)
 2933:             {
 2934:                 foreach (int i in list)
 2935:                 {
 2936:                     if (c == 0)
 2937:                     {
 2938:                         start = i;
 2939:                         done = false;
 2940:                     }
 2941:  
 2942:                     if (c == count - 1)
 2943:                     {
 2944:                         end = i;
 2945:  
 2946:                         tuple = new Tuple<int, int>(start, end);
 2947:  
 2948:                         tupleList.Add(tuple);
 2949:  
 2950:                         done = true;
 2951:                         c = 0;
 2952:                     }
 2953:                     else c++;
 2954:                 }
 2955:  
 2956:                 if (!done)
 2957:                 {
 2958:                     end = list[list.Count - 1];
 2959:  
 2960:                     tuple = new Tuple<int, int>(start, end);
 2961:  
 2962:                     tupleList.Add(tuple);
 2963:                 }
 2964:             }
 2965:             else throw new System.ArgumentException("List empty or range too short. ");
 2966:  
 2967:             tupleList.Sort();
 2968:  
 2969:             return tupleList;
 2970:         }
 2971:  
 2972:         ////////////////////////////////////////////////////////////////////////////
 2973:  
 2974:         /// <summary>
 2975:         /// Generate a OptimizedStartEndRangeList() but with a range buffer before start and after end.
 2976:         /// </summary>
 2977:         public static List<Tuple<int, int>> OptimizedStartEndRangeBufferedList(List<int> list, int count, int bufferRange)
 2978:         {
 2979:             int start, end;
 2980:             List<Tuple<int, int>> tupleList;
 2981:  
 2982:             tupleList = OptimizedStartEndRangeTupleList(list, count);
 2983:  
 2984:             if (tupleList != null && tupleList.Count > 0)
 2985:             {
 2986:                 start = tupleList[0].Item1;
 2987:                 end = tupleList[tupleList.Count - 1].Item1;
 2988:  
 2989:                 tupleList.Insert(0, new Tuple<int, int>(start - bufferRange, start - 1));
 2990:                 tupleList.Insert(tupleList.Count - 1, new Tuple<int, int>(end + 1, end + bufferRange + 1));
 2991:             }
 2992:  
 2993:             tupleList.Sort();
 2994:  
 2995:             return tupleList;
 2996:         }
 2997:  
 2998:         ////////////////////////////////////////////////////////////////////////////
 2999:  
 3000:         /// <summary>
 3001:         /// Generate a list of start, end points of all integer ranges with given start, end and range. End included in range.
 3002:         /// </summary>
 3003:         public static List<Tuple<int, int>> StartEndRangeList(int start, int end, int range)
 3004:         {
 3005:             Tuple<int, int> tuple;
 3006:             List<Tuple<int, int>> tupleList;
 3007:  
 3008:             tupleList = new List<Tuple<int, int>>();
 3009:  
 3010:             if (end > start && range < (end - start))
 3011:             {
 3012:                 for (int i = start; i <= end; i += range)
 3013:                 {
 3014:                     if (i + range - 1 < end) tuple = new Tuple<int, int>(i, i + range - 1);
 3015:                     else tuple = new Tuple<int, int>(i, end);
 3016:  
 3017:                     tupleList.Add(tuple);
 3018:                 }
 3019:             }
 3020:             else throw new System.ArgumentException("Start, end, or range is/are invalid. ");
 3021:  
 3022:             tupleList.Sort();
 3023:  
 3024:             return tupleList;
 3025:         }
 3026:  
 3027:         ////////////////////////////////////////////////////////////////////////////
 3028:  
 3029:         /// <summary>
 3030:         /// Generate a StartEndRangeList() but with a range buffer before start and after end.
 3031:         /// </summary>
 3032:         public static List<Tuple<int, int>> StartEndRangeBufferedList(int start, int end, int range, int bufferRange)
 3033:         {
 3034:             List<Tuple<int, int>> tupleList;
 3035:  
 3036:             tupleList = StartEndRangeList(start, end, range);
 3037:  
 3038:             if (tupleList != null && tupleList.Count > 0)
 3039:             {
 3040:                 tupleList.Insert(0, new Tuple<int, int>(start - bufferRange, start - 1));
 3041:                 tupleList.Insert(tupleList.Count - 1, new Tuple<int, int>(end + 1, end + bufferRange + 1));
 3042:             }
 3043:  
 3044:             tupleList.Sort();
 3045:  
 3046:             return tupleList;
 3047:         }
 3048:  
 3049:         ////////////////////////////////////////////////////////////////////////////
 3050:  
 3051:         /// <summary>
 3052:         ///
 3053:         /// </summary>
 3054:         public static List<int> ConvertHyphenAndCommaSeperatedNumberStringToNumberList(string listStringAbbriviation)
 3055:         {
 3056:             int j, start, end;
 3057:             string abbriviation, u;
 3058:             List<int> list;
 3059:             MatchCollection matchCollection;
 3060:  
 3061:             // change number range (e.g. "1-5") to comma seperated numbers like "1,2,3,4,5"
 3062:  
 3063:             list = new List<int>();
 3064:  
 3065:             abbriviation = listStringAbbriviation;
 3066:  
 3067:             matchCollection = Regex.Matches(abbriviation, @"(\d{1,4})\-(\d{1,4})");
 3068:  
 3069:             foreach (Match match in matchCollection)
 3070:             {
 3071:                 start = int.Parse(match.Groups[1].Value);
 3072:                 end = int.Parse(match.Groups[2].Value);
 3073:  
 3074:                 u = "";
 3075:                 for (int i = start; i <= end; i++) u += i + ",";
 3076:  
 3077:                 u = u.TrimEnd(',');
 3078:  
 3079:                 // remove the matched string from the main string
 3080:                 abbriviation = abbriviation.Replace(match.Groups[0].Value, u);
 3081:             }
 3082:  
 3083:             foreach (string s in abbriviation.Split(','))
 3084:             {
 3085:                 if (int.TryParse(s, out j)) list.Add(j);
 3086:             }
 3087:  
 3088:             return list;
 3089:         }
 3090:  
 3091:         ////////////////////////////////////////////////////////////////////////////
 3092:         ////////////////////////////////////////////////////////////////////////////
 3093:  
 3094:         /// <summary>
 3095:         ///
 3096:         /// <see href="https://stackoverflow.com/questions/7688881/convert-list-to-number-range-string"/>
 3097:         /// </summary>
 3098:         public static string ConvertNumberListToHyphenAndCommaSeperatedNumberString(List<int> numberList)
 3099:         {
 3100:             return NumberListToPossiblyDegenerateRanges(numberList).Select(r => PrettyRange(r)).Intersperse(",");
 3101:         }
 3102:  
 3103:         private static IEnumerable<Tuple<int, int>> NumberListToPossiblyDegenerateRanges(IEnumerable<int> numList)
 3104:         {
 3105:             Tuple<int, int> currentRange = null;
 3106:  
 3107:             foreach (var num in numList)
 3108:             {
 3109:                 if (currentRange == null)
 3110:                 {
 3111:                     currentRange = Tuple.Create(num, num);
 3112:                 }
 3113:                 else if (currentRange.Item2 == num - 1)
 3114:                 {
 3115:                     currentRange = Tuple.Create(currentRange.Item1, num);
 3116:                 }
 3117:                 else
 3118:                 {
 3119:                     yield return currentRange;
 3120:                     currentRange = Tuple.Create(num, num);
 3121:                 }
 3122:             }
 3123:             if (currentRange != null)
 3124:             {
 3125:                 yield return currentRange;
 3126:             }
 3127:         }
 3128:  
 3129:         private static string PrettyRange(Tuple<int, int> range)
 3130:         {
 3131:             if (range.Item1 == range.Item2)
 3132:             {
 3133:                 return range.Item1.ToString();
 3134:             }
 3135:             return string.Format("{0}-{1}", range.Item1, range.Item2);
 3136:         }
 3137:  
 3138:         private static string Intersperse(this IEnumerable<string> items, string interspersand)
 3139:         {
 3140:             var currentInterspersand = "";
 3141:             var result = new StringBuilder();
 3142:  
 3143:             foreach (var item in items)
 3144:             {
 3145:                 result.Append(currentInterspersand);
 3146:                 result.Append(item);
 3147:                 currentInterspersand = interspersand;
 3148:             }
 3149:  
 3150:             return result.ToString();
 3151:         }
 3152:  
 3153:         ////////////////////////////////////////////////////////////////////////////
 3154:         ////////////////////////////////////////////////////////////////////////////
 3155:  
 3156:         /// <summary>
 3157:         ///
 3158:         /// </summary>
 3159:         public static string NumberListToCommaSeperatedNumberString(List<int> numberList)
 3160:         {
 3161:             string s;
 3162:  
 3163:             if (numberList != null && numberList.Count > 0)
 3164:             {
 3165:                 s = string.Join(",", numberList.Select(n => n.ToString()).ToArray());
 3166:             }
 3167:             else s = null;
 3168:  
 3169:             return s;
 3170:         }
 3171:  
 3172:         ////////////////////////////////////////////////////////////////////////////
 3173:  
 3174:         /// <summary>
 3175:         ///
 3176:         /// </summary>
 3177:         public static List<int> CommaSeperatedNumberStringToNumberList(string numberListString)
 3178:         {
 3179:             List<int> numberList;
 3180:  
 3181:             if (!string.IsNullOrEmpty(numberListString))
 3182:             {
 3183:                 numberList = (numberListString != string.Empty) ? numberListString.Split(',').Select(Int32.Parse).ToList() : null;
 3184:                 numberList.Sort();
 3185:             }
 3186:             else numberList = new List<int>();
 3187:  
 3188:             return numberList;
 3189:         }
 3190:  
 3191:         /*
 3192:         ////////////////////////////////////////////////////////////////////////////
 3193: 
 3194:         /// <summary>
 3195:         /// Generate HTML table from list of generic class with specified properties
 3196:         /// <see href="http://stackoverflow.com/questions/11126137/generate-html-table-from-list-of-generic-class-with-specified-properties"/>
 3197:         /// </summary>
 3198:         public static string GenerateHtmlTableFromListOfGenericClass<T>(IEnumerable<T> list, List<string> columnNameList, params Func<T, object>[] fxns)
 3199:         {
 3200:             StringBuilder sb;
 3201: 
 3202:             sb = new StringBuilder();
 3203: 
 3204:             sb.Append("<table>\n");
 3205: 
 3206:             // column names
 3207:             if (columnNameList.Count > 0)
 3208:             {
 3209:                 sb.Append("<tr>");
 3210: 
 3211:                 foreach (string column in columnNameList)
 3212:                 {
 3213:                     sb.Append("<td>");
 3214:                     sb.Append(column);
 3215:                     sb.Append("</td>");
 3216:                 }
 3217: 
 3218:                 sb.Append("</tr>\n");
 3219:             }
 3220: 
 3221:             foreach (var item in list)
 3222:             {
 3223:                 sb.Append("<tr>");
 3224: 
 3225:                 foreach (var fxn in fxns)
 3226:                 {
 3227:                     sb.Append("<td>");
 3228:                     sb.Append(fxn(item));
 3229:                     sb.Append("</td>");
 3230:                 }
 3231: 
 3232:                 sb.Append("</tr>\n");
 3233:             }
 3234: 
 3235:             sb.Append("</table>");
 3236: 
 3237:             return sb.ToString();
 3238:         }
 3239:          */
 3240:  
 3241:         ////////////////////////////////////////////////////////////////////////////
 3242:  
 3243:         /// <summary>
 3244:         /// 
 3245:         /// <see href="http://stackoverflow.com/questions/564366/convert-generic-list-enumerable-to-datatable"/>
 3246:         /// </summary>
 3247:         public static DataTable GenerateDataTableFromGenericClassList<T>(this IList<T> data)
 3248:         {
 3249:             DataTable dataTable;
 3250:             PropertyDescriptor propertyDescriptor;
 3251:             PropertyDescriptorCollection propertyDescriptorCollection;
 3252:  
 3253:             dataTable = new DataTable();
 3254:  
 3255:             dataTable.TableName = TypeDescriptor.GetClassName(typeof(T));
 3256:             propertyDescriptorCollection = TypeDescriptor.GetProperties(typeof(T));
 3257:             object[] values = new object[propertyDescriptorCollection.Count];
 3258:  
 3259:             for (int i = 0; i < propertyDescriptorCollection.Count; i++)
 3260:             {
 3261:                 propertyDescriptor = propertyDescriptorCollection[i];
 3262:  
 3263:                 dataTable.Columns.Add(propertyDescriptor.Name, propertyDescriptor.PropertyType);
 3264:             }
 3265:  
 3266:             foreach (T item in data)
 3267:             {
 3268:                 for (int i = 0; i < values.Length; i++) values[i] = propertyDescriptorCollection[i].GetValue(item);
 3269:  
 3270:                 dataTable.Rows.Add(values);
 3271:             }
 3272:  
 3273:             return dataTable;
 3274:         }
 3275:  
 3276:         ////////////////////////////////////////////////////////////////////////////
 3277:  
 3278:         /// <summary>
 3279:         /// 
 3280:         /// <see href="http://stackoverflow.com/questions/564366/convert-generic-list-enumerable-to-datatable"/>
 3281:         /// </summary>
 3282:         public static DataTable GenerateDataTableFromGenericClassList<T>(this IList<T> data, string dataTableName)
 3283:         {
 3284:             // I need to pass the dataTableName here to make the code consitant
 3285:             DataTable dataTable;
 3286:             PropertyDescriptor propertyDescriptor;
 3287:             PropertyDescriptorCollection propertyDescriptorCollection;
 3288:             Type type;
 3289:             PropertyInfo propertyInfo;
 3290:             object value;
 3291:             List<int> itemToBeRemoved;
 3292:  
 3293:             dataTable = new DataTable();
 3294:             itemToBeRemoved = new List<int>();
 3295:  
 3296:             dataTable.TableName = dataTableName; //TypeDescriptor.GetClassName(typeof(T));
 3297:  
 3298:             propertyDescriptorCollection = TypeDescriptor.GetProperties(typeof(T));
 3299:             List<object> valueList = new List<object>(propertyDescriptorCollection.Count);
 3300:  
 3301:             for (int i = 0; i < propertyDescriptorCollection.Count; i++)
 3302:             {
 3303:                 propertyDescriptor = propertyDescriptorCollection[i];
 3304:  
 3305:                 // Important: to handle complicated EF variables that represent foreign keys. I will check the property's full name, if it starts with "Ia."
 3306:                 // then this is a complicated foreign key variables that most likely points to an Id in another table, and I will attach a "_Id" suffix to the name and pass it into the DataTable.
 3307:                 // Also we will get the property type of this "Id" by checking the 0 index property of the parent property (since Id is almost always the first element)
 3308:  
 3309:                 if (propertyDescriptor.PropertyType.FullName.StartsWith("Ia."))
 3310:                 {
 3311:                     dataTable.Columns.Add(propertyDescriptor.Name + "_Id", TypeDescriptor.GetProperties(propertyDescriptor.PropertyType)[0].PropertyType);
 3312:                 }
 3313:                 else if (propertyDescriptor.PropertyType.FullName.StartsWith("System.Collections.Generic.ICollection"))
 3314:                 {
 3315:                     // this is a virtual property to to be converted to anything in table
 3316:                     itemToBeRemoved.Add(i);
 3317:                 }
 3318:                 else dataTable.Columns.Add(propertyDescriptor.Name, propertyDescriptor.PropertyType);
 3319:             }
 3320:  
 3321:             foreach (T d in data)
 3322:             {
 3323:                 valueList.Clear();
 3324:  
 3325:                 for (int i = 0; i < propertyDescriptorCollection.Count; i++)
 3326:                 {
 3327:                     if (!itemToBeRemoved.Contains(i))
 3328:                     {
 3329:                         propertyDescriptor = propertyDescriptorCollection[i];
 3330:  
 3331:                         // below: same as above we will check to see if property full name starts with "Ia." and assign the appropriate value accordingly
 3332:  
 3333:                         if (propertyDescriptor.PropertyType.FullName.StartsWith("Ia."))
 3334:                         {
 3335:                             type = d.GetType();
 3336:  
 3337:                             propertyInfo = type.GetProperty("Designation");
 3338:  
 3339:                             if (propertyInfo != null)
 3340:                             {
 3341:                                 value = propertyInfo.GetValue(d);
 3342:  
 3343:                                 valueList.Add(value.GetType().GetProperty("Id").GetValue(value));
 3344:                             }
 3345:                             else
 3346:                             {
 3347:                                 valueList.Add(null);
 3348:                             }
 3349:                         }
 3350:                         else valueList.Add(propertyDescriptor.GetValue(d));
 3351:                     }
 3352:                 }
 3353:  
 3354:                 dataTable.Rows.Add(valueList.ToArray());
 3355:             }
 3356:  
 3357:             return dataTable;
 3358:         }
 3359:  
 3360:         ////////////////////////////////////////////////////////////////////////////
 3361:  
 3362:         /// <summary>
 3363:         /// 
 3364:         /// <see href="http://stackoverflow.com/questions/564366/convert-generic-list-enumerable-to-datatable"/>
 3365:         /// </summary>
 3366:         public static string GenerateTextFromListOfGenericClass<T>(this IList<T> data, string propertyName)
 3367:         {
 3368:             StringBuilder sb;
 3369:             PropertyDescriptor prop;
 3370:             PropertyDescriptorCollection props;
 3371:  
 3372:             sb = new StringBuilder();
 3373:             props = TypeDescriptor.GetProperties(typeof(T));
 3374:  
 3375:             object value;
 3376:  
 3377:             value = new object();
 3378:  
 3379:             foreach (T item in data)
 3380:             {
 3381:                 for (int i = 0; i < props.Count; i++)
 3382:                 {
 3383:                     prop = props[i];
 3384:  
 3385:                     if (propertyName == prop.Name) value = props[i].GetValue(item);
 3386:                 }
 3387:  
 3388:                 sb.AppendLine(value.ToString());
 3389:             }
 3390:  
 3391:             return sb.ToString();
 3392:         }
 3393:  
 3394:         ////////////////////////////////////////////////////////////////////////////
 3395:  
 3396:         /// <summary>
 3397:         /// 
 3398:         /// <see href="http://stackoverflow.com/questions/564366/convert-generic-list-enumerable-to-datatable"/>
 3399:         /// </summary>
 3400:         public static DataTable GenerateDataTableFromListOfGenericClass<T>(this IList<T> data, params string[] propertyNameList)
 3401:         {
 3402:             int j;
 3403:             DataTable table;
 3404:             PropertyDescriptor prop;
 3405:             PropertyDescriptorCollection props;
 3406:  
 3407:             table = new DataTable();
 3408:             props = TypeDescriptor.GetProperties(typeof(T));
 3409:  
 3410:             for (int i = 0; i < props.Count; i++)
 3411:             {
 3412:                 prop = props[i];
 3413:  
 3414:                 if (propertyNameList.Contains(prop.Name))
 3415:                 {
 3416:                     table.Columns.Add(prop.Name, prop.PropertyType);
 3417:                 }
 3418:             }
 3419:  
 3420:             object[] values = new object[propertyNameList.Length];
 3421:  
 3422:             foreach (T item in data)
 3423:             {
 3424:                 j = 0;
 3425:  
 3426:                 for (int i = 0; i < props.Count; i++)
 3427:                 {
 3428:                     prop = props[i];
 3429:  
 3430:                     if (propertyNameList.Contains(prop.Name))
 3431:                     {
 3432:                         values[j++] = props[i].GetValue(item);
 3433:                     }
 3434:                 }
 3435:  
 3436:                 table.Rows.Add(values);
 3437:             }
 3438:  
 3439:             return table;
 3440:         }
 3441:  
 3442:         ////////////////////////////////////////////////////////////////////////////
 3443:  
 3444:         /// <summary>
 3445:         /// Generate HTML table from DataTable
 3446:         /// <see href="http://stackoverflow.com/questions/19682996/datatable-to-html-table"/>
 3447:         /// </summary>
 3448:         public static string GenerateHtmlTableFromDataTable(DataTable dataTable)
 3449:         {
 3450:             StringBuilder sb;
 3451:  
 3452:             sb = new StringBuilder();
 3453:  
 3454:             sb.Append("<table>\n");
 3455:  
 3456:             // header
 3457:             sb.Append("<tr>");
 3458:  
 3459:             for (int i = 0; i < dataTable.Columns.Count; i++) sb.Append("<td>" + dataTable.Columns[i].ColumnName + "</td>");
 3460:  
 3461:             sb.Append("</tr>");
 3462:  
 3463:             // row
 3464:             for (int i = 0; i < dataTable.Rows.Count; i++)
 3465:             {
 3466:                 sb.Append("<tr>");
 3467:  
 3468:                 for (int j = 0; j < dataTable.Columns.Count; j++) sb.Append("<td>" + dataTable.Rows[i][j].ToString() + "</td>");
 3469:  
 3470:                 sb.Append("</tr>");
 3471:             }
 3472:  
 3473:             sb.Append("</table>");
 3474:  
 3475:             return sb.ToString();
 3476:         }
 3477:  
 3478:         ////////////////////////////////////////////////////////////////////////////
 3479:  
 3480:         /// <summary>
 3481:         /// Generate tab separated text format from DataTable
 3482:         /// </summary>
 3483:         public static string GenerateTabSeparatedTextFromDataTable(DataTable dataTable)
 3484:         {
 3485:             StringBuilder sb;
 3486:  
 3487:             sb = new StringBuilder();
 3488:  
 3489:             for (int i = 0; i < dataTable.Columns.Count; i++) sb.Append(dataTable.Columns[i].ColumnName + "\t");
 3490:  
 3491:             sb.AppendLine();
 3492:  
 3493:             // row
 3494:             for (int i = 0; i < dataTable.Rows.Count; i++)
 3495:             {
 3496:                 for (int j = 0; j < dataTable.Columns.Count; j++) sb.Append(dataTable.Rows[i][j].ToString() + "\t");
 3497:  
 3498:                 sb.AppendLine();
 3499:             }
 3500:  
 3501:             return sb.ToString();
 3502:         }
 3503:  
 3504:         ////////////////////////////////////////////////////////////////////////////
 3505:  
 3506:         /// <summary>
 3507:         /// Generate DataTable from delimited text
 3508:         /// </summary>
 3509:         public static DataTable GenerateDataTableFromTabDelimitedText(string text, out Result result)
 3510:         {
 3511:             // this will only read rows that have at least 5 distinct columns
 3512:             bool first;
 3513:             string line;
 3514:             string[] columnList, lineSplit;
 3515:             DataTable dataTable;
 3516:  
 3517:             first = true;
 3518:             dataTable = new DataTable();
 3519:             result = new Result();
 3520:  
 3521:             using (StringReader reader = new StringReader(text))
 3522:             {
 3523:                 while ((line = reader.ReadLine()) != null)
 3524:                 {
 3525:                     // lineSplit = line.Split((string[])null, StringSplitOptions.None);
 3526:                     lineSplit = Regex.Split(line, @"\t", RegexOptions.None);
 3527:  
 3528:                     if (IsTableHeaderText(line) && first)
 3529:                     {
 3530:                         columnList = lineSplit;
 3531:  
 3532:                         foreach (var column in columnList) dataTable.Columns.Add(column);
 3533:  
 3534:                         first = false;
 3535:                     }
 3536:                     else if (!first)
 3537:                     {
 3538:                         if (lineSplit.Length == dataTable.Columns.Count)
 3539:                         {
 3540:                             dataTable.Rows.Add(lineSplit);
 3541:                         }
 3542:                         else
 3543:                         {
 3544:                             result.AddWarning("lineSplit.Length != dataTable.Columns.Count for line: " + line);
 3545:                         }
 3546:                     }
 3547:                     else
 3548:                     {
 3549:                     }
 3550:                 }
 3551:             }
 3552:  
 3553:             return dataTable;
 3554:         }
 3555:  
 3556:         ////////////////////////////////////////////////////////////////////////////
 3557:  
 3558:         /// <summary>
 3559:         /// Examine a string to see if it resembles a table header string
 3560:         /// <see href="https://stackoverflow.com/questions/17812566/count-words-and-spaces-in-string-c-sharp"/>
 3561:         /// </summary>
 3562:         private static bool IsTableHeaderText(string text)
 3563:         {
 3564:             bool isHeader;
 3565:             int whitespaceCount, wordCount;
 3566:             Regex regex = new Regex(@"^[a-zA-Z\s]+$");
 3567:  
 3568:             if (!string.IsNullOrEmpty(text))
 3569:             {
 3570:                 text = text.Replace("\t", "     "); // we will replace every tab with 5 spaces
 3571:  
 3572:                 whitespaceCount = text.Count(Char.IsWhiteSpace);
 3573:                 wordCount = CountWordsInText(text);
 3574:  
 3575:                 // if the whitespace count is bigger than word count is probably a header text string
 3576:                 if (whitespaceCount > 2 * wordCount)
 3577:                 {
 3578:                     if (regex.IsMatch(text)) isHeader = true;
 3579:                     else isHeader = false;
 3580:                 }
 3581:                 else isHeader = false;
 3582:             }
 3583:             else
 3584:             {
 3585:                 isHeader = false;
 3586:             }
 3587:  
 3588:             return isHeader;
 3589:         }
 3590:  
 3591:         ////////////////////////////////////////////////////////////////////////////
 3592:  
 3593:         /// <summary>
 3594:         ///
 3595:         /// </summary>
 3596:         private static int CountWordsInText(string text)
 3597:         {
 3598:             int wordCount = 0, index = 0;
 3599:  
 3600:             while (index < text.Length)
 3601:             {
 3602:                 // check if current char is part of a word
 3603:                 while (index < text.Length && !char.IsWhiteSpace(text[index])) index++;
 3604:  
 3605:                 wordCount++;
 3606:  
 3607:                 // skip whitespace until next word
 3608:                 while (index < text.Length && char.IsWhiteSpace(text[index])) index++;
 3609:             }
 3610:  
 3611:             return wordCount;
 3612:         }
 3613:  
 3614:         ////////////////////////////////////////////////////////////////////////////
 3615:  
 3616:         /// <summary>
 3617:         /// Generate tab separated text format from Dictionary
 3618:         /// </summary>
 3619:         public static string GenerateTabSeparatedTextFromDictionary(Dictionary<string, int> dictionary)
 3620:         {
 3621:             StringBuilder sb;
 3622:  
 3623:             sb = new StringBuilder();
 3624:  
 3625:             foreach (KeyValuePair<string, int> kvp in dictionary)
 3626:             {
 3627:                 sb.AppendLine(kvp.Key + "\t" + kvp.Value);
 3628:             }
 3629:  
 3630:             return sb.ToString();
 3631:         }
 3632:  
 3633:         ////////////////////////////////////////////////////////////////////////////
 3634:  
 3635:         /// <summary>
 3636:         /// Generate tab separated text format from SortedDictionary
 3637:         /// </summary>
 3638:         public static string GenerateTabSeparatedTextFromDictionary(SortedDictionary<string, int> sortedDictionary)
 3639:         {
 3640:             StringBuilder sb;
 3641:  
 3642:             sb = new StringBuilder();
 3643:  
 3644:             foreach (KeyValuePair<string, int> kvp in sortedDictionary)
 3645:             {
 3646:                 sb.AppendLine(kvp.Key + "\t" + kvp.Value);
 3647:             }
 3648:  
 3649:             return sb.ToString();
 3650:         }
 3651:  
 3652:         ////////////////////////////////////////////////////////////////////////////
 3653:  
 3654:         /// <summary>
 3655:         /// Generate simple two column name value table from DataTable
 3656:         /// </summary>
 3657:         public static string GenerateTwoColumnNameValueTextFromDataTable(DataTable dataTable)
 3658:         {
 3659:             StringBuilder sb;
 3660:  
 3661:             sb = new StringBuilder();
 3662:  
 3663:             for (int i = 0; i < dataTable.Rows.Count; i++)
 3664:             {
 3665:                 for (int j = 0; j < dataTable.Columns.Count; j++)
 3666:                 {
 3667:                     sb.Append(dataTable.Columns[j].ColumnName + ":\t" + dataTable.Rows[i][j].ToString() + "\r\n");
 3668:                 }
 3669:  
 3670:                 sb.AppendLine();
 3671:             }
 3672:  
 3673:             return sb.ToString().Trim();
 3674:         }
 3675:  
 3676:         ////////////////////////////////////////////////////////////////////////////
 3677:  
 3678:         /// <summary>
 3679:         /// Remove non-numeric characters
 3680:         /// http://stackoverflow.com/questions/3977497/stripping-out-non-numeric-characters-in-string
 3681:         /// </summary>
 3682:         public static string RemoveNonNumericCharacters(string line)
 3683:         {
 3684:             string s;
 3685:  
 3686:             if (line != null && line.Length != 0)
 3687:             {
 3688:                 s = new string(line.Where(c => char.IsDigit(c)).ToArray());
 3689:                 //s = Regex.Replace(line, "[^0-9]", "");
 3690:             }
 3691:             else s = line;
 3692:  
 3693:             return s;
 3694:         }
 3695:  
 3696:         ////////////////////////////////////////////////////////////////////////////
 3697:  
 3698:         /// <summary>
 3699:         /// 
 3700:         /// <remarks>http://stackoverflow.com/questions/2475795/check-for-missing-number-in-sequence</remarks>
 3701:         /// </summary>
 3702:         public static List<int> ExcludedNumberListFromNumberListWithinRange(List<int> list, int listSize)
 3703:         {
 3704:             // Check for missing number in sequence
 3705:             List<int> exclusionList;
 3706:  
 3707:             exclusionList = Enumerable.Range(1, listSize).Except(list).ToList();
 3708:  
 3709:             return exclusionList;
 3710:         }
 3711:  
 3712:         ////////////////////////////////////////////////////////////////////////////
 3713:  
 3714:         /// <summary>
 3715:         /// 
 3716:         /// </summary>
 3717:         public static string AutoGeneratedXmlTextStartCommentString
 3718:         {
 3719:             get
 3720:             {
 3721:                 string s;
 3722:  
 3723:                 s = @"<!-- auto generated: start -->";
 3724:  
 3725:                 return s;
 3726:             }
 3727:         }
 3728:  
 3729:         ////////////////////////////////////////////////////////////////////////////
 3730:  
 3731:         /// <summary>
 3732:         /// 
 3733:         /// </summary>
 3734:         public static string AutoGeneratedXmlTextEndCommentString
 3735:         {
 3736:             get
 3737:             {
 3738:                 string s;
 3739:  
 3740:                 s = @"<!-- auto-generated: end -->";
 3741:  
 3742:                 return s;
 3743:             }
 3744:         }
 3745:  
 3746:         ////////////////////////////////////////////////////////////////////////////
 3747:  
 3748:         /// <summary>
 3749:         /// 
 3750:         /// </summary>
 3751:         public static string AutoGeneratedCSharpCodeStartCommentString
 3752:         {
 3753:             get
 3754:             {
 3755:                 string s;
 3756:  
 3757:                 s = @"// auto generated: start";
 3758:  
 3759:                 return s;
 3760:             }
 3761:         }
 3762:  
 3763:         ////////////////////////////////////////////////////////////////////////////
 3764:  
 3765:         /// <summary>
 3766:         /// 
 3767:         /// </summary>
 3768:         public static string AutoGeneratedCSharpCodeEndCommentString
 3769:         {
 3770:             get
 3771:             {
 3772:                 string s;
 3773:  
 3774:                 s = @"// auto-generated: end";
 3775:  
 3776:                 return s;
 3777:             }
 3778:         }
 3779:  
 3780:         ////////////////////////////////////////////////////////////////////////////
 3781:  
 3782:         /// <summary>
 3783:         /// 
 3784:         /// </summary>
 3785:         public static int LevenshteinDistance(string s, string t)
 3786:         {
 3787:             if (string.IsNullOrEmpty(s))
 3788:             {
 3789:                 if (string.IsNullOrEmpty(t)) return 0;
 3790:  
 3791:                 return t.Length;
 3792:             }
 3793:  
 3794:             if (string.IsNullOrEmpty(t))
 3795:             {
 3796:                 return s.Length;
 3797:             }
 3798:  
 3799:             int n = s.Length;
 3800:             int m = t.Length;
 3801:             int[,] d = new int[n + 1, m + 1];
 3802:  
 3803:             // initialize the top and right of the table to 0, 1, 2, ...
 3804:             for (int i = 0; i <= n; d[i, 0] = i++) ;
 3805:             for (int j = 1; j <= m; d[0, j] = j++) ;
 3806:  
 3807:             for (int i = 1; i <= n; i++)
 3808:             {
 3809:                 for (int j = 1; j <= m; j++)
 3810:                 {
 3811:                     int cost = (t[j - 1] == s[i - 1]) ? 0 : 1;
 3812:                     int min1 = d[i - 1, j] + 1;
 3813:                     int min2 = d[i, j - 1] + 1;
 3814:                     int min3 = d[i - 1, j - 1] + cost;
 3815:                     d[i, j] = Math.Min(Math.Min(min1, min2), min3);
 3816:                 }
 3817:             }
 3818:  
 3819:             return d[n, m];
 3820:         }
 3821:  
 3822:         ////////////////////////////////////////////////////////////////////////////
 3823:         ////////////////////////////////////////////////////////////////////////////
 3824:     }
 3825:  
 3826: #if WFA
 3827:     ////////////////////////////////////////////////////////////////////////////
 3828:  
 3829:     /// <summary>
 3830:     ///
 3831:     /// </summary>
 3832:     public class ListItem
 3833:     {
 3834:         // I created this because Windows Forms does not have the equivalent like System.Web ListItem
 3835:  
 3836:         private string name;
 3837:         private string value;
 3838:  
 3839:         ////////////////////////////////////////////////////////////////////////////
 3840:  
 3841:         /// <summary>
 3842:         ///
 3843:         /// </summary>
 3844:         public ListItem(string _name, string _value)
 3845:         {
 3846:             this.name = _name;
 3847:             this.value = _value;
 3848:         }
 3849:  
 3850:         ////////////////////////////////////////////////////////////////////////////
 3851:  
 3852:         /// <summary>
 3853:         ///
 3854:         /// </summary>
 3855:         public string Name
 3856:         {
 3857:             get
 3858:             {
 3859:                 return name;
 3860:             }
 3861:         }
 3862:  
 3863:         ////////////////////////////////////////////////////////////////////////////
 3864:  
 3865:         /// <summary>
 3866:         ///
 3867:         /// </summary>
 3868:         public string Value
 3869:         {
 3870:  
 3871:             get
 3872:             {
 3873:                 return value;
 3874:             }
 3875:         }
 3876:     }
 3877: #else
 3878: #endif
 3879:  
 3880:     ////////////////////////////////////////////////////////////////////////////
 3881:     ////////////////////////////////////////////////////////////////////////////
 3882: }