Friday, June 5, 2009

Display XML string on Browser

Initially I thought that it is very simple task. I was surprised myself that it is not so simple as I thought.

My requirement: XMLs are stored in SQL Server 2005 database in XML data type field. On web page, I wanted to display a hyperlink and when user click to this hyperlink, then the xml should display for the selected ID.

My Solution:
Created a sample web application, here is the code snippet of Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ShowXmlOnPage._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd >">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"></a>;
<html xmlns="<a href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>" ><head runat="server"> <title>Untitled Page</title></head><body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>

And then subsequent code behind page(Default.aspx.cs) is:


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Xml.Xsl;

namespace ShowXmlOnPage
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(MapPath("Vendor.xml"));
Response.Write(LoadXmldocument(doc));
}
private string LoadXmldocument(XmlDocument xmlDocument)
{
System.IO.Stream s = System.IO.File.OpenRead(MapPath("defaultss.xsl"));
System.Xml.XmlReader reader = System.Xml.XmlReader.Create(s);
XslCompiledTransform transform = new XslCompiledTransform(true);
transform.Load(reader);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
XmlWriter writer = XmlWriter.Create(sb);
transform.Transform(xmlDocument, writer);
return sb.ToString();
}
}
}

The .NET Solution file require a couple of other files too.
1. Vendor.xml file - Here is the XML file:
<?xml version="1.0" encoding="utf-16" ?><Response> <Success> <Operation>PlaceOrderCredit,PlaceSalesOrderPOs,Payment</Operation> <SORefNumber /> <PONumber /> <PONumber /> <SalesReturnRefNumber /> <CreditRefNumber /> <RMARefNumber /> <PaymentRefNumber /> </Success> <Failure> <Operation>PlaceOrderCredit,PlaceSalesOrderPOs,Payment</Operation> <SORefNumber /> <PONumber /> <PONumber /> <SalesReturnRefNumber /> <CreditRefNumber /> <RMARefNumber /> <PaymentRefNumber /> <ErrorMessage /> </Failure></Response>

2. defaultss.xsl - This file I downloaded from "http://blorgh.files.wordpress.com/2006/10/defaultss.txt". Thanks to the author who has modified this xsl file. Save the file as "defaultss.xsl" - that is what I referenced in the code.

And that's it. This is the sample application where Vendor.xml is physically present in the solution folder. It is required to have defaultss.xsl present in the web folder too.

Happy Coding!!!

Wednesday, May 20, 2009

Loading excel data column (alpha-numeric) using OLE DB adapter

Problem: One of the excel data column has alpha-numeric data stored. While loading the data using Ole DB adapter, adapter preset the data type to numeric and subsequently doesn't read the alpha numeric content.

Solution: While defining the connection string, define it as:
string connectionString = "Provider=Microsoft.Jet.OleDb.4.0;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';";
connectionString += "data source='C:\Temp\FileTobeProcessed.xls';";

we generally define the extended properties = 'Excel 8.0;' only and forget to add, if excel file has header, or treat the column data type as alpha-numeric (add IMEX=1 in extended properties).

It helped me a lot of places, mostly, when I used Dynamics GP eConnect to transfer Items from One bin location to another, and item number data was alpha-numeric.

Tuesday, March 3, 2009

MATSHITA DVD +- RW UJ 857G Driver Problem/Solution

I have DELL XPS 1330 with MATSHITA DVD UJ 857G DVD Drive. Because of nVidia problems, the mother board is replaced three times (thanks GOD that I took 3 years warranty on it), so I didnt pay a single penny so far on repairs.
Now I formatted it with Vista Ultimate 64bit and later I found that Vista is not detecting drivers for DVD.
I searched a lot but didnt find any suitable driver.
In device manager for the drive, I was getting error 32, and googled it based on the error description,and then I found the following posting:
http://www.pchell.com/hardware/cd_drive_error_code_39.shtml

It talks about error 39, still I did the same what the post says... and deleted entry LowerFilters - PxHlpa64. Restarted the computer, and noticed that drive is working just fine.

Tuesday, January 6, 2009

Read QueryString Parameter and use in CAML to display specific blog category Posts

Well, SharePoint is full of challenges, and one day before I was stuck on this problem. I modified Blog Category page, and hence the page became customized. Now when I click a specific blog category, the page was displaying all the posts having no category defined. I was confused what is happening. Apparently I looked the CAML Query in 'SelectCommand' attribute and found the CAML as:
<View><Query><OrderBy><FieldRef Name="PublishedDate" Ascending="FALSE"/><FieldRef Name="ID" Ascending="FALSE"/></OrderBy><Where><Eq><FieldRef Name="PostCategory"/><Value Type=""><GetVar Scope="Request" Name="Name"/></Value></Eq></Where></Query></View>"

Looked Ok to me! What is wrong in there. Noticed that how <GetVar> is working here. I thought of modifying the CAML query, but thought to investigate further using SharePoint Designer. BTW, here I am modifying WebPartPages:DataFormWebPart that has my custom formatting.
  1. Now clicked this DataFormWebPart and a left arrow appears on the right of the same window, click it.
  2. The window is titled as 'Common Data View Tasks'.
  3. Click the top option - Filter.
  4. When you click this 'Filter' option, 'Filter Criteria' window will open.
  5. Click 'Click here to add a new clause...'.
  6. Select the Field Name - In my case it was 'Category'.
  7. Select Comparison value as 'Equals'
  8. Select Value field and drop down option, click the last option...Create a new parameter. As soon as you click this new parameter option from drop down list, a new window will open titled as 'Data View Properties'.
  9. Under Parameters, Type 'CategoryName' for 'Param1'. You can name anything here.
  10. For Source Column, on the right side, select 'Query String' from the 'Parameter Source' drop down combo box.
  11. As soon as you select 'Query String' from drop down, two new text boxes will appear. One is to define the query string variable name. Category page, by default, has 'Name' as Query String value. So Type 'Name' in Query String Variable Text Box.
  12. Leave 'Default Value' text box empty.
  13. Click Ok to close 'Data View Properties' window.
  14. Click Ok to close 'Filter Criteria' window.

You are done here.

And finally when you look back your CAML query, it will look like the following:

<View><Query><OrderBy><FieldRef Name="PublishedDate" Ascending="FALSE"/><FieldRef Name="ID" Ascending="FALSE"/></OrderBy><Where><Eq><FieldRef Name="PostCategory"/><Value Type="Text">{CategoryName}</Value></Eq></Where></Query></View>

And when I ran my category page again for a specific category, filtering was happening correctly.

I am relieved!!

References that I used:

http://msdn.microsoft.com/en-us/library/aa218649.aspx - This I looked to understand the parameter binding relationship and from here I got the clue to look for 'Query String' option as one of the ParameterBindings Name is 'filterParam'. It is interesting article though.