Wednesday, August 3, 2011

Javascript for calculating age

<script type="text/javascript">
       function showAge() {
          
           var d = document.getElementById('<%= txtDateOfBirth.ClientID %>').value.split('/');
           var today = new Date();
           var bday = new Date(d[2], d[1], d[0]);
           var by = bday.getFullYear();
           var bm = bday.getMonth() - 1;
           var bd = bday.getDate();
           var age = 0; var dif = bday;
           while (dif <= today) {
               var dif = new Date(by + age, bm, bd);
               age++;
           }
           age += -2;
           document.getElementById("<%=txtCurrentAge.ClientID %>").value = age;
           alert('You are ' + age + ' years old')
       }
</script>

//On page load write this..
this.txtDateOfBirth.Attributes.Add("OnBlur", "javascript:return  showAge();");

Validation on Checkbox

 <script language="javascript" type="text/javascript"> 

function validateCheckBox1(source, args)
        {
            args.IsValid = document.getElementById('<%= Chk1.ClientID %>').checked;
        }
</script>

// call this with custom validator in HTML page

CheckBox ID="Chk1" runat="server" />
                      
<asp:CustomValidator runat="server" ID="CustomValidator4" Visible="true"
ClientValidationFunction="validateCheckBox2"
 ErrorMessage="Please confirm the authorisation 1" Display="Dynamic"  />

Validation in Radiobutton List


<script language="javascript" type="text/javascript">

 function ValidateRadioButtonListSIALenece() {
            var RBL = document.getElementById('<%= RbtnSIA.ClientID %>');
            var radiobuttonlist = RBL.getElementsByTagName("input");
            var counter = 0;
            var atLeast = 1;
            for (var i = 0; i < radiobuttonlist.length; i++) {
                if (radiobuttonlist[i].checked) {

                    if (radiobuttonlist[i].value == "Yes") {
                        // alert('y');
                        document.getElementById("<%=txtDateOfApplication.ClientID %>").value = "";
                        document.getElementById("<%=txtDateOfApplication.ClientID %>").readOnly = false;
                        document.getElementById("<%=Img29.ClientID %>").style.visibility = 'visible';
                        document.getElementById("<%=RegularExpressionValidator10.ClientID %>").readOnly = false;
                    }
                    if (radiobuttonlist[i].value == "No") {
                        // alert('N');
                        document.getElementById("<%=txtDateOfApplication.ClientID %>").value = "";
                        document.getElementById("<%=txtDateOfApplication.ClientID %>").readOnly = true;
                        document.getElementById("<%=Img29.ClientID %>").style.visibility = 'hidden';
                        document.getElementById("<%=RegularExpressionValidator10.ClientID %>").readOnly = true;

                    }

                    counter++;
                }
            }
        }
</script>

I am calling this on radiobutton list click event
this.RBtnList.Attributes.Add("OnClick", "javascript:return  ValidateRadioButtonListSIALenece();");

Saturday, July 30, 2011

The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.

The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.
The above error will be thrown at the time of updating Library or Lists items due to authentication problem, or We didn’t have the rights to update the items.
By using the follwoing code, We can resolve this as,
Web.AllowUnsafeUpdates = true;
item["Title"]=”Sample”;
item.Update();
or we can also use the follwoing code to overcome the above error, this will affect the WebApplication. This will change the Security settings of a Web Application to allow the anonymous user to update item.
web.Site.WebApplication.FormDigestSettings.Enabled = false;
item["Title"]=”Sample”;
item.Update();
web.Site.WebApplication.FormDigestSettings.Enabled = true;
or we can change the settings in Central Administration to anonymously update the items. But this method will be dangerous.
Central Administration > Application Management > Web Application General Settings
Choose the WebApplication to overwrite the settings
Select the option Off in Web Page Security Validation as follows,

Wednesday, June 8, 2011

