Formating Code In Eclipse

To format your code, use short cut CTRL + Shift + F

Advertisements

Filtering Lists In .NET 2.0

In 3.x+ you can use LINQ and Lambda’s. In 2.0 a nice way to filter lists is by using the FindAll method passing in a search predicate.

List.FindAll Method At MSDN

example:


...
..
.
  return list.FindAll(FilterInsiderFilings);

}

private static bool FilterInsiderFilings(InvestorFilingData investorFilingData)
        {
            // search predicate to filter out insider filings (forms 3, 4, 5)
            if (investorFilingData.Filing == "3" | investorFilingData.Filing == "4" | investorFilingData.Filing == "5")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

Most optimal way to parse QueryString within a string in C#

The following will return: 5076639 which is the value of the ipage param

using System.Collections.Specialized;

        // URL to parse
        string xmlHTMLUrl = "http://www.google.com/filing.xml?repo=tenk&ipage=5076639&attach=ON";



        // Ignore the host/path.  Just get the params in the URL

        // Create a URI object from the URL string.  
        Uri uriXML = new Uri(xmlHTMLUrl);

        
        // Extract just the QueryString portion of the URL
        string xmlHTMLUrlParms = uriXML.Query;
        
        NameValueCollection nameValueTable = new NameValueCollection();
        nameValueTable = HttpUtility.ParseQueryString(xmlHTMLUrlParms);

        // Return the ipage parameter value
        string iPageID = nameValueTable["ipage"];



Rendering XML formated as HTML using XSLT

Source XML with HTML formatted text inside

<Wizard.com_filing version="2.1">
−
<FILING_INFO>
<NAME>DEREK REYNOLDS CO</NAME>
<FILER>DEREK REYNOLDS CO</FILER>
<DATE_FILED>20070731</DATE_FILED>
<FORM>15-12G</FORM>
<SIZE>17.1 KB</SIZE>
<PAGES>1</PAGES>
</FILING_INFO>
−
<FILING view="Entire">
−
<DOCUMENT type="HTML" number="1">
<html>
  
  <head>
    <title>tncform15.htm</title>
</head>
  <body bgcolor="#ffffff"><br>
    <div style="DISPLAY: block; MARGIN-LEFT: 0pt; TEXT-INDENT: 0pt; MARGIN-RIGHT: 0pt" align="center"><font style="DISPLAY: inline; FONT-SIZE: 12pt; FONT-FAMILY: Times New Roman;"><strong>UNITED
      STATES</strong></font></div>
    <div style="DISPLAY: block; MARGIN-LEFT: 0pt; TEXT-INDENT: 0pt; MARGIN-RIGHT: 0pt" align="center"><font style="DISPLAY: inline; FONT-SIZE: 12pt; FONT-FAMILY: Times New Roman;"><strong>SECURITIES
      AND EXCHANGE COMMISSION</strong></font></div>
    <div style="DISPLAY: block; MARGIN-LEFT: 0pt; TEXT-INDENT: 0pt; MARGIN-RIGHT: 0pt" align="center"><font style="DISPLAY: inline; FONT-SIZE: 12pt; FONT-FAMILY: Times New Roman;"><strong>WASHINGTON,
      D.C. 20549</strong></font></div>
    <div><br></div>
    <div style="DISPLAY: block; MARGIN-LEFT: 0pt; TEXT-INDENT: 0pt; MARGIN-RIGHT: 0pt" align="justify"> </div>
    <div style="DISPLAY: block; MARGIN-LEFT: 0pt; TEXT-INDENT: 0pt; MARGIN-RIGHT: 0pt" align="center"><font style="DISPLAY: inline; FONT-SIZE: 12pt; FONT-FAMILY: Times New Roman;"><strong>FORM
      15</strong></font></div>
    <div><br></div>
    <div style="DISPLAY: block; MARGIN-LEFT: 0pt; TEXT-INDENT: 0pt; MARGIN-RIGHT: 0pt" align="justify"><font style="DISPLAY: inline; FONT-SIZE: 12pt; FONT-FAMILY: Times New Roman;">CERTIFICATION
      AND NOTICE OF TERMINATION OF REGISTRATION UNDER SECTION 12(g) OF THE SECURITIES
      EXCHANGE ACT OF 1934 OR SUSPENSION OF DUTY TO FILE REPORTS UNDER SECTIONS 13
      AND 
      15(d) OF THE SECURITIES EXCHANGE ACT OF 1934.</font></div>
...
..
.

XSLT filtering out only the DOCUMENT portion containing the HTML (HTMLView.xslt)
Note: disable-output-escaping=”yes” is required, otherwise the HTML markup will not be rendered in browser. If you exclude this the output will just be the HTML source.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <xsl:value-of select="/Wizard.com_filing/FILING/DOCUMENT"  disable-output-escaping="yes"/>
  </xsl:template>
</xsl:stylesheet>

ASP.NET code to transform XML using XSLT



<script runat="server">
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        string xmlUrl = "http://ccbn.10kwizard.com/xml/filing.xml?repo=tenk&ipage=5076639&attach=ON&sXBRL=1";
       

        xmlOutput.DocumentContent = RetrieveXML(xmlUrl);

        xmlOutput.TransformSource = Server.MapPath("HTMLView.xslt");


       

    }

   

    private static string RetrieveXML(string url)
    {
        // Create the WebRequest for the URL we are using
        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
        req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)";

        // Get the stream from the returned web response
        StreamReader stream = new StreamReader(req.GetResponse().GetResponseStream());

        //experiment try reading entire thing at once
        string html = stream.ReadToEnd();

        // Finished with the stream so close it now
        stream.Close();

        return html;

    }

    
    


</script>

<html>
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Xml ID="xmlOutput" runat="server" Visible="true"></asp:Xml>      
    </div>
    </form>
</body>
</html>

Object DataSource With Paramaters

Prefix the Business Logic Layer method that accepts parameters with the following. otherwise the object DataSource will not identify this method that accepts parameters:

 [DataObjectMethod(DataObjectMethodType.Select)]

Example:

    [DataObjectMethod(DataObjectMethodType.Select)]
    public static DataTable GetInvestorFilingDataTable(InvestorDataFeedType feedType)
    {

        List<InvestorFilingData> entireList = new List<InvestorFilingData>();
    
        entireList = DAL.GetInvestorFilingData(feedType);

        DataTable datatable = ListToDataTable(entireList);

        return datatable;

    }        

Then to pass params to this method within your datasource specify SelectParameter’s:

<asp:ObjectDataSource ID="DataSourceFilingsDT" runat="server" SelectMethod="GetInvestorFilingDataTable"
    TypeName="BLL" OnSelected="DataSourceFilingsDT_Selected" 
    OldValuesParameterFormatString="original_{0}" 
    onselecting="DataSourceFilingsDT_Selecting">
    <SelectParameters>
        <asp:Parameter Name="feedType" Type="Object" />
    </SelectParameters>
</asp:ObjectDataSource>

To pass values to the method within the DataSource:

 protected void DataSourceFilingsDT_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        // Change the Parameter of feedType within GetInvestorFilingDataTable
        e.InputParameters["feedType"] = DataToDisplay;
    }

More info here.

Passing Parameters To WebControls

ASPX consuming Control:

<%@ Page Language="C#" MasterPageFile="~/lib/mp/Interior.master" Inherits="AEPcomPage"
    Section="Investors" Title="" %>

<%@ Register Src="~/lib/uc/finance/FilingsGridControl.ascx" TagName="FilingsGridControl" TagPrefix="uc2" %>


<script runat="server">
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        
        FilingsGridControl.DataToDisplay = "APCOData";         
        
    }
    
    
</script>

<asp:Content ID="c2" ContentPlaceHolderID="cphCol2"  runat="Server">
    <uc2:FilingsGridControl
        ID="FilingsGridControl1"
        runat="server"
        />
    
   
</asp:Content>

Control

<%@ Control Language="C#" ClassName="FilingsGridControl" %>
<%@ Import Namespace="System.Web.Mail" %>

<script runat="server">
   
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        lblPagingInformation.Text = DataToDisplay;
    }

   static string sDataToDisplay;

    public static string DataToDisplay
    {
        get { return sDataToDisplay; }
        set { sDataToDisplay = value; }
    }   
 ...
..
.       
    

Also see Passing Parameters From Page (aspx) to WebControl (ascx) Where control exists in another project

Convert List to DataTable – (ListToDataTable function)

using System.Data;
using System.Reflection;

private static DataTable ListToDataTable<T>(List<T> list)
        {
            DataTable dt = new DataTable();

            foreach (PropertyInfo info in typeof(T).GetProperties())
            {
                dt.Columns.Add(new DataColumn(info.Name, info.PropertyType));
            }
            foreach (T t in list)
            {
                DataRow row = dt.NewRow();
                foreach (PropertyInfo info in typeof(T).GetProperties())
                {
                    row[info.Name] = info.GetValue(t, null);
                }
                dt.Rows.Add(row);
            }
            return dt;
        }

Example implementation:

        List<InvestorFilingData> lst = FilingsLogic.GetInvestorFiling();
        DataTable dt = ListToDataTable(lst);

Example use: When you have an object bound to a GridView and want to sort the columns.