Wednesday, March 30, 2016

Code to search CSV files and provide user the option to download them

You have some CSV files stored in a folder on your server and you want to let the user download each separately. You will need a ASP.NET placeholder (ID="Downloads") on your UI.

 private void CheckForDownloadFiles(string filePath)
    {
        DirectoryInfo d = new DirectoryInfo(filePath);  
        FileInfo[] Files = d.GetFiles("*.csv"); //Getting CSV files
        Downloads.Controls.Clear();
        string str = ""; int i = 0;
        foreach (FileInfo file in Files)
        {
            str = str + ", " + file.Name;
            i++;
            CreateLinkButtonAndLabel(file.Name,i, filePath);
        }
    }


private void CreateLinkButtonAndLabel(string fileName, int count, string filePath)
    {

        Downloads.Controls.Add(new LiteralControl("<br />"));
        LinkButton newDownload = new LinkButton();
        newDownload.ID = "DownloadBtn"+count;
        newDownload.CommandArgument = filePath+fileName;
        newDownload.Click += new EventHandler(DownloadCSV);
        newDownload.CssClass += "btn btn-success btn-xs";
        newDownload.Text = "<img src='../Images/icons/csv-file.gif' alt='Download CSV File' /> Download CSV";
        Downloads.Controls.Add(newDownload);
        Label label = new Label();
        label.ID = "DownloadLabel" + count;       
        label.Text = " "+fileName+" ";
        Downloads.Controls.Add(label);
}


protected void DownloadCSV(object sender, EventArgs e)
    {

        LinkButton btn = (LinkButton)sender;
        string fullFilePath = btn.CommandArgument;
        if(fileName.Contains("csv"))
            Response.Redirect(fullFilePath);
  }

This will create a button and display the name of your file next to it:





Sunday, March 27, 2016

Badman vs. Superbad

What a bad movie!

I should have stayed at home and done some coding.

Or just slept.

When will I learn?

Saturday, March 26, 2016

Beneficiaries vs. Dependents


I work in the health insurance domain and believe me it helps to have a little bit of domain knowledge. Every employee who is enrolled in a benefit plan can have dependents and beneficiaries attached to the plans.

Source: http://letsallgetcovered.org/ 



You enter dependents in order to make them eligible for benefits such as medical insurance coverage. 


You enter beneficiaries to identify individuals who are entitled to receive benefits in the event of an employee's death, for example, life insurance or 401(k) beneficiaries.


Currently, in our system we have different pages through which the beneficiaries and dependents are added into the system. Further, there are different tables in which they are stored in the back-end. 


References:

1. https://docs.oracle.com/cd/E26228_01/doc.93/e21932/ww_depend_benefic.htm#WEAHB173


Tuesday, March 22, 2016

Bootstrap makes overflow hidden automatically.

My Div

 <div class="form-group" >

contained a table far too wide. But instead of showing the horizontal scrollbar, the table was just brutally cut off and we couldn't see it.





Simple fix:

 <div class="form-group" style="overflow:scroll" >

Of course, later I decided to use an IFRAME and so it didn't matter anymore!

Iframe's are useful when your code takes a while to load.

I was reading a CSV file and converting it to a HTML table to display it. However, since the file was large, it was taking a while for the code to execute and the page to be rendered. Hence, I shifted the table to an iframe and the code to the corresponding page. Now the rest of the page loads fast, and only the iframe still takes three to four more seconds to load. I can handle this even better if I can show the user an image which signifies that the iframe is loading. But I don't really need that right now. And fyi, StringBuilder helped reduce the execution time by a lot!

Sample code behind the iframe page:

//Note that FileContents is a literal control on the page.


  string folderPath;
    protected void Page_Load(object sender, EventArgs e)
    {
        folderPath = @"../Something/SomeFolder/";

        PrintFile();
    }

    private void PrintFile()
    {
        Directory.CreateDirectory(folderPath);

        DirectoryInfo d = new DirectoryInfo(folderPath);
        FileInfo[] Files = d.GetFiles("*.csv");
        foreach (FileInfo file in Files)
        {
            PrintContents(file.Name);
        }

    }

    private void PrintContents(string name)
    {
        string path = folderPath + name;
        FileContents.Text = "";
        StringBuilder htmlOutput = new StringBuilder();
        using (StreamReader readFile = new StreamReader(path))
        {
            string line;
            string[] row;
            line = readFile.ReadLine();
            string[] rowHeaders = line.Split(',');

            htmlOutput.Append("<table border='1'><tr>");
         
            foreach (string s in rowHeaders)
            {
                htmlOutput.Append("<th>" + s + "</th>");
               
            }
            htmlOutput.Append("</tr>");

            while ((line = readFile.ReadLine()) != null)
            {
                row = line.Split(',');
                if (row.Length > 1)
                {
                   
                    htmlOutput.Append("<tr>");
                    foreach (string s in row)
                    {
                        htmlOutput.Append("<td>" + s + "</td>");
                       
                    }
                    
                    htmlOutput.Append("</tr>");
                }
            }
            htmlOutput.Append("</table>");
           
        }

     


        FileContents.Text = htmlOutput.ToString();

    }


RESULT:


Friday, March 18, 2016


Code using Bootstrap to wrap text around a embedded video:







<div class="widget-content">
<h1 class="page-title">Welcome!</h1>
<div class="separator-2"></div>


<div class="row">
<div class="col-md-12">
                               
                                        <div class="pull-right" style="position:relative;padding-bottom:2%; padding-left:2%">

<div class="embed-responsive embed-responsive-16by9">
                                            <iframe width="420" height="315" src="https://www.youtube.com/embed/R-TBYP0xZCw" frameborder="0" allowfullscreen></iframe>

</div>
                                    </div>

                          
Blah blah blah blah.







</div>
</div>

Codility's got it

New website I came across. Good learning material and fun challenges.