Wednesday, July 1, 2009

How to reverse contents of a file in windows

I recently needed to mimic the linux functionality of being able to reverse the contents of any file, on windows. After playing with a few options, I found vb-scripting to be the least painful way to do this. See below for details:

Step 1:
Save the script below as a .vbs file for example ReverseStream.vbs

' ------- BEGIN CALLOUT A -------Dim Stack: Set Stack = CreateObject("System.Collections.Stack")' ------- END CALLOUT A -------
' ------- BEGIN CALLOUT B -------Do While Not WScript.StdIn.AtEndofStream Stack.Push WScript.StdIn.ReadLineLoop' ------- END CALLOUT B -------
' ------- BEGIN CALLOUT C -------WScript.StdOut.WriteLine Join(Stack.ToArray, vbCrLf)' ------- END CALLOUT C -------

Step 2:
To run the script use the following command on the cmd shell

c:\>cscript c:\ReverseStream.vbs c:\Reversefile.xml

Wednesday, June 10, 2009

Download from Datatable to EXCEL/CSV

Code snippet to download from a datatable to Excel/CSV, for a given number of rows from a machine where Excel may not necessarily be installed.(I.E if you dont have office the file loads from any other editor that will load a CSV)


protected void ExportToExcel()
{
string query = @"AS
(
SELECT *, RANK() OVER(ORDER BY [column_name]) AS RankNumber FROM [table_name]
)
SELECT * FROM CTE WHERE RankNumber >= @StartRow AND RankNumber <= @EndRow";
SqlCommand cmd = new SqlCommand(query);
cmd.CommandType = CommandType.Text;
cmd.Connection = new SqlConnection("");
cmd.Parameters.AddWithValue("@StartRow", Page.Request.QueryString["StartRow"].ToString());
cmd.Parameters.AddWithValue("@EndRow", Page.Request.QueryString["EndRow"].ToString());
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
string name = "CSVExport";
HttpContext context = HttpContext.Current;
context.Response.Clear();
foreach (DataColumn column in dt.Columns)
{
context.Response.Write(column.ColumnName + ",");
}
context.Response.Write(Environment.NewLine);
foreach (DataRow row in dt.Rows)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
context.Response.Write(row[i].ToString().Replace(",", string.Empty) + ",");
}
context.Response.Write(Environment.NewLine);
}
}

Tuesday, June 9, 2009

How to retain scrool position in tree view after postback

I have been looking for an elegant solution to this problem for a while here is the cleanest one I have found so far:

///
/// Handles the click event when a tree node is selected
///

/// treeview reference
///
protected void TreeView_SelectedNodeChanged(object sender, EventArgs e)
{
// Cast the sender to a treeview
TreeView T = sender as TreeView;
//Execute required code
//Finally register the start of JScript to load to selected treeview
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "selectNode", "var elem = document.getElementById('" + T.ClientID + "_SelectedNode');var node = document.getElementById(elem.value);node.scrollIntoView(true);elem.scrollLeft=0;", true);
}