Monday, December 03, 2007

Swami Vivekananda – Chicago Speech - Audio

Swami Vivekananda was the first Spiritual Master who introduced the knowledge of Vedas in the West. He started this movement of spiritual connection of East and West. Later, many spiritual masters followed his path and still continuing. I had only read his speeches in books but today I found audio version on web which were given by beloved Swami Vivekananda more than a century ago (Sept 11th - 27th, 1893) at World Parliament of Religions in Chicago. It gives me an immense pleasure to collect these links from Web and put them together for everyone. [Read More]

Friday, November 02, 2007

Using XMLWriter and XMLReader

This is a simple example for demonstrating how to use XMLReader or XMLWriter for reading or writing a XML file or XML String. I am using a Customer Entity which was created in previous examples.

XML File/String:

      <?xml version="1.0" encoding="utf-8" standalone="yes" ?>
          <XML_RESPONSE STATUS="1" DESCRIPTION="Success">
                   <CUSTOMERS>
                              <CUSTOMER ID="1" NAME="Joe Smith" DOB="1/1/1980" />
                              <CUSTOMER ID="2" NAME="Anita Wang" DOB="1/1/1982" />
                    </CUSTOMERS>
       </XML_RESPONSE>
         
      Required Imports Statements
      Imports System.Xml 
      Imports System.IO

      XML Writer:

           Private Function CreateCustomerXML(ByVal statusID As Integer, _
                                    ByVal statusDescription As String, _ 

                                    ByVal customers As List(Of BECustomer)) As String

        Dim xmlDocString As New System.Text.StringBuilder

        Dim xmlWriterSettings As New XmlWriterSettings()

        xmlWriterSettings.Indent = True

        xmlWriterSettings.IndentChars = vbTab

 

        Using xmlDocWriter As XmlWriter = XmlWriter.Create(xmlDocString, xmlWriterSettings)

            'Using xmlDocWriter As XmlWriter = XmlWriter.Create("C:\Temp\CustXml.xml")

            xmlDocWriter.WriteStartDocument(True)

            xmlDocWriter.WriteStartElement("XML_RESPONSE")

            xmlDocWriter.WriteAttributeString("STATUS", statusID.ToString)

            xmlDocWriter.WriteAttributeString("DESCRIPTION", statusDescription)

 

            xmlDocWriter.WriteStartElement("CUSTOMERS")

            For i As Integer = 0 To customers.Count - 1

                xmlDocWriter.WriteStartElement("CUSTOMER")

                xmlDocWriter.WriteAttributeString("ID", customers(i).CustID.ToString)

                xmlDocWriter.WriteAttributeString("NAME", customers(i).CustName.ToString)

                xmlDocWriter.WriteAttributeString("DOB", customers(i).CustDOB.ToShortDateString)

                xmlDocWriter.WriteEndElement()

            Next

 

            xmlDocWriter.WriteEndElement()

            xmlDocWriter.WriteEndDocument()

            xmlDocWriter.Close()

        End Using

 

        Return xmlDocString.ToString()

    End Function

     XML Reader:

 

    Private Function ReadCustomerXML(ByVal xmlDocString As String) As List(Of BECustomer)

        Dim customers As New List(Of BECustomer)

        Dim status As Integer = 0

        Dim statusDescription As String = String.Empty

        Dim uniEncoding As New System.Text.UnicodeEncoding()

        Dim stringBytes As Byte() = uniEncoding.GetBytes(xmlDocString)

        Dim xmlDocSteam As New MemoryStream(stringBytes, 0, stringBytes.Length)

 

        Using xmlDocReader As XmlReader = XmlReader.Create(xmlDocSteam)

            'Using xmlDocReader As XmlReader = XmlReader.Create("C:\Temp\Custxml.xml")

            xmlDocReader.Read()

            xmlDocReader.MoveToContent()

            If xmlDocReader.HasAttributes Then

                xmlDocReader.MoveToAttribute("STATUS")

                status = CInt(xmlDocReader.Value)

                xmlDocReader.MoveToAttribute("DESCRIPTION")

                statusDescription = xmlDocReader.Value

            End If

 

            While xmlDocReader.Read()

                If xmlDocReader.NodeType = XmlNodeType.Element _

                      AndAlso xmlDocReader.Name = "CUSTOMER" Then

                    Dim custRec As New BECustomer

                    If xmlDocReader.HasAttributes Then

                        xmlDocReader.MoveToAttribute("ID")

                        custRec.CustID = CInt(xmlDocReader.Value)

                        xmlDocReader.MoveToAttribute("NAME")

                        custRec.CustName = xmlDocReader.Value

                        xmlDocReader.MoveToAttribute("DOB")

                        custRec.CustDOB = CDate(xmlDocReader.Value)

                        customers.Add(custRec)

                    End If

                End If

            End While

        End Using

        xmlDocSteam.Close()

 

        Return customers

 

      End Function

 

 

     Test :

 

       Private Sub TestReadWriteXML()

 

        Dim xmlDocString As String = String.Empty

        Dim custs As New List(Of BECustomer)

 

        Dim cust1 As New BECustomer(1, "Joe Smith", #1/1/1980#)

        custs.Add(cust1)

        Dim cust2 As New BECustomer(2, "Anita Wang", #1/1/1982#)

        custs.Add(cust2)

 

        'Create XML String

        xmlDocString = CreateCustomerXML(1, "Success", custs)

        MsgBox(xmlDocString)

 

        'Read XML String

        custs = ReadCustomerXML(xmlDocString)

        MsgBox(custs.Count.ToString)

       End Sub

 

Friday, October 19, 2007

Implementing Data Transfer Objects

When you separate your business layer from data access layer in two different assembly, you come across a situation as which data transfer method will be best for me. In this example, I went through various data transfer object currently available in .NET such as XML, DataSet, DataTable, ArrayList and Custom Data Transfer Object. Each option has its pros and cons, so depending on project one should choose as what best fits the need. In the following code I have tried to use all the four possibilities. Read More

Sunday, August 05, 2007

Freeze Column in GridView

The following example will demonstrate as how to freeze or fix one or more column in a GridView. I have used stylesheet to achieve this result. At present this solution only works in IE 7.0. The data source is a xml file containing product sales result.

This example freezes the first column “Product Name” and allows scrolling GridView horizontally to see monthly sales for each product.

XML Data Format: Sample File

 <?xmlversion="1.0" encoding="utf-8" ?>
<ProductSales>
<Product ID="1" Name="Product 1" Year ="2007">
    <Jan>123,200</Jan>
    <Feb>333,300</Feb>
    <Mar>332,400</Mar>
    <Apr>222,200</Apr>
    <May>444,250</May>
    <Jun>234,300</Jun>
    <Jul>229,233</Jul>
    <Aug>111,333</Aug>
    <Sep>212,646</Sep>
    <Oct>423,266</Oct>
    <Nov>987,332</Nov>
    <Dec>282,234</Dec>
</Product>
</ProductSales>

  Page Name: FreezeColumnGridView.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="FreezeColumnGridView.aspx.vb" Inherits="FreezeColumnGridView" %>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Product Sales</title>
   
 <%-- Ideally you can use the following link to have stylesheet as a separate file
    <link href="App_Themes/StyleSheet.css" rel="stylesheet" type="text/css" /> --%>
   
    <style type="text/css">
       
    body
    {
          font-family: Arial,Verdana, Helvetica;
          background-color:White;  
    }
H2    {    
          font-family: Arial,Verdana, Helvetica;
          font-size:    18px;
          font-weight:bold;  
      }
.OddColumn
    {
        font-family: Arial,Verdana, Helvetica;
        font-size: 14px;
        font-weight: normal;
        border-style: solid;
        border-width: 1px;
        border-color: #999966;
        width:60px;   
    }
.EvenColumn
    {
        font-family: Arial,Verdana, Helvetica;
        font-size: 14px;
        font-weight: normal;
        border-style: solid;
        border-width: 1px;
        border-color: #999966;
        width:60px;
        background-color:Silver;       
    }
.StaticColumn
      {    
          font-family: Arial,Verdana, Helvetica;
        font-size: 14px;
        font-weight: normal;   
          border: none;
          position: relative;
      }
     
#GridViewContainer
    {
          overflow: scroll;  
          margin-bottom: 14px;     
          width:600px;
    }
.GridViewHeader
      {
            font-family: Arial,Verdana, Helvetica;
            font-size: 14px;       
            font-weight:bold;
            color: black;          
        background-color:#aaaadd;             
    }
.GridViewRow
      {
            font-family: Arial,Verdana, Helvetica;
            font-size: 14px;       
            color: black;                
        background-color:#ccccff;                  
    }
   
.GridViewAltRow
      {
            font-family: Arial,Verdana, Helvetica;         
            font-size: 14px;
            color: black;          
            background-color:white;
    }
   
    </style>
   
</head>
<body>
    <form id="frmTestGridView" runat="server">
        <h2>Freeze column in GridView</h2>
        <div id="GridViewContainer">                          
               <asp:GridView ID="gvwSales" runat="server" DataKeyNames="ID"
                EmptyDataText="No record found." AutoGenerateColumns="false" AllowPaging="False">                       
                       <RowStyle CssClass="GridViewRow" />
                       <AlternatingRowStyle CssClass="GridViewAltRow" />
                      <HeaderStyle CssClass="GridViewHeader" />
                        <Columns>                                                             
                               <asp:BoundField DataField="Name" HeaderText="Product Name" ReadOnly="true" ItemStyle-CssClass="StaticColumn" HeaderStyle-CssClass="StaticColumn"
                                                     HeaderStyle-Font-Bold="true" ItemStyle-Wrap="false" HeaderStyle-Wrap="false"/>                   
                               <asp:BoundField DataField="Jan" HeaderText="Jan" ReadOnly="true" ItemStyle-CssClass="EvenColumn" HeaderStyle-CssClass="EvenColumn" HeaderStyle-Font-Bold="true" />                   
                               <asp:BoundField DataField="Feb" HeaderText="Feb" ReadOnly="true" ItemStyle-CssClass="OddColumn" HeaderStyle-CssClass="OddColumn" HeaderStyle-Font-Bold="true"/>                   
                               <asp:BoundField DataField="Mar" HeaderText="Mar" ReadOnly="true" ItemStyle-CssClass="EvenColumn" HeaderStyle-CssClass="EvenColumn" HeaderStyle-Font-Bold="true"/>                   
                               <asp:BoundField DataField="Apr" HeaderText="Apr" ReadOnly="true" ItemStyle-CssClass="OddColumn" HeaderStyle-CssClass="OddColumn" HeaderStyle-Font-Bold="true"/>                    
                               <asp:BoundField DataField="May" HeaderText="May" ReadOnly="true" ItemStyle-CssClass="EvenColumn" HeaderStyle-CssClass="EvenColumn" HeaderStyle-Font-Bold="true"/>                   
                               <asp:BoundField DataField="Jun" HeaderText="Jun" ReadOnly="true" ItemStyle-CssClass="OddColumn" HeaderStyle-CssClass="OddColumn" HeaderStyle-Font-Bold="true"/>                   
                               <asp:BoundField DataField="Jul" HeaderText="Jul" ReadOnly="true" ItemStyle-CssClass="EvenColumn" HeaderStyle-CssClass="EvenColumn" HeaderStyle-Font-Bold="true"/>                   
                               <asp:BoundField DataField="Aug" HeaderText="Aug" ReadOnly="true" ItemStyle-CssClass="OddColumn" HeaderStyle-CssClass="OddColumn" HeaderStyle-Font-Bold="true"/>                   
                               <asp:BoundField DataField="Sep" HeaderText="Sep" ReadOnly="true" ItemStyle-CssClass="EvenColumn" HeaderStyle-CssClass="EvenColumn" HeaderStyle-Font-Bold="true"/>                   
                               <asp:BoundField DataField="Oct" HeaderText="Oct" ReadOnly="true" ItemStyle-CssClass="OddColumn" HeaderStyle-CssClass="OddColumn" HeaderStyle-Font-Bold="true"/>                   
                               <asp:BoundField DataField="Nov" HeaderText="Nov" ReadOnly="true" ItemStyle-CssClass="EvenColumn" HeaderStyle-CssClass="EvenColumn" HeaderStyle-Font-Bold="true"/>                   
                               <asp:BoundField DataField="Dec" HeaderText="Dec" ReadOnly="true" ItemStyle-CssClass="OddColumn" HeaderStyle-CssClass="OddColumn" HeaderStyle-Font-Bold="true"/>                                                                               
                         </Columns>
                    </asp:GridView> 
           </div>   
    </form>
</body>
</html>
 
 

  Code Behind : FreezeColumnGridView.aspx.vb

ProtectedSub Page_Load(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMe.Load

 

            IfNot Page.IsPostBack Then

                Dim salesData AsNew System.Data.DataSet

                salesData.ReadXml("C:\Example\Vishwa.Example.WebSite\App_Data\ProductSales.xml")

                Me.gvwSales.DataSource = salesData

                Me.gvwSales.DataBind()

            EndIf

        EndSub

 

  Output at the runtime: