OCDProgrammer.com

It's Microsoft's World, and I'm just living in it
View Clarence Klopfstein's profile on LinkedIn

Clarence Klopfstein's Facebook profile

This site is under construction...

Categories

New Comments

Referring Sites


Disclaimer

  • This is MY blog. The views represented here are not in relation to anybody else. Please read my full disclaimer for a more complete disclaimer.

How To List All Session Variables ASP.NET

August 6, 2008 03:00 by ckincincy

Recently I needed to display all Session Variables when a crash would happen.  A crash would happen and I have this website I was working on send me an email with the stack trace and now the session variables.  However I wasn't aware of how to list all session variables, so I went to my friend Google and came up with the following code:

string strSessions;
for(int i=0;i<Session.Count;i++)
{
       strSessions += Session.Keys[i].ToString() + " - " + Session[i].ToString() + Environment.NewLine;
}

Works like a champ!

HT: davidj.org


Response.Redirect, Server.Transfer

August 2, 2008 03:00 by ckincincy

After recently launching a website, I had some built in error reporting in it.  Starting seeing a few 'Thread is being Aborted' errors.  I remembered dealing with this at my previous job and that it was related to the Response.Redirect call.

So hopefully with the help of a few of my readers we can put a list of issues related to the various calls that you can make to redirect a user to another page.

Response.Redirect
Response.Redirect was something I first used in classic ASP.  now due to the nature of that platform I didn't see any issues with it.  But in .NET its a different story.  Microsoft gives us a few versions of this call.

1. Response.Redirect("URL")  - The parameter is just the URL that you want the user sent to.  Pretty strait forward.
2. Response.Redirect("URL", true/false) - The second parameter is what causes the error above.  By default version one of this call sets the second parameter to true.  However what that does is 'abort the thread' so as your page goes down to the next call it will see that the page is being aborted.  By setting this to false, you fix the issue.  However this is not idea because by setting it to true you are getting a bit more overhead on your HTTP traffic because you are telling the server to continue the request. Even though you have moved on.

Server.Transfer
Server.Transfer is a lot like Response.Redirect in that you have two versions to call.

1. Server.Transfer("URL")
2. Server.Transfer("URL", True/False) - The second parameter determines whether or not you can use the objects (like text boxes) on the original page.  If it is set to true, you can use the objects.  If it is set to false, you can't.  Those items expire when the new page loads.

Comparisons
Now from my reading there are a few other distinct differences between the two.

1. Response.Redirect allows you to redirect to pages external to your site. The Server.Transfer HAS to be on your server.
2. Server.Transfer is just like Response.Redirect with a true passed in.  Except it doesn't throw the original error.  This allows you to save server resources as you transfer from page to page.

Well do you have anything to add?  Did I miss anything?

HT: Microsoft
HT: Karl Moore


Refactoring Code

July 29, 2008 03:00 by ckincincy

When I started at my new job I inherited a website that was, to put it mildly, written poorly.  Unfortunately due to the nature of a fast paced business you can't always take the time to make code right.  You have to just pile bad on top of bad because it works.

However recently I took the time to refactor a page.  Before starting it had 2,411 lines of code... after I was done, 649!!!  What used to be a pain to add onto or edit, is now a breeze. 

So when the time is write, refactor.  It makes life a lot better.  Better yet... write it right to begin with!


DateTime.ToString() Patterns

July 17, 2008 03:00 by ckincincy

Many times when programming in .NET you need your DateTime variable to display in various formats:

08/22/2006
Tuesday, 22 August 2006
etc...

This is thankfully made pretty trivial with the ToString() function.  Just pass in the appropriate format string and you will get what you need.

You can find a great list of these here.


Interviews and Recommendations

July 5, 2008 11:48 by ckincincy

It has been an interesting week for me.

Started off with an old coworker IM'ing me and asking if I'd be a reference for a job he was interviewing on.  I certainly said yes, and last word I got is that he got the job.  The email I sent in reply to the email looking for feedback was a glowing recommendation for somebody who is a very skilled developer, and my first mentor in the trade.  Its good to be able to give back to him at times.

Then I was tasked with tech interviewing a candidate for a job at my employer.  First real interview I've had to do.  Was fun. In the end I love programming and talking about programming is pretty fun as well.

Next I was talking to a dad of one of my daughters friends.  He was recently back from a 10 month tour in Iraq.  He is a true, decorated war hero.  But now he has to reintegrate back into the real world.  Part of that is him wanting to get an IT job in the area.  He is interested in a newbie position, but some of his skills are a bit above that as well.  He has secret security clearance.  He'd be good for any company that needs general IT help (server setup, maintenance, deployment, help desk related work).  So if you work for a company that could honor one of our war hero's AND get a new worker at the same time... let me know and we can talk.  I have his resume on hand.

