September 18, 2010 5:43:16 PM
Handling path to a file in ASP.Net/C#?
We will have various scenarios where we need to provide the path to a file in code behind. As ASP.Net engine compiles and copies all the binaries to a temp location and execute from there, we need to handle paths in a smart way.
If part of file path resides in a sibling to the aspx page, then we can just use the path beginning from the sibling location. For example, if the project path is C:\TEMP, the code behind file of aspx page is C:\TEMP\TEST.aspx and the image path is C:\TEMP\IMAGES\IMG.jpg, then we can define the path like Server.MapPath("IMAGES\IMG.jpg")
If the file resides somewhere in the project folder, then the best way to define the full path from the project folder beginning with ~. E.g.: Server.MapPath("IMAGES\IMG.jpg"). Here ~ represents the path of the project folder which is C:\TEMP
September 18, 2010 5:43:16 PMTransorming XML file using XSLT in ASP.Net/C#?
Tranforming XML in to HTML in ASP.Net/C# is very simple and pretty straight forward. Following example shows how to tranform XML in to HTML using XSLT and assign the HTML content to a div control.
XPathDocument xPathDoc = new XPathDocument(Server.MapPath("~\\Temp\\Temp.xml"));
XslCompiledTransform xsl = new XslCompiledTransform();
StringBuilder sb = new StringBuilder();
TextWriter tw = new StringWriter(sb);
xsl.Transform(xPathDoc, null, tw);
divControl.InnerHtml = sb.ToString();
September 20, 2010 5:43:16 PM
Using "DataReader" as return type in web services
This is pretty common requirement many times but we can not really return the DataReader from a web service as DataReader is connection oriented. So, the best way is to work with DataSet instead of DataReader.