Listing of Utility.cs


using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.Net; // these two added for readUrl using System.IO; using Word = Microsoft.Office.Interop.Word; // for convert to pdf /// <summary> /// Utility functions /// </summary> public class Utility { //--------------------------------------------------------------------------- // decode database string //--------------------------------------------------------------------------- static public string decodeString(string s) { string u = s.Replace("'", "\\'"); u = u.Replace("\n", "\\n"); u = u.Replace("\r", ""); return u; } //--------------------------------------------------------------------------- // encode database string //--------------------------------------------------------------------------- static public string encodeString(string s) { return s.Replace("'", "''"); } //--------------------------------------------------------------------------- // json encode record //--------------------------------------------------------------------------- static public string encodeJson(SqlDataReader reader) { string json = "{"; for (int i=0; i<reader.FieldCount; i++) { if (i > 0) json += ","; string value; if (reader[i].GetType().ToString() == "System.DateTime") { value = ((DateTime)reader[i]).ToString("yyyy'-'MM'-'dd HH':'mm"); value = "'" + decodeString(value) + "'"; } else { value = reader[i].ToString(); if (reader[i].GetType().ToString() == "System.String") value = "'" + decodeString(value) + "'"; else if (reader[i].GetType().ToString() == "System.DBNull") value = "''"; } json += "'" + reader.GetName(i) + "': " + value; } json += "}"; return json; } //--------------------------------------------------------------------------- // load an html page into a string //--------------------------------------------------------------------------- static public string readUrl(string url) { Uri uri = new Uri(url); WebRequest req = WebRequest.Create(uri); WebResponse resp = req.GetResponse(); Stream stream = resp.GetResponseStream(); StreamReader sr = new StreamReader(stream); string s = sr.ReadToEnd(); return (s); } //--------------------------------------------------------------------------- // convert a word file to a pds //--------------------------------------------------------------------------- static public void DocToPdf(string source, string target) { Word.Application newApp = new Word.Application(); object Source = source; object Target = target; object unknown = Type.Missing; object readOnly = true; newApp.Documents.Open(ref Source, ref unknown, ref readOnly, ref unknown, ref unknown, ref unknown, ref unknown, ref unknown, ref unknown, ref unknown, ref unknown, ref unknown, ref unknown, ref unknown, ref unknown, ref unknown); object format = Word.WdSaveFormat.wdFormatPDF; newApp.ActiveDocument.SaveAs(ref Target, ref format, ref unknown, ref unknown, ref unknown, ref unknown, ref unknown, ref unknown, ref unknown, ref unknown, ref unknown, ref unknown, ref unknown, ref unknown, ref unknown, ref unknown); newApp.Quit(ref unknown, ref unknown, ref unknown); } }