CK's Tech Blog
It's Microsoft's World, and I'm just living in it

Nullable Int type in C#

August 18, 2008 06:00 by ckincincy

I was recently doing some development that needed to allow for an int to be null.  However in .NET that isn't allowed by default.

So I started doing some searches and found that there is a way to have nullable types in C#.

There are two ways to do this:

1. Nullable<int> x = new Nullable<int>(125);

2. int? x = 125

Now what I find interesting is how you can find out if this is null.  You can do, again, one of two things.

1. if(x.HasValue).....

2. if(x != null)....

I actually prefer option two in both situations because that just looks normal to me. 

HT: Eric Gunnerson


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Quick Tip - Getting the most out of Visual Studio

August 14, 2008 06:00 by ckincincy

Great page to read to get the most out of Visual Studion.


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Failed to access IIS metabase

August 10, 2008 06:00 by ckincincy

At work we've recently hired a few new developers and they got this error.

Having worked at setting up many development boxes in the past I knew what the issue was, but honestly I need to post about it so I have a quick reference to the fix... and I figured it may help you as well.

The problem?  .NET framework was installed before IIS.  The fix is pretty easy, you need to 'reinstall' the .NET framework.  Thankfully Microsoft makes it fairly easy.

Open up a command line and type the following and you will be good to go.

c:\windows\microsoft.net\framework\v2.0.50727\aspnet_regiis.exe -i

Which will look something like this after the fact:

image

More info here.


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

How To List All Session Variables ASP.NET

August 6, 2008 06: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


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Response.Redirect, Server.Transfer

August 2, 2008 06: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


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Refactoring Code

July 29, 2008 06: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!


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

DateTime.ToString() Patterns

July 17, 2008 06: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.


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Interviews and Recommendations

July 5, 2008 14: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.


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Working with Zip files in .NET

July 2, 2008 06: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.


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Math with Doubles and Decimals in .NET

June 28, 2008 06: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


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5