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.

Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Saturday, August 15, 2009

What are VB Me and MyBase Equivalents in C#?

What are VB Me and MyBase Equivalents in C#?














VB

C#

Me

this

MyBase

base

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>

Monday, June 23, 2008

Opening, Reading, and Writing a file

Here is an example of Opening/Reading a file in btnOpen_Click and Writing in btnSave_Click:

protected void btnOpen_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtFileName.Text)) txtFileName.Text="c:/temp/temp.txt";
StreamReader sr = null;
try
{
sr = new StreamReader(txtFileName.Text);
txtFileContent.Text = sr.ReadToEnd();
sr.Close();
displayMessage("Successfully opened file");
}
catch (Exception ex)
{
displayMessage(ex.Message);
}
finally
{
sr.Close();
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtFileName.Text)) txtFileName.Text = "c:/temp/temp.txt";
StreamWriter sw = null;
try{
sw = new StreamWriter(txtFileName.Text);
string[] ar;
ar = txtFileContent.Text.Split(Environment.NewLine.ToCharArray());
foreach (string sIn in ar)
{
if(!string.IsNullOrEmpty(sIn))
sw.Write(sIn.Trim()+Environment.NewLine);
}
}catch (Exception ex)
{
displayMessageLine(ex.Message);
}
finally{
sw.Close();
}
}

Saturday, June 21, 2008

Using a nullable boolean.

It is possible to assign a nullable boolean in C#. The trick is to use ? in the declaration. Once declared, one can use the HasValue() method. An example follows:

protected void Page_Load(object sender, EventArgs e)
{
bool? isUser = null;
lblStatus.Text = isUser.HasValue.ToString(); //displays false
// an int example: Nullable<int> i = null;
}
protected void btnCheckStatus_Click(object sender, EventArgs e)
{
bool? isUser = false;
lblStatus.Text = isUser.HasValue.ToString(); //displays true
}

Friday, April 25, 2008

Creating a GUID from scatch

Sometimes you might want a psuedo-random string value. This can be done with:
System.Guid.NewGuid().ToString();

Thursday, December 13, 2007

Handy C# tricks

Here are some handy C# tricks to increase productivity.
First, the code:

        //

        // Summary:

        //     Gets or sets a value that indicates whether the System.Web.UI.WebControls.MenuItem

        //     object is enabled, allowing the item to display a pop-out image and any child

        //     menu items.

        //

        // Returns:

        //     true if the menu item is enabled; otherwise, false.

        [Browsable(true)]

        [DefaultValue(true)]

        public bool Enabled { get; set; }

  • The [Browsable(true)] allows the properties window to display the property or event.
  • Default value does as you would expect if the variable is not directly set
  • Short cut get and sets! Simple.

Friday, October 5, 2007

Help user redirect to secure target page after login and avoid extra navigation

Here's a solution for when the user needs to log in before accessing a secure page. This allows the user to avoid going through the menu system to go to the original page desired. I avoid some kind of error by using the false parameter in the redirect. The querystring of note is ReturnUrl.


protected void Page_Load(object sender, EventArgs e)
{
if (User.Identity.IsAuthenticated == false)
{
Response.Redirect("Login.aspx?ReturnUrl=DesiredPage.aspx", false);
}
else
{
MembershipUser user = Membership.GetUser().ProviderUserKey.ToString();
}
}

RedirectFromLoginPage Method

Saturday, September 22, 2007

Proper ViewState use and the coalescing operator

Here is a quick lesson the ViewState object for viewing an object's state. I found the use of the coalescing operator helpful for future use instead of wordy if then else statments.
  • public int CompanyID

    {

    get { return (int)(ViewState["CompanyID"] ?? 0); }

    set { ViewState["CompanyID"] = value; }

    }
The coalescing operator is a brother of if-then-else and
the more succinct: if (isX ? doA() : elseDoB).
I would describe coalescing as shorthand for: if (isX!=null) then getX() else getB().