kelumKP / Sample-Codes

This is sample code 100% made by me for future references
0 stars 0 forks source link

Get Date of Birthby NIC number #17

Open kelumKP opened 8 years ago

kelumKP commented 8 years ago
        private string GetDOBbyNIC(string nic)
        {
            try
            {
                String nic_ = nic.Trim().ToUpper();
                char[] nicarray = nic_.ToCharArray();

                ///assign Title and Sex

                string thirdNum = (nicarray[2]).ToString();
                if (thirdNum == "5" || thirdNum == "6" || thirdNum == "7" || thirdNum == "8" || thirdNum == "9")
                {
                    ddlSex.SelectedValue = "F";
                    ddlTitle.SelectedValue = "Ms.";
                }
                else
                {
                    ddlSex.SelectedValue = "M";
                    ddlTitle.SelectedValue = "Mr.";
                }

                //---------DOB generation----------------------
                string threechar = (nicarray[2]).ToString() + (nicarray[3]).ToString() + (nicarray[4]).ToString();
                Int32 DPBnum = Convert.ToInt32(threechar);
                if (DPBnum > 500)
                {
                    DPBnum = DPBnum - 500;
                }

                // Int32 JAN = 1, FEF = 2, MAR = 3, APR = 4, MAY = 5, JUN = 6, JUL = 7, AUG = 8, SEP = 9, OCT = 10, NOV = 11, DEC = 12;

                Dictionary<string, Int32> monthDict = new Dictionary<string, int>();
                monthDict.Add("JAN", 31);
                monthDict.Add("FEF", 29);
                monthDict.Add("MAR", 31);
                monthDict.Add("APR", 30);
                monthDict.Add("MAY", 31);
                monthDict.Add("JUN", 30);
                monthDict.Add("JUL", 31);
                monthDict.Add("AUG", 31);
                monthDict.Add("SEP", 30);
                monthDict.Add("OCT", 31);
                monthDict.Add("NOV", 30);
                monthDict.Add("DEC", 31);

                string bornMonth = string.Empty;
                Int32 bornDate = 0;

                Int32 leftval = DPBnum;
                foreach (var itm in monthDict)
                {
                    bornDate = leftval;

                    if (leftval <= itm.Value)
                    {
                        bornMonth = itm.Key;

                        break;
                    }

                    leftval = leftval - itm.Value;
                }

                //-------------------------------
                // string bornMonth1 = bornMonth;
                // Int32 bornDate1 = bornDate;

                Dictionary<string, Int32> monthDict2 = new Dictionary<string, int>();
                monthDict2.Add("JAN", 1);
                monthDict2.Add("FEF", 2);
                monthDict2.Add("MAR", 3);
                monthDict2.Add("APR", 4);
                monthDict2.Add("MAY", 5);
                monthDict2.Add("JUN", 6);
                monthDict2.Add("JUL", 7);
                monthDict2.Add("AUG", 8);
                monthDict2.Add("SEP", 9);
                monthDict2.Add("OCT", 10);
                monthDict2.Add("NOV", 11);
                monthDict2.Add("DEC", 12);
                Int32 dobMon = 0;
                foreach (var itm in monthDict2)
                {
                    if (itm.Key == bornMonth)
                    {
                        dobMon = itm.Value;
                    }
                }
                Int32 dobYear = 1900 + Convert.ToInt32((nicarray[0].ToString())) * 10 + Convert.ToInt32((nicarray[1].ToString()));
                try
                {
                    DateTime dob = new DateTime(dobYear, dobMon, bornDate);
                    nic = dob.Date.ToShortDateString();
                    //dob.ToString("dd/MM/yyyy");
                    return nic;
                }
                catch (Exception err)
                {
                    DisplayMessage("Please enter a valid NIC no!", 2);
                }
            }
            catch (Exception err)
            {
                DisplayMessage("Error Occurred while processing!", 2);
            }
            return null;
        }