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;
    }