Tuesday, January 7, 2014

Create New XML file using C#

I want to create xml file using c#. so i have to created like
XML File source file.
<Students>
    <Student>
      <Name>senthil</Name>
      <Subject1>50</Subject1>
      <Subject2>60</Subject2>
      <Subject3>70</Subject3>
      <Subject4>80</Subject4>
      <Image>
 <ImageOrigin>
  <X>1</X>
  <Y>10</Y>
 </ImageOrigin>

      </Image>
    </Student>
</Students>
Created xml using c# code.
XDocument document = new XDocument();
XElement rootElement = new XElement("Students");

foreach(Student student in StudentsCollection)
{
    XElement studentElement = new XElement("Student");
    
    studentElement.Add(new XElement("Name") { Value = student.Name });
    studentElement.Add(new XElement("Subject1") { Value = student.Subject1 });
    studentElement.Add(new XElement("Subject2") { Value = student.Subject2 });
    studentElement.Add(new XElement("Subject3") { Value = student.Subject3 });
    studentElement.Add(new XElement("Subject4") { Value = student.Subject4 });
    studentElement.Add(new XElement("Image"));

    XElement imageElement = studentElement.Element("Image");

    imageElement.Add(new XElement("ImageOrigin"));

    XElement imageOriginElement = imageElement.Element("ImageOrigin");

    imageOriginElement.Add(new XElement("X") { Value = "1" });
    imageOriginElement.Add(new XElement("Y") { Value = "10" });

    rootElement.Add(studentElement);
}

document.Add(rootElement);
document.Save("D:\Stduent.xml");

Remove the space in textbox using JavaScript

This code remove the space in form textbox value on onkeydown Event using .net form and JavaScript.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
message = ("Sorry, you are not allowed to enter any spaces");
function nospaces(which) {
x = which.value
if (navigator.appName == "Netscape" ) {
if (e.which == 32) {
alert (message);
return false
}
}
if (navigator.appName == "Microsoft Internet Explorer") {
if (event.keyCode == 32) {
alert (message);
return false;
}
}
x = x.replace(/\s/g,""); // remove the unwanted space
document.myform.textbox.value = x;
}
</script>
</head>

<body>
<form name = "myform" action="#" method="post" >
<input type="text" name ="textbox" id="textbox" onkeydown="nospaces(this)">

</form>
</body>
</html>

Monday, January 6, 2014

Update query in Wordpress Option table cloumn "guid"

This query for wordpress option table quid cloumn url change.
UPDATE wp_options SET option_value = replace(option_value, 'http://www.oldurl', 'http://www.newurl') WHERE option_name = 'home' OR option_name = 'siteurl';

UPDATE wp_posts SET guid = replace(guid, 'http://www.oldurl','http://www.newurl');

UPDATE wp_posts SET post_content = replace(post_content, 'http://www.oldurl', 'http://www.newurl');

UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.oldurl','http://www.newurl');

Saturday, October 6, 2012

Sum of two Textbox values into third Textbox using JQuery

A script that sums up two textbox values using jQuery. **Note that I am not using any validation for textbox values. its very simple.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>  
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
 var textBox1 = $('input:text[id$=TextBox1]').keyup(foo);
           var textBox2 = $('input:text[id$=TextBox2]').keyup(foo);           
            function foo() {

                var value1 = textBox1.val();
                var value2 = textBox2.val();              
                var sum = add(value1, value2);

                $('input:text[id$=TextBox3]').val(sum);
            }
            function add() {

                var sum = 0;
                for (var i = 0, j = arguments.length; i < j; i++) {
                    if (IsNumeric(arguments[i])) {
                        sum += parseFloat(arguments[i]);
                    }
                }
                return sum;
            }
            function IsNumeric(input) {
                return (input - 0) == input && input.length > 0;
            }
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table width="300px" border="1" style="border-collapse:collapse;background-color:#E8DCFF">
    <tr>
        <td>Butter</td>
        <td> <asp:TextBox runat="server" ID="TextBox1"></asp:TextBox></td>
    </tr>
    <tr>
        <td>Cheese</td>
        <td>  
    <asp:TextBox runat="server" ID="TextBox2"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>Total</td>
        <td>
    <asp:TextBox runat="server" ID="TextBox3"></asp:TextBox>
        </td>
    </tr>
    </table>  
    </div>
    </form>
</body>
</html>

Output:


Wednesday, March 14, 2012

Basics of .NET Collections in C#

Introduction

This article focuses on managing collections in .NET Framework 2.0. Collection represents a set of objects that you can access by stepping through each element in turn. The .NET Framework provides specialized classes for managing collection and these classes have rich capability for enhancing your programming experience through better performance and easy maintenance.

Object class is the base class of every type in .NET. All the collections implement IEnumerable interface that is extended by ICollection interface. IDictionary and IList are also interfaces for collection which are derived from ICollection as shown in the diagram.



System.Object

Object class is the base class of every type. All other types directly or indirectly derive from object class. Because of its lofty position, a .NET developer should have a good knowledge of the object class and its members.

  1. Static Methods

     
    object.Equals(object objA, object objB) 

     This method does some testing for null on objA and objB and calls objA.Equals(objB). It returns true if objA and objB are null references or both are the same instance, or if objA.Equals(objB) returns true, otherwise it returns false.


 int n1 = 2;
 int n2 = 3;
 bool result1 = object.Equals(n1, n2); 
 // returns false.
 // because n1 & n2 are value type 
 // and it will be compared by value. 
 
 
 string s1 = "test";
 string s2 = "test";
 bool result2 = object.Equals(s1, s2); 
 
// returns true. s1 & s2 are 
// reference type,
// but Equals(object obj) method of 
// object class is overridden by 
// string class.
// that's why it returns true because 
// s1 and s2 are comparing 
// by their values.
   
 object obj1 = new Person(1, "Test1");
 object obj2 = new Person(1, "Test1");
 bool result3 = object.Equals(obj1, obj2); 
 
// returns false. obj1 & obj2 
// are reference type,
// both are comparing by 
// reference and both are 
// different instances but 
// having same values.