Friday, January 31, 2014

JAVA : Read Recursive XML Using Dom

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


public class DomFunction {
   
   
    public static void main(String args[])
    {
        try
        {
            DocumentBuilderFactory dbl=DocumentBuilderFactory.newInstance();
            DocumentBuilder db=dbl.newDocumentBuilder();
            Document doc=db.parse("http://www.freakycoders.com/android/t5_data/phonebook.xml");
           
           
           
            printAttributes(doc);
            printElement(doc);
           
           
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    private static void printAttributes(Document doc)
    {
        NodeList nl=doc.getElementsByTagName("*");
        for(int i=0; i < nl.getLength(); i++)
        {
            Element e=(Element)nl.item(i);
            NamedNodeMap nnm=e.getAttributes();
           
            if(nnm != null)
            {
                for(int k=0; k < nnm.getLength(); k++)
                {
                    Node n=nnm.item(k);
                    System.out.println(e.getNodeName() + " : " + n.getNodeName() +" : " + n.getNodeValue());
                }
            }
        }
    }

    private static void printElement(Document doc) {
        NodeList nl=doc.getElementsByTagName("*");
        for(int i=0; i < nl.getLength(); i++)
        {
            Node n=nl.item(i);
            System.out.println(n.getNodeName() + " : " + n.getFirstChild().getTextContent());
           
        }
       
    }


}

Tuesday, January 28, 2014

Select distinct values from a large DataTable column

Method 1:
DataView view = new DataView(table);
   DataTable distinctValues = view.ToTable(true, "id");
Method 2: You will have to create a class matching your datatable column names and then you can use the following extension method to convert Datatable to List
 public static List<T> ToList<T>(this DataTable table) where T : new()
    {
        List<PropertyInfo> properties = typeof(T).GetProperties().ToList();
        List<T> result = new List<T>();

        foreach (var row in table.Rows)
        {
            var item = CreateItemFromRow<T>((DataRow)row, properties);
            result.Add(item);
        }

        return result;
    }

    private static T CreateItemFromRow<T>(DataRow row, List<PropertyInfo> properties) where T : new()
    {
        T item = new T();
        foreach (var property in properties)
        {
            if (row.Table.Columns.Contains(property.Name))
            {
                if (row[property.Name] != DBNull.Value)
                    property.SetValue(item, row[property.Name], null);
            }
        }
        return item;
    }
and then you can get distinct from list using

YourList.Select(x => x.Id).Distinct();

Monday, January 27, 2014

Getting the source of a specific image element with jQuery

<div>
    <img alt="example" src="\images\show.jpg" />
    <img  alt="exampleAll" src="\images\showAll.jpg" />  

</div>


var src = $('img[alt="example"]').attr('src');
alert("source of image with alternate text = example - " + src);


var srcAll = $('img[alt="exampleAll"]').attr('src');
alert("source of image with alternate text = exampleAll - " + srcAll );

Friday, January 24, 2014

JAVA : Read XML Using DOM Parsing.

.xml

<?xml version="1.0" encoding="UTF-8"?>
<book>
    <person>
        <first>bhavesh</first>
        <last>Rangani</last>
        <age>22</age>
    </person>
    <person>
        <first>Vivek</first>
        <last>Patoliya</last>
        <age>21</age>
    </person>
    <person>
        <first>Vishal</first>
        <last>Mangroliya</last>
        <age>21</age>
    </person>
</book>



.JAVA

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;


public class DomParser {
   
    public static void main(String args[])
    {
        try
        {
           
           
            DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
            DocumentBuilder b = f.newDocumentBuilder();
            Document doc = b.parse("/Volumes/Project/Bhavesh/workspace/DomParser/src/person.xml");

            doc.getDocumentElement().normalize();
            System.out.println ("Root element: " +
                        doc.getDocumentElement().getNodeName());
      
           
            NodeList items = doc.getElementsByTagName("person");
            for (int i = 0; i < items.getLength(); i++)
            {
                Node n = items.item(i);
                if (n.getNodeType() != Node.ELEMENT_NODE)
                    continue;
                Element e = (Element) n;
              
                System.out.println("\n");
                System.out.println("Person:");
                NodeList first =e.getElementsByTagName("first");
                Element titleElem = (Element) first.item(0);               
                Node titleNode = titleElem.getChildNodes().item(0);
                System.out.println("First Name :"+titleNode.getNodeValue());
               
                NodeList last =e.getElementsByTagName("last");
                Element titleElem1 = (Element) last.item(0);               
                Node titleNode1 = titleElem1.getChildNodes().item(0);
                System.out.println("Last name :"+titleNode1.getNodeValue()); 
               
                NodeList age =e.getElementsByTagName("age");
                Element titleElem11 = (Element) age.item(0);               
                Node titleNode11 = titleElem11.getChildNodes().item(0);
                System.out.println("Age :"+titleNode11.getNodeValue());
               
               
               
            }
       
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
       
    }
   

}

Wednesday, January 22, 2014

ANDROID : Set Red Green and Blue BackGround Color Using SeekBar.

.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/myScreen"
>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RED" />

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="255"
        android:progress="0" />

    <TextView
        android:id="@+id/countred"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="0" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="GREEN" />

    <SeekBar
        android:id="@+id/seekBar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="255"
        android:progress="0" />

    <TextView
        android:id="@+id/countgreen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="0" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BLUE" />

    <SeekBar
        android:id="@+id/seekBar3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="255"
        android:progress="0" />

    <TextView
        android:id="@+id/countblue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="0" />

</LinearLayout>



.JAVA

 package com.bhavesh.colorchooser;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;


public class MainActivity extends Activity {

   
    SeekBar seekred,seekgreen,seekblue;
    LinearLayout layout1;
    TextView countred,countgreen,countblue;
  
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      
        seekred=(SeekBar)findViewById(R.id.seekBar1);
        seekgreen=(SeekBar)findViewById(R.id.seekBar2);
        seekblue=(SeekBar)findViewById(R.id.seekBar3);
        layout1=(LinearLayout)findViewById(R.id.myScreen);
       
        countred=(TextView)findViewById(R.id.countred);
        countgreen=(TextView)findViewById(R.id.countgreen);
        countblue=(TextView)findViewById(R.id.countblue);
       
       
       
        seekred.setOnSeekBarChangeListener(ChangeColor);
        seekgreen.setOnSeekBarChangeListener(ChangeColor);
        seekblue.setOnSeekBarChangeListener(ChangeColor);
    }
       
    private OnSeekBarChangeListener ChangeColor=new OnSeekBarChangeListener() {
           
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
               
               
            }
           
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
               
               
            }
           
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
       
               
                int R,G,B;
               
                 R = seekred.getProgress();
                 G = seekgreen.getProgress();
                 B = seekblue.getProgress();
                 layout1.setBackgroundColor(0xff000000 + R * 0x10000 + G * 0x100 + B);
               
                 countred.setText(String.valueOf(R));
                 countgreen.setText(String.valueOf(G));
                 countblue.setText(String.valueOf(B));
               
               
            }
    };

   

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
       
       
       
       
       
    }
   
}

