Programming Journal C#, Java, SQL and to a lesser extent HTML, CSS, XML, and regex. I made this so other programmers could benefit from my experience.

Monday, December 15, 2008

Using static strings and objects to increase performance and maintainence

I found this global variable article below to help me with using static strings and objects to increase performance and maintainence.

For example, suppose I have an error message, "Error: ", that I might want to change to "There was a problem: ". Instead of search and replace n times, I could just use a static string variable to change the string value.

Sample Global class:

/// <summary>
/// Summary description for Global
/// </summary>
public static class Global
{
public static string CSS_HIDDEN
{
get { return "hidden"; }
}
public static string CSS_EMPTY
{
get { return string.Empty; }
}
public static string CSS_CONTENT
{
get { return "content1"; }
}
public static string ERROR_HEADER_HTML
{
get { return "<red>There was a problem:</red></br>"; }
}
public static string ERROR_HEADER
{
get { return "There was a problem:"; }
}
}
Then one just accesses it with Global.ERROR_HEADER. Notice that there is no declarative instantiation for the static class.

Reference: http://dotnetperls.com/Content/Global-Variables-ASPNET.aspx

Sunday, December 14, 2008

Use securitytrimming to limit role views of Menu and SiteMap

Use securitytrimming to limit role views of Menu and SiteMap.

Modify the Web.config file's configuration tag with:


<system.web>
<siteMap defaultProvider="secureProvider">
<providers>
<add name="secureProvider" type="System.Web.Xml




Provider"

siteMapFile="web.sitemap" securityTrimmingEnabled="true"/>
</providers>
</siteMap>
</system.web>
Then modify Web.sitemap to make sure roles parameter is set to * or admin. Notice how I explicitly set the nodes roles parameter in the links outside the website

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="SiteMap.aspx" title="Site Map" description="Site Map">
<siteMapNode url="Default.aspx" title="Home" description="Home" roles="*">
<siteMapNode url="Login.aspx" title="Login/Logout" description="Login or Logout" />
<siteMapNode url="About.aspx" title="About" description="About" />
</siteMapNode>
<siteMapNode url="~/Tools/Tools.aspx" title="Tools" description="" roles="*">
<siteMapNode url="~/Tools/GrowthAnalysis.aspx" title="Growth Analysis" description="" />
<siteMapNode url="~/Tools/FundamentalAnalysis.aspx" title="Fundamental Analysis" description="" />
<siteMapNode url="~/Tools/PriceToSalesAnalysis.aspx" title="Price To Sales Analysis" description="" />
<siteMapNode url="~/Tools/TechnicalAnalysis.aspx" title="Technical Analysis" description="" />
</siteMapNode>
<siteMapNode url="~/MyLists/MyLists.aspx" title="My Lists" description="" roles="*">
<siteMapNode url="~/MyLists/Management.aspx" title="Management" description="" />
<siteMapNode url="~/MyLists/Tracker.aspx" title="Tracker" description="" />
</siteMapNode>
<siteMapNode url="~/MarketWatch/MarketWatch.aspx" title="Market Watch" description="" roles="*">
<siteMapNode url="~/MarketWatch/MarketSummary.aspx" title="Market Summary" description="" />
<siteMapNode url="~/MarketWatch/SectorSummary.aspx" title="Sector Summary" description="" />
<siteMapNode url="~/MarketWatch/IntermarketSummary.aspx" title="Inter-market Summary" description="" />
<siteMapNode url="~/MarketWatch/WorldMarketSummary.aspx" title="World Market Summary" description="" />
</siteMapNode>
<siteMapNode url="~/Media.aspx" title="Media" description="" roles="*">
<siteMapNode url="http://www.bloomberg.com/news/av/" title="Bloomberg Audio/Video" description="" roles="*" />
<siteMapNode url="http://www.thestreet.com/_tscnav/audio/taskaudio/index.html" title="Real Story With Aaron Task" description="" roles="*" />
<siteMapNode url="http://www.thestreet.com/_tscnav/video/index.html" title="TheStreet.com TV" description="" roles="*" />
<siteMapNode url="http://online.wsj.com/public/us" title="Wall Street Journal" description="" roles="*" />
<siteMapNode url="http://www.nytimes.com/pages/business/index.html" title="New York Times Business" description="" roles="*" />
<siteMapNode url="http://markets.usatoday.com/custom/usatoday-com/html-markets.asp" title="USA Today Markets" description="" roles="*" />
<siteMapNode url="http://www.briefing.com/Investor/Public/MarketSnapshot/HeadlineHits.htm" title="Briefing.com Headline Hits" description="" roles="*" />
<siteMapNode url="http://www.pbs.org/nbr/" title="PBS Nightly Business Report" description="" roles="*" />
<siteMapNode url="http://www.redoption.com/shadow_video.php" title="RedOption Shadow Trader Video" description="Weekend Update" roles="*" />
</siteMapNode>
<siteMapNode url="~/Commentary.aspx" title="Commentary" description="" roles="*">
<siteMapNode url="http://stockmonger.blogspot.com/" title="Stock Monger" description="" roles="*" />
<siteMapNode url="http://find.thestreet.com/cgi-bin/texis/find/results.html?nh=20&amp;t=Cramer" title="Jim Cramer" description="" roles="*" />
<siteMapNode url="http://www.rightsideadvisors.com/advisors/default.aspx?" title="Richard Suttmeier" description="" roles="*" />
<siteMapNode url="http://articles.moneycentral.msn.com/Commentary/Experts/Jubak/Jim_Jubak.aspx" title="Jim Jubak" description="" roles="*" />
<siteMapNode url="http://bigpicture.typepad.com/" title="The Big Picture With Barry Ritholtz" description="" roles="*" />
</siteMapNode>
<siteMapNode url="~/Admin/Admin.aspx" title="Admin" description="" roles="Admin">
<siteMapNode url="~/Admin/UpdateAllStocks.aspx" title="Update All Stocks" description="" roles="Admin" />
<siteMapNode url="~/Admin/StockScaffold.aspx" title="Stock Scaffold" description="" roles="Admin" />
<siteMapNode url="~/Admin/ManageRoles.aspx" title="Manager User Roles" description="" roles="Admin" />
<siteMapNode url="~/Admin/ManageLists.aspx" title="Manage Lists" description="" roles="Admin" />
</siteMapNode>
</siteMapNode>
</siteMap>