Date Validation at code behind in c#


        string strDOB = ""; string strDebtType = "";
        txtJudgementdate.Text = hdnJudgementdate.Value;
        txtCostCertificationDate.Text = hdnCostCertificationDate.Value;
        txtDefaultDate.Text = hdnDefaultDate.Value;
        if (ddlDay.SelectedIndex > 0)
            if (ddlMonth.SelectedIndex > 0 && ddlYear.SelectedIndex > 0)
            {
                if (ddlMonth.SelectedIndex == 4 || ddlMonth.SelectedIndex == 6 || ddlMonth.SelectedIndex == 9 || ddlMonth.SelectedIndex == 11)
                {
                    if (ddlDay.SelectedIndex > 30)
                    {
                        lblError.Text = "Please Select Valid Date of Birth.";
                        return;
                    }
                }
                else if (ddlMonth.SelectedIndex == 2)
                {
                    if (Convert.ToInt32(ddlYear.SelectedValue) % 4 == 0)
                    {
                        if (ddlDay.SelectedIndex > 29)
                        {
                            lblError.Text = "Please Select Valid Date of Birth.";
                            return;
                        }
                    }
                    else
                    {
                        if (ddlDay.SelectedIndex > 28)
                        {
                            lblError.Text = "Please Select Valid Date of Birth.";
                            return;
                        }
                    }
                }
               
                strDOB = new DateTime(Convert.ToInt32(ddlYear.SelectedValue), Convert.ToInt16(ddlMonth.SelectedIndex), Convert.ToInt16(ddlDay.SelectedValue)).ToString("dd/MM/yyyy");
                //strDOB = DateTime.Parse(ddlDay.SelectedValue + "/" + Convert.ToInt16(ddlMonth.SelectedIndex) + "/" + Convert.ToInt32(ddlYear.SelectedValue)).ToString("dd/MM/yyyy");
            }
            else
            {
                lblError.Text = "Please Select Valid Date of Birth.";
                return;
            }
        else if (ddlMonth.SelectedIndex > 0)
            if (ddlDay.SelectedIndex > 0 && ddlYear.SelectedIndex > 0)
            {
                if (ddlMonth.SelectedIndex == 4 || ddlMonth.SelectedIndex == 6 || ddlMonth.SelectedIndex == 9 || ddlMonth.SelectedIndex == 11)
                {
                    if (ddlDay.SelectedIndex > 30)
                    {
                        lblError.Text = "Please Select Valid Date of Birth.";
                        return;
                    }}
                else if (ddlMonth.SelectedIndex == 2)
                {
                    if (Convert.ToInt32(ddlYear.SelectedValue) % 4 == 0)
                    {
                        if (ddlDay.SelectedIndex > 29)
                        {
                            lblError.Text = "Please Select Valid Date of Birth.";
                            return;
                        }
                    }
                    else
                    {
                        if (ddlDay.SelectedIndex > 28)
                        {
                            lblError.Text = "Please Select Valid Date of Birth.";
                            return;
                        }
                    }
                }
                strDOB = new DateTime(Convert.ToInt32(ddlYear.SelectedValue), Convert.ToInt16(ddlMonth.SelectedIndex), Convert.ToInt16(ddlDay.SelectedValue)).ToString("dd/MM/yyyy");
            }
            else
            {
                lblError.Text = "Please Select Valid Date of Birth.";
                return;
            }
        else if (ddlYear.SelectedIndex > 0)
            if (ddlMonth.SelectedIndex > 0 && ddlDay.SelectedIndex > 0)
            {
                if (ddlMonth.SelectedIndex == 4 || ddlMonth.SelectedIndex == 6 || ddlMonth.SelectedIndex == 9 || ddlMonth.SelectedIndex == 11)
                {
                    if (ddlDay.SelectedIndex > 30)
                    {
                        lblError.Text = "Please Select Valid Date of Birth.";
                        return;
                    }}
                else if (ddlMonth.SelectedIndex == 2)
                {
                    if (Convert.ToInt32(ddlYear.SelectedValue) % 4 == 0)
                    {
                        if (ddlDay.SelectedIndex > 29)
                        {
                            lblError.Text = "Please Select Valid Date of Birth.";
                            return;
                        }
                    }
                    else
                    {
                        if (ddlDay.SelectedIndex > 28)
                        {
                            lblError.Text = "Please Select Valid Date of Birth.";
                            return;
                        }
                    }
                }
                strDOB = new DateTime(Convert.ToInt32(ddlYear.SelectedValue), Convert.ToInt16(ddlMonth.SelectedIndex), Convert.ToInt16(ddlDay.SelectedValue)).ToString("dd/MM/yyyy");
            }
            else
            {
                lblError.Text = "Please Select Valid Date of Birth.";
                return;
            }

Tuesday, May 24, 2011

Check If A String Value Is Numeric or Not

Following is the example for testing string value as number or not
            string str123 = "1245"
               Double res = 0;
            bool b12=Double.TryParse(str123, out res);
this will return true. for the above example. If however, str123="123ab" then the above code will return false.

             

Friday, May 20, 2011

How to merge two diffrent Hashtable in asp.net

//Method return an XML
  public Hashtable GetSimpleCollection()
    {
        Hashtable XMLmappingHash = new Hashtable();
        ////Test XML parser....
        XmlTextReader testXMlReader = XMLparse();
        //Load the Loop
        while (!testXMlReader.EOF)
        {
            //Go to the name tag
            testXMlReader.Read();
            //if not start element exit while loop
            //if (!testXMlReader.IsStartElement())
            //{
            //    break; // TODO: might not be correct. Was : Exit While
            //}
            string nodetype = testXMlReader.Name;
            string attributeValue = string.Empty;
            if (nodetype == "excelcol")
            {
               
                while (testXMlReader.MoveToNextAttribute())
                {
                    attributeValue = testXMlReader.Value;
                }
                XMLmappingHash.Add(testXMlReader.ReadElementString("excelcol"), attributeValue);
            }
          
        }
       
        return XMLmappingHash;
    }

// Another Mehtod return hashtable
 public Hashtable GetCompositeHashtable()
    {
        string val =string.Empty;
        StringBuilder sb;
        Hashtable CompHashtable=new Hashtable();
        XmlDocument XMLRead = new XmlDocument(); // Create instance of XmlDocument class    
        //XMLRead.Load(Server.MapPath("Page1.xml")); // Load Xml file
        XMLRead.Load("D:\\Work\\GetExcelSheetNames\\XMLFile.xml");
        XmlNodeList nodes = XMLRead.SelectNodes(@"excelMapping/tableName");
        foreach (XmlNode node in nodes)
        {
            XmlNodeList nodes2list = node.ChildNodes;
            for (int i = 0; i < nodes2list.Count; i++)
            {
              
                string key = "comp1" + i;
                string Nodetype = nodes2list[i].Name;
                if (Nodetype == "excelsComposite")
                {
                    XmlNode CompositeNode = nodes2list.Item(i);
                    XmlNodeList CompNodeChild = CompositeNode.ChildNodes;
                    sb = new StringBuilder();
                    foreach (XmlNode CompChild in CompNodeChild)
                    {
                        if (CompChild.Attributes["operation"].Value == "Add")
                        {
                        
                            foreach (XmlNode CompChildNodeValue in CompChild)
                            {
                                //sb = sb.Append("SUM");
                                sb = sb.Append(CompChildNodeValue.InnerText);
                               sb= sb.Append(",");
                              
                            }
                            if (sb.Length!=0)
                            {
                                sb.Remove(sb.Length - 1, 1);
                               
                            }
                         
                        }
                        if (CompChild.Attributes["operation"].Value == "Divide")
                        {
                            sb = sb.Append(CompChild.InnerText);
                            //val = "(x1+x2)/x3";
                        }
                      
                        //string ControlID = n2a.InnerText.Trim();
                        // string ControlType = n2a.Attributes["ControlType"].Value;
                        // string AccessMode = n2a.Attributes["AccessType"].Value;
                        // AddControl(ControlType, ControlID, AccessMode);
                    }
                    CompHashtable.Add(key, sb);
                
                }
            }
        }
        return CompHashtable;
               
    }

////Method that merge two diffrent Hashtables

 public Hashtable GetCompleteCollection()
    {
        string HashKey = string.Empty;
        string HashValue = string.Empty;
        Hashtable DatafieldHashmap = new Hashtable();
        Hashtable CompHashMap = new Hashtable();
        DatafieldHashmap = GetSimpleCollection();
        CompHashMap = GetCompositeHashtable();
        IDictionaryEnumerator Hashmap = CompHashMap.GetEnumerator();
        while (Hashmap.MoveNext())
        {
            HashKey = Hashmap.Key.ToString();
            HashValue = Hashmap.Value.ToString();
            DatafieldHashmap.Add(HashKey,HashValue);
        }
        return DatafieldHashmap;
    }

//// Call this on page load like this

 Hashtable XMLCollectionreturn = new Hashtable();
     
   XMLCollectionreturn = GetCompleteCollection();