Insert Data Into Excel Sql Server 2008

INSERT INTO OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0';'Database=@myfile';,'SELECT *  FROM [@SheetName]')

Tuesday, January 21, 2014

LINQ on Datatable Find where all rows are empty ans remove asp.net

Just move the NOT (!) out one level. You want the items where "all rows are null" is not true, rather than where "all of the rows are not null" is true.
DataTable filteredRows = dt.Rows.Cast<DataRow>()
    .Where(row => !row.ItemArray.All(field => field is System.DBNull))
    .CopyToDataTable();

Monday, January 20, 2014

Loading, Editing, and Saving a Text File in HTML5 Using Javascript

<html>
<body>

<table>
 <tr><td>Text to Save:</td></tr>
 <tr>
  <td colspan="3">
   <textarea id="inputTextToSave" style="width:512px;height:256px"></textarea>
  </td>
 </tr>
 <tr>
  <td>Filename to Save As:</td>
  <td><input id="inputFileNameToSaveAs"></input></td>
  <td><button onclick="saveTextAsFile()">Save Text to File</button></td>
 </tr>
 <tr>
  <td>Select a File to Load:</td>
  <td><input type="file" id="fileToLoad"></td>
  <td><button onclick="loadFileAsText()">Load Selected File</button><td>
 </tr>
</table>

<script type='text/javascript'>

function saveTextAsFile()
{
 var textToWrite = document.getElementById("inputTextToSave").value;
 var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
 var fileNameToSaveAs = document.getElementById("inputFileNameToSaveAs").value;

 var downloadLink = document.createElement("a");
 downloadLink.download = fileNameToSaveAs;
 downloadLink.innerHTML = "Download File";
 if (window.webkitURL != null)
 {
  // Chrome allows the link to be clicked
  // without actually adding it to the DOM.
  downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
 }
 else
 {
  // Firefox requires the link to be added to the DOM
  // before it can be clicked.
  downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
  downloadLink.onclick = destroyClickedElement;
  downloadLink.style.display = "none";
  document.body.appendChild(downloadLink);
 }

 downloadLink.click();
}

function destroyClickedElement(event)
{
 document.body.removeChild(event.target);
}