Friday, July 25, 2008

Beginning Subversion (SVN) for .NET

I've had enough of zipping my project for backup before editing it. I mean it works fine for personal development, but for team projects it is time for a versioning system. That means either using CVS (Control Versioning System) or SVN (Subversion), since VSS (Visual Source Safe) would be too expensive.

First you need the download and install subversion: http://subversion.tigris.org/

Next you need to create the repository. Suppose you've created your svn directory. Fire up the command prompt and navigate there. create the "MyProject" repository:
svnadmin create MyProject

Next, recognize the recommended standard structures:
svn/MyProject
svn/MyProject/trunk
svn/MyProject/branches
svn/MyProject/tags

Next, place the trunk directory in hte MyProject directory and the .NET actual Website directory in the trunk:
svn/MyProject/trunk/MyWebsite

Next, checkout the project:
svn checkout "file:///c:/documents and settings/user/svn/MyProject"

Next, add the trunk, branches, and tags folders to the MyProject repository:
cd MyProject
svn mkdir trunk
svn mkdir branches
svn update
svn add tags

Next, commit the updates to in effect check-in the project:
svn commit -m "initial project setup"

References:
http://www.germane-software.com/~ser/R_n_R/subversion.html
http://www.onlamp.com/pub/a/bsd/2005/08/11/FreeBSD_Basics.html?page=2
http://svnbook.red-bean.com/

Saturday, July 19, 2008

Opposite Conventions List

Here is a list of opposing actions to help maintain consistent conventions:
add/remove
increment/decrement
open/close
begin/end
insert/delete
show/hide
create/destroy
lock/unlock
source/target
first/last
min/max
start/stop
get/put
next/previous
up/down
get/set
old/new
Reference: Code Complete

Saturday, July 12, 2008

Wiki Trials and Errors

I read about Wikis in Code Complete and decided to try some out for software design. This was my first contact beyond the famous Wikipedia. Wikis are basically editable html pages for documenting facts, design, and collaborating amongst users. Here are the results:

I tried FlexWiki and read their basic documentation to add namespaces and edit pages. I set it up on my localhost and continuously ran into 404 errors after updating a page. The few fixes I read about looked incomplete, so I scrapped the FlexWiki.

Next I installed DokuWiki from a PHP application on my Kensoft server. I'll update the review when I'm done setting up a sample. But in the meantime, I'd recommend avoiding FlexWiki.

Reference: http://www.splitbrain.org/go/dokuwiki

Thursday, July 3, 2008

Using a GridView with Edit and Delete that use stored procedures

Here is how to use a GridView with Edit and Delete that use stored procedures for update and delete. First, the aspx:

<asp:GridView ID="gvEmail" runat="server" AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
AllowPaging="True" OnRowDeleting="gvEmail_RowDeleting" PageSize="99" OnRowUpdating="gvEmail_OnRowUpdating">
<Columns>
<asp:BoundField DataField="email" HeaderText="email" SortExpression="email" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [email] FROM [vw_Email]"
DeleteCommand="sproc_DeleteEmail" DeleteCommandType="StoredProcedure"
UpdateCommand="sproc_UpdateEmail" UpdateCommandType="StoredProcedure"
>
<DeleteParameters><asp:Parameter Name="email" Type="String" /></DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="email" Type="String" />
<asp:Parameter Name="newEmail" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
Now the code behind.

protected void gvEmail_RowDeleting(object sender, GridViewDeleteEventArgs e) //DELETE
{
SqlConnection conn = new SqlConnection(SqlDataSource1.ConnectionString);
System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand("sproc_DeleteEmail", conn);
command.CommandType = System.Data.CommandType.StoredProcedure;
conn.Open();
string email = string.Empty;
DataControlFieldCell cell = gvEmail.Rows[e.RowIndex].Cells[1] as DataControlFieldCell;
gvEmail.Columns[0].ExtractValuesFromCell(
e.Keys,
cell,
DataControlRowState.Normal,
true);
email = e.Keys[0].ToString();
if (!string.IsNullOrEmpty(email))
{
command.Parameters.AddWithValue("@email", email);
command.ExecuteNonQuery();
}
command.Parameters.Clear();
conn.Close();
}
protected void gvEmail_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection conn = new SqlConnection(SqlDataSource1.ConnectionString);
System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand("sproc_UpdateEmail", conn);
command.CommandType = System.Data.CommandType.StoredProcedure;
conn.Open();
string newEmail = e.NewValues[0] as string;
string email = e.OldValues[0] as string;
if (!string.IsNullOrEmpty(email))
{
command.Parameters.AddWithValue("@email", email);
command.Parameters.AddWithValue("@newEmail", newEmail);
command.ExecuteNonQuery();
}
command.Parameters.Clear();
conn.Close();
}
Note: Exception handling left off for brevity.
Reference: http://www.developerfusion.co.uk/show/91/7/

Tuesday, July 1, 2008

Using XML and XPath for a GridView

This demonstrates how to use XML and XPath with a GridView to display links:

<asp:GridView ID="gvLinks" runat="server" DataSourceID="XmlDataSource1" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Link">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" Target="_blank" NavigateUrl='<%#XPath("href") %>'>
<%#XPath("title")%>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label ID="lblrOwner" runat="server" Text=&lt;%#XPath("description")%&gt;></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/App_Data/Links.xml"
XPath="links/link"></asp:XmlDataSource>
Here is the XML file:

<?xml version="1.0" encoding="utf-8" ?>
<links>
<link>
<title>Yahoo</title>
<href>http://www.yahoo.com/</href>
<owner>Yang</owner>
<description>Yahoo web portal</description>
</link>
<link>
<title>MSN</title>
<href>http://www.msn.com/</href>
<owner>Balmer</owner>
<description>Microsoft web portal</description>
</link>
<link>
<title>Delicious</title>
<href>http://del.icio.us/</href>
<owner></owner>
<description>Internet bookmark manager</description>
</link>
<link>
<title>YouTube</title>
<href>http://youtube.com/</href>
<owner></owner>
<description>Internet videos</description>
</link>
<link>
<title>SlashDot</title>
<href>http://slashdot.org/</href>
<owner></owner>
<description>News that matters</description>
</link>

</links>
Reference: http://bytes.com/forum/thread528704.html