Search This Blog

Friday, June 12, 2009

How to bind gridview from dataset using XSLT

-------------------------------
1:-XSLT file: Books.xsl
This is your XSLT file code
-------------------------------
<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">

      <books>
        <xsl:apply-templates select="//book"/>
      </books>

    </xsl:template>
    <xsl:template match="book">
      <xsl:if test="price > 200">
        <book>
         <xsl:attribute name="name">
          <xsl:value-of select="name"/>
         </xsl:attribute>
         <xsl:attribute name="specialization">
          <xsl:value-of select="specialization"/>
         </xsl:attribute>
         <xsl:attribute name="author">
          <xsl:value-of select="author"/>
         </xsl:attribute>
         <xsl:attribute name="publication">
           <xsl:value-of select="publication"/>
         </xsl:attribute>
         <xsl:attribute name="price">
          <xsl:value-of select="price"/>
         </xsl:attribute>
        </book>
      </xsl:if>
</xsl:template>
</xsl:stylesheet>

--------------------------
2:-GridView Control
------------------------
add a gridview control in your web page with xmlDataSource as follows:
<asp:GridView ID="gvXML" runat="server" DataSourceID="XmlDataSource1">
<HeaderStyle BackColor="Gray" />
</asp:GridView>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" TransformFile="~/BindGridView.xsl">
</asp:XmlDataSource>
Note:This control always look data in <book name="ask" price="1220"> </book>
format

----------------------------
3:-C# code
Add this code in your Code-Behind file
--------------------------
SqlConnection myCon = new SqlConnection(@"data source=192.168.2.200\sql2k;uid=sa;pwd=bench;database=DMS1");
SqlDataAdapter da = new SqlDataAdapter("select name, price, specialization, author, publication from book", myCon);
DataSet ds = new DataSet();
ds.DataSetName = "books";
da.Fill(ds, "book");
xmlDataSource2.Data = ds.GetXml();

After doing the above steps you will get the following output










namespecializationauthorpublicationprice
ASP.Net 3.5Web Programming Using C# and VB Bill ThomasWrox560.00
C# 3.5 C# 2008 Powered by Microsoft Microsoft Team Wrox600.00

How to bind an xml datasource to a gridview

XMl Document: books.xml
This is your xml document
-------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<books>
   <book>
     <name> Ajax 2.0 </name>
     <specialization> Programming Language </specialization>
     <author>Wiley ,Bill</author>
     <publication>Wrox</publication>
     <price>200.00</price>
   </book>
   <book>
     <name> ASP.Net 3.5</name>
     <specialization>Web Programming Using C# and VB     </specialization>
     <author>Bill Thomas</author>
     <publication>Wrox</publication>
     <price>560.00</price>
   </book>
   <book>
     <name>C# 3.5 </name>
     <specialization> C# 2008 Powered by Microsoft     </specialization>
     <author> Microsoft Team </author>
     <publication>Wrox</publication>
     <price>600.00</price>
   </book>
</books>
-------------------------------
XSLT file: Books.xsl
This is your XSLT file code
-------------------------------
<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">

      <books>
        <xsl:apply-templates select="//book"/>
      </books>

    </xsl:template>
    <xsl:template match="book">
      <xsl:if test="price > 200">
        <book>
         <xsl:attribute name="name">
          <xsl:value-of select="name"/>
         </xsl:attribute>
         <xsl:attribute name="specialization">
          <xsl:value-of select="specialization"/>
         </xsl:attribute>
         <xsl:attribute name="author">
          <xsl:value-of select="author"/>
         </xsl:attribute>
         <xsl:attribute name="publication">
           <xsl:value-of select="publication"/>
         </xsl:attribute>
         <xsl:attribute name="price">
          <xsl:value-of select="price"/>
         </xsl:attribute>
        </book>
      </xsl:if>
</xsl:template>
</xsl:stylesheet>

--------------------------
GridView Control
------------------------
add a gridview control in your web page with xmlDataSource as follows:
<asp:GridView ID="gvXML" runat="server" DataSourceID="XmlDataSource1">
<HeaderStyle BackColor="Gray" />
</asp:GridView>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/books.xml" TransformFile="~/BindGridView.xsl">
</asp:XmlDataSource>
Note:This control always look data in <book name="ask" price="1220"> </book>
format


After doing the above steps you will get the following output:










namespecializationauthorpublicationprice
ASP.Net 3.5Web Programming Using C# and VB Bill ThomasWrox560.00
C# 3.5 C# 2008 Powered by Microsoft Microsoft Team Wrox600.00

How to tranform xml Document into html table using XSLT and C#

XMl Document: books.xml
-------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<books>
   <book>
     <name> Ajax 2.0 </name>
     <specialization> Programming Language </specialization>
     <author>Wiley ,Bill</author>
     <publication>Wrox</publication>
     <price>200.00</price>
   </book>
   <book>
     <name> ASP.Net 3.5</name>
     <specialization>Web Programming Using C# and VB     </specialization>
     <author>Bill Thomas</author>
     <publication>Wrox</publication>
     <price>560.00</price>
   </book>
   <book>
     <name>C# 3.5 </name>
     <specialization> C# 2008 Powered by Microsoft     </specialization>
     <author> Microsoft Team </author>
     <publication>Wrox</publication>
     <price>600.00</price>
   </book>
</books>
-------------------------------
XSLT file: books.xsl
------------------------------

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html"/>
   <xsl:template match="/">

    <xsl:element name="books">
     <table >
     <tr bgcolor="gray">
        <td>Name</td>
        <td>Specialization</td>
        <td>Author</td>
        <td>Publication</td>
       <td>Price</td>
      </tr>
      <xsl:apply-templates select="//book" ></xsl:apply-templates>
     </table>

    </xsl:element>

  </xsl:template>
   <xsl:template match="book">

     <tr>
     <xsl:if test="price > 400">
     <td width="100">
        <xsl:value-of select="name"/>
     </td>
        <td width="100">
     </td>
     <td width="100">
        <xsl:value-of select="author"/>
     </td>
    <td width="100">
<xsl:value-of select="publication"/>
</td>
       <td width="100">
     </td>
     </xsl:if>
     </tr>

   </xsl:template>


</xsl:stylesheet>



-----------------------------
Method 1

Step1:
add a literal control to the page like below:
<asp:Literal ID="ltr1" runat="server"></asp:Literal>

Step2:
C#
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
public partial class xmlTesting : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
       XPathDocument xPathDoc = new XPathDocument(Server.MapPath("books.xml"));
       XslTransform xTrans = new XslTransform();
       xTrans.Load(Server.MapPath("Books.xsl"));

       StringWriter Sw = new StringWriter();

       xTrans.Transform(xPathDoc, null, Sw);

       ltr1.Text = Sw.ToString();
   }
}
Method 2

add a xml control to the page:
<asp:Xml ID="xml1" runat="server" DocumentSource="~/books.xml" TransformSource="~/Books.xsl"></asp:Xml>

-------------------------------------------------------------------
Output

-------------------------------------------------------------------










namespecializationauthorpublicationprice
ASP.Net 3.5Web Programming Using C# and VB Bill ThomasWrox560.00
C# 3.5 C# 2008 Powered by Microsoft Microsoft Team Wrox600.00

Monday, June 8, 2009

How to handle: 'sys' is undefined.

Add this code in web.config if above error come:

<system.web>
  <httphandlers>
     <add verb="GET" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler" validate="false">
     </add>
   </httphandlers>
</system.web>

Saturday, June 6, 2009

How to crop an image

Click here to Crop Image using   javascript + ASP.Net + C#.

Friday, June 5, 2009

Thursday, June 4, 2009

Special Key and keyCode:

special_keys = {
'esc':27,
'escape':27,
'tab':9,
'space':32,
'return':13,
'enter':13,
'backspace':8,

'scrolllock':145,
'scroll_lock':145,
'scroll':145,
'capslock':20,
'caps_lock':20,
'caps':20,
'numlock':144,
'num_lock':144,
'num':144,

'pause':19,
'break':19,

'insert':45,
'home':36,
'delete':46,
'end':35,

'pageup':33,
'page_up':33,
'pu':33,

'pagedown':34,
'page_down':34,
'pd':34,

'left':37,
'up':38,
'right':39,
'down':40,

'f1':112,
'f2':113,
'f3':114,
'f4':115,
'f5':116,
'f6':117,
'f7':118,
'f8':119,
'f9':120,
'f10':121,
'f11':122,
'f12':123’
}