function loadFileAsText()
{
 var fileToLoad = document.getElementById("fileToLoad").files[0];

 var fileReader = new FileReader();
 fileReader.onload = function(fileLoadedEvent) 
 {
  var textFromFileLoaded = fileLoadedEvent.target.result;
  document.getElementById("inputTextToSave").value = textFromFileLoaded;
 };
 fileReader.readAsText(fileToLoad, "UTF-8");
}

</script>

</body>
</html>

Simple way to export SQL Server data to Text Files

EXEC xp_cmdshell 'bcp "SELECT * FROM ACESRepView.Users.XmlData" queryout "d:\data.txt" -S "server name" -U username  -P  passwrd -c'

write text file in sql server 2008

DECLARE @cmd varchar(256), @var varchar(50)
SET @var = 'Hello world!' +CHAR(13)+CHAR(10)+ 'This is second line'
print @var
SET @cmd = 'echo>d:\myfile.txt ' + @var 
EXEC master..xp_cmdshell @cmd, no_output

Enable 'xp_cmdshell' SQL Server

-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GO

Saturday, January 18, 2014

Export HTML to Excel ap.net c#

aspx page code

<form id="form1" runat="server">
    <div>
        <div runat="server" id="ExportDiv">
            <span class="SubSectionHeading">First Table:</span><table rules="all" cellspacing="0"
                cellpadding="3" width="922">
                <tr>
                    <th width="200px">
                        Item Name
                    </th>
                    <th width="100px">
                        Price
                    </th>
                    <th width="100px">
                        Market Price
                    </th>
                    <th width="100px">
                        Actual Price
                    </th>
                    <th width="100px">
                        Difference
                    </th>
                    <th width="200px">
                        Notes
                    </th>
                </tr>
                <tr>
                    <td>
                        Item 1
                    </td>
                    <td>
                        &#8364;78.00
                    </td>
                    <td>
                        &#8364;67.00
                    </td>
                    <td>
                        &#8364;67.00
                    </td>
                    <td>
                        &#8364;0.00
                    </td>
                    <td>
                        description of Item
                    </td>
                </tr>
                <tr>
                    <td>
                        Item 2
                    </td>
                    <td>
                        &#8364;90.00
                    </td>
                    <td>
                        &#8364;789.00
                    </td>
                    <td>
                        &#8364;90.00
                    </td>
                    <td>
                        -&#8364;699.00
                    </td>
                    <td>
                        description of Item
                    </td>
                </tr>
                <tr>
                    <td>
                        Item 3
                    </td>
                    <td>
                        &#8364;900.00
                    </td>
                    <td>
                        &#8364;700.00
                    </td>
                    <td>
                        &#8364;8.00
                    </td>
                    <td>
                        -&#8364;692.00
                    </td>
                    <td>
                        description of Item
                    </td>
                </tr>
                <tr>
                    <td>
                        Item 4
                    </td>
                    <td>
                        &#8364;789.00
                    </td>
                    <td>
                        &#8364;78.00
                    </td>
                    <td>
                        &#8364;980.00
                    </td>
                    <td>
                        &#8364;902.00
                    </td>
                    <td>
                        description of Item
                    </td>
                </tr>
                <tfoot>
                    <tr>
                        <td style="text-align: center;">
                            Table 1 Total
                        </td>
                        <td>
                            &#8364;1,857.00
                        </td>
                        <td>
                            &#8364;1,634.00
                        </td>
                        <td>
                            &#8364;1,145.00
                        </td>
                        <td>
                            -&#8364;489.00
                        </td>
                        <td>
                        </td>
                    </tr>
                    <tfoot>
            </table>
            </br><span class="SubSectionHeading">Second Table:</span><table rules="all" cellspacing="0"
                cellpadding="3" width="922">
                <tr>
                    <tr>
                        <th width="200px">
                            Item Name
                        </th>
                        <th width="100px">
                            Price
                        </th>
                        <th width="100px">
                            Market Price
                        </th>
                        <th width="100px">
                            Actual Price
                        </th>
                        <th width="100px">
                            Difference
                        </th>
                        <th width="200px">
                            Notes
                        </th>
                    </tr>
                    <tr>
                        <td>
                            Item 1
                        </td>
                        <td>
                            &#8364;45.00
                        </td>
                        <td>
                            &#8364;67.00
                        </td>
                        <td>
                            &#8364;8.00
                        </td>
                        <td>
                            -&#8364;59.00
                        </td>
                        <td>
                            description of Item
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Item 2
                        </td>
                        <td>
                            &#8364;600.00
                        </td>
                        <td>
                            &#8364;899.00
                        </td>
                        <td>
                            &#8364;65.00
                        </td>
                        <td>
                            -&#8364;834.00
                        </td>
                        <td>
                            description of Item
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Item 3
                        </td>
                        <td>
                            &#8364;7.00
                        </td>
                        <td>
                            &#8364;9.00
                        </td>
                        <td>
                        </td>
                        <td>
                        </td>
                        <td>
                            description of Item
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Item 4
                        </td>
                        <td>
                            &#8364;78.00
                        </td>
                        <td>
                            &#8364;90.00
                        </td>
                        <td>
                            &#8364;67.00
                        </td>
                        <td>
                            -&#8364;23.00
                        </td>
                        <td>
                            description of Item
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Item 5
                        </td>
                        <td>
                            &#8364;789.00
                        </td>
                        <td>
                            &#8364;980.00
                        </td>
                        <td>
                            &#8364;67.00
                        </td>
                        <td>
                            -&#8364;913.00
                        </td>
                        <td>
                            description of Item
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Item 6
                        </td>
                        <td>
                            &#8364;6.00
                        </td>
                        <td>
                            &#8364;6.00
                        </td>
                        <td>
                            &#8364;78.00
                        </td>
                        <td>
                            &#8364;72.00
                        </td>
                        <td>
                            description of Item
                        </td>
                    </tr>
                    <tfoot>
                        <tr>
                            <td style="text-align: center;">
                                Table 2 Total
                            </td>
                            <td>
                                &#8364;1,525.00
                            </td>
                            <td>
                                &#8364;2,051.00
                            </td>
                            <td>
                                &#8364;285.00
                            </td>
                            <td>
                                -&#8364;1,757.00
                            </td>
                            <td>
                            </td>
                        </tr>
                        <tfoot>
            </table>
            </br><span class="SubSectionHeading">Total:</span><table rules="all" cellspacing="0"
                cellpadding="3" class="rgMasterTableTotal" width="630">
                <tr>
                    <th width="200px">
                        Table
                    </th>
                    <th width="100px">
                        Price
                    </th>
                    <th width="100px">
                        Market Price
                    </th>
                    <th width="100px">
                        Actual Price
                    </th>
                    <th width="100px">
                        Difference
                    </th>
                </tr>
                <tr>
                    <td style="text-align: center;">
                        Table 1
                    </td>
                    <td>
                        &#8364;1,857.00
                    </td>
                    <td>
                        &#8364;1,634.00
                    </td>
                    <td>
                        &#8364;1,145.00
                    </td>
                    <td>
                        -&#8364;489.00
                    </td>
                </tr>
                <tr>
                    <td style="text-align: center;">
                        Table 2
                    </td>
                    <td>
                        &#8364;1,525.00
                    </td>
                    <td>
                        &#8364;2,051.00
                    </td>
                    <td>
                        &#8364;285.00
                    </td>
                    <td>
                        -&#8364;1,757.00
                    </td>
                </tr>
                <tfoot>
                    <tr style="background-color: #404040; color: White;">
                        <td style="text-align: center;">
                            Grand Total
                        </td>
                        <td>
                            &#8364;3,382.00
                        </td>
                        <td>
                            &#8364;3,685.00
                        </td>
                        <td>
                            &#8364;1,430.00
                        </td>
                        <td>
                            -&#8364;2,246.00
                        </td>
                    </tr>
                    <tfoot>
            </table>
        </div>
        <asp:Button runat="server" ID="ExportToExcelButton" OnClick="ExportToExcelButton_Click"
            Text="Export To Excel" />
    </div>
    </form>

--------------------------------------------------------------
code hehind

protected void ExportToExcelButton_Click(object sender, EventArgs e)
    {
        Response.AppendHeader("content-disposition", "attachment;filename=ExportedHtml.xls");
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/vnd.ms-excel";
        this.EnableViewState = false;
        Response.Write(ExportDiv.InnerHtml);
        Response.End();

    }

Set "everyone" Permission to Folder using C# in Asp.Net

string redirectionFolder = @"D:\test\subtest\";

FileSystemAccessRule everyOne = new FileSystemAccessRule("Everyone", FileSystemRights.FullControl,AccessControlType.Allow);
DirectorySecurity dirSecurity = new DirectorySecurity(redirectionFolder, AccessControlSections.Group);
dirSecurity.AddAccessRule(everyOne);
Directory.SetAccessControl(redirectionFolder, dirSecurity);


Note:
If you get the error "Attempted to perform an unauthorized operation"

add the tag (<identity impersonate="true"/>) in your web.config file.

Opps Part 1 : Abstraction

  Abstraction in C# is a fundamental concept of object-oriented programming (OOP) that allows developers t...