Monday, October 29, 2012

LINQ Sample

There are three parts in any LINQ Query
a. Data Source  - Could be an array collection, Database TAble, Object Collection, Etc...
b. Creating Query - The LINQ Query Creation
c. Executing Query - Query created doesn't get executed immedietly. It gets executed only during enumeraing the result

Sample

class SampleLINQ
{       
    static void Main()
    {

        //  1. Getting Data source.
        int[] intarray = new int[5] { 3, 1, 9, 63, 17};

        // 2. Query creation.
        var intenumQuery =
            from num in intarray
            where (num % 3) == 0
            select num;

        // 3. Query execution.
        foreach (int intvalue in intenumQuery )
        {
            Console.Write("{0,1} ", num);
        }
    }
}

Wednesday, October 24, 2012

Getting HTML from aspx page and save to file

This post gives the sample code to download the html of an aspx and save in to a file


Add reference to System.Net. namespace.

protected void Download_Click(object sender, EventArgs e)
     {
         WebClient mydwdClient = new WebClient();
         string currentUrl = Request.Url.ToString(); // The page which needs to be downloaded
         string webpageinHTML = String.Empty;
         byte[] bytHTML;
        UTF8Encoding utfenc= new UTF8Encoding();       
         bytHTML = mydwdClient.DownloadData(currentUrl );
        webpageinHTML = utfenc.GetString(bytHTML);         
         Response.Write(webpageinHTML );                   
     }
 

Another method which i found in asp.net forum is

protected override void Render(HtmlTextWriter writer)
{
StringBuilder sbOut = new StringBuilder();
StringWriter swOut = new StringWriter(sbOut);
HtmlTextWriter htwOut = new HtmlTextWriter(swOut);
base.Render(htwOut);
string sOut = sbOut.ToString();

// Send sOut as an Email

writer.Write(sOut);
}