Finally came the most interesting part of the week.  We needed another .NET developer (we had hired the one I interviewed previously) for my employer and I recommended a former co-worker whom I helped train in what was nice and  good about programming.  This is a guy who at one point thought x was a valid variable name.... still can't believe that first code I saw from him... From what I can tell salary arrangements have been agreed on and if the owners of the company approve, me and this fellow will be co-workers once again. 

This final guy is one where I put my reputation on the line for him.  I am very tight lipped about my recommendations.  There are just four guys who would get my unequivocal recommendation.  There are many others who would get a recommendation on their work ethic, ability to learn, etc... but just four who I would recommend for ANY job they were going for.  He is one of them.


Working with Zip files in .NET

July 2, 2008 03:00 by ckincincy

Recently I was working on a project that required me to unzip a file and process its contents.

But I needed to first figure out how to unzip a file.  To the rescue came the SharpZipLib.  This is under the GPL but is specifically released for personal and commercial use.

In my very basic use it worked like a champ.


Math with Doubles and Decimals in .NET

June 28, 2008 03:00 by ckincincy

Just before I went on vacation I had an interesting issue at work.

I was trying to multiple an int by a decimal.  Nothing big, but I kept getting this error:

Operator '*' cannot be applied to operands of type 'decimal'

I was floored, what do you mean I can't multiply a decimal in .NET?  What in the world are you talking about?

So after a brief search I found the fix.

Basically I had to add an M to the decimal.  So instead of:

4 * .95

I needed:

4 * .95M

Honestly still don't understand WHY Microsoft would think this is required, but they do. 

Hope this helps you as it helped me.

HT: Asp.net


List all installed fonts on your machine

June 24, 2008 03:00 by ckincincy

I recently ran across this post and loved it!  I do some image creation stuff and the hardest part of my task is picking a font.  Now I can print out my installed fonts and quickly pick one out.

Now here is the guys code, but slightly edited.  As his had a few syntax errors in it. 

try
{
    StringBuilder sb = new StringBuilder();
 
    foreach (FontFamily family in FontFamily.Families)
    {
    sb.Append("<h2 style='color:blue;font-family:Arial;'>" + family.Name + "</h2>");
    sb.Append("<font face='" + family.Name + "' size='6'>");
    sb.Append("The quick brown fox jumps over the lazy dog. 1234567890");
    sb.Append("</font><hr/>");
    sb.Append(Environment.NewLine);
    }
 
    lblFontList.Text = sb.ToString();
}
catch (Exception ex)
{
    lblErrorMessage.Text = ex.ToString();
}

Telling a client no

March 15, 2008 16:43 by ckincincy

I've helped a local company do their web site over the years.  They have come to me several times with 'great ideas' for their web site.  From 'nice' blinking text to hiding text at the bottom of the page to 'fool Google'. 

When they come to me with these idea's I fill them in on why that is either a bad practice or a downright bad idea. 

And this isn't just this client that I've had these kinds of conversations with.  Most highly respect my technical skills and accept my answer.  Sometimes they listen to my reasons and decide differently.  In the end they are the boss. I've only had one client that seems to constantly override my thoughts, frustrating for sure. 

But this local company came to me with another 'great idea' and I again told them why it was bad.  So they decided to go with somebody else.  Honestly, kind of relieved.  But I wait for the day that they face some of the consequences of not having somebody willing to tell them no.  When Google's spider realizes the hidden text and drops them from their index.

So I guess the bottom line is, feel free to tell a client no.  But realize they are the boss!  And if it is in your power, and them telling you no just frustrates you to no end... drop them as a client.  Having them make you design a bad site or application isn't worth your reputation.


Naming Conventions

January 18, 2008 21:11 by ckincincy

Well if you've been a programmer for any length of time, this better be something you're familiar with.  If it's not... change and follow these rules for the sake of humanity.

I've had the joy over the past few months to work on two projects I inherited from others... the coding standards are killing me.

So lets work this out, lets say you have a text box for a name.  The proper naming convention of that text box is NOT:

NameTxt. 

The proper naming convention of this text box IS:

txtName

You see that.. the type of object goes in FRONT of the variable name. 

There is this neat little trick we have available to us in modern IDE's.  When I know I am looking to edit a Button I start by typing btn... and you know what happens?  I get a pop up with all the buttons on my form.

I really thought this was programming 101... guess not.

So here are a few of my naming conventions, frankly I don't care if you follow this.. just be consistent so the person that follows you can figure it out!

Button: btn
Radio Button: rdo
Label: lbl
Text Box: txt
Drop Down List: ddl

Really is this that hard?


Categories: Programming
Actions: E-mail | Permalink | Comments (1) | Comment RSS