Wednesday, August 3, 2011

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();

How to read and return an XML in asp.net

public XmlTextReader XMLparse()
    {
        XmlTextReader m_xmlr = null;
        //Create the XML Reader
        m_xmlr = new XmlTextReader("D:\\Work\\GetExcelSheetNames\\XMLFile.xml");
        //Disable whitespace so that you don't have to read over whitespaces
        m_xmlr.WhitespaceHandling = WhitespaceHandling.None;
        //read the xml declaration and advance to family tag
        m_xmlr.Read();
        //read the family tag
        m_xmlr.Read();
        //Load the Loop
        while (!m_xmlr.EOF)
        {
            //Go to the name tag
            m_xmlr.Read();
            //if not start element exit while loop
            if (!m_xmlr.IsStartElement())
            {
                break; // TODO: might not be correct. Was : Exit While
            }
            //Get the Gender Attribute Value
            string genderAttribute = m_xmlr.GetAttribute("gender");
            //Read elements firstname and lastname
            m_xmlr.Read();
            //Get the firstName Element Value
            string firstNameValue = m_xmlr.ReadElementString("firstname");
            //Get the lastName Element Value
            string lastNameValue = m_xmlr.ReadElementString("lastname");
            //Write Result to the Console
            Response.Write("Gender: " + genderAttribute + " FirstName: " + firstNameValue + " LastName: " + lastNameValue);
            // Console.Write(Constants.vbCrLf);
        }
        //close the reader
        m_xmlr.Close();
        return m_xmlr;
    }

Thursday, April 21, 2011

How to make and use Split function in sql Server

1. Create a Function in SQL as follows:

Create FUNCTION dbo.StringSplit
(
@SplitStr nvarchar(2000),
@SplitChar nvarchar(20)
)
RETURNS @RtnValue table
(
Data nvarchar(2000)
)
AS
BEGIN
Declare @Count int
Set @Count = 1
While (Charindex(@SplitChar,@SplitStr)>0)
Begin
Insert Into @RtnValue (Data)
Select
Data = ltrim(rtrim(Substring(@SplitStr,1,Charindex(@SplitChar,@SplitStr)-1)))
Set @SplitStr = Substring(@SplitStr,Charindex(@SplitChar,@SplitStr)+1,len(@SplitStr))
Set @Count = @Count + 1
End
Insert Into @RtnValue (Data)
Select Data = ltrim(rtrim(@SplitStr))
Return
END

2. you need to pass two parameter one is string and otherone is spliting character
here is example

select * from SplitString('Contact-No-Deal\Email\08-30-2010\SP10000175-08-30-2010-16-56-28-5322-BalanceRequest.doc', '\')

output will be like this:

Contact-No-Deal
Email
08-30-2010
SP10000175-08-30-2010-16-56-28-5322-BalanceRequest.doc