Posts Tagged ‘.NET 2.0’

Getting the ASP.NET Development Web Server to use a root path

Friday, July 10th, 2009

ASP.NET 2.0 comes with a test webserver which can be run by simply pressing F5 in Visual Studio from a website project which is located on your PC.  The only problem is that for reasons best known to Microsoft, it launches with the site configured in a subfolder.  This isn’t always convenient, as you may have paths relative to the site root which prevent this being practical for testing.

In order to get around this problem, you need to take the following steps: -

First of all, configure Visual Studio so that you can launch the test server manually as follows: -

Under the Tools menu, select External Tools.
Add a new entry
Call it something like ASP.NET Development Server
Command is C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\WebDev.WebServer.EXE (you may need to alter the path for your local machine)
Arguments are /port:80 /path:$(ProjectDir) (note that you will need to leave a space on the end of this for it to work properly.  Also, you can change the port number if you wish)

Press OK.  You can now launch the development server from the new entry on your Tools menu.  This will show in your system tray.  You should probably remember to close it when you are done.

The next step is to configure your project to use the server.  Right click on your project and click Property Pages.  If an empty dialogue comes up, press cancel and repeat the process – it should work second time.  Under Start Options, select Use custom server and leave the Base URL blank.  You may wish to change the start action as well.

Once you are done, you can press F5 to start debugging.  Don’t forget that next time you open the project you will need to start the server from the tools menu before you start debugging again, otherwise it won’t work.

Also, bear in mind that if this is a copy of a remote site, things like database connection strings may need changing.  Don’t forget to be careful not to overwrite any settings when you copy back if that is the case!

If you do want to develop on a copy of the site, the Website menu has a useful option to Copy website.

Generics in .NET Part 1 – What are Generics and How to Use Them

Monday, June 29th, 2009
This entry is part of a series, Generics»

One of the most important parts of program design is making sure that your code is as reusable and maintainable as possible.  Most major developments in new programming languages tend to be aimed towards this, as well as organisation of code.  The real problem is finding different ways of creating patterns of reusable code.

One of these ways is object orientation, including polymorphism.  Whilst this is very useful, it does have some limitations.  Sometimes it is useful to be able to create a data type or a method which is almost a template – it contains or operates on or is in other way based around some other type (or multiple types), which you want to pass in when you create it.  If you are a C++ programmer, you may be familiar with this concept as templates.

As an example, in .NET 1.0/1.1, you can create a Collection, but you can’t specify what type the objects in the collection are without creating a special class for each type that you want to include in a Collection.  In .NET 2.0, if you look in the System.Collections.Generic namespace, you will find a number of different collection types that use Generics to allow you to create a collection for a specific type of item.

For example, if you want a list of dates, you can create a List(Of Date).  This will create a list that not only forces you to only put dates into it, but also intellisense will be aware that an item contained within it will be a date and will be able to eg. use the AddDays method.

If you want to use a Generic class that takes multiple type parameters, you just pass them in using the format above Type(Of TemplateTypeName), only you use commas to separate them like Type(Of T1, T2, …).  An example of this would be if you wanted to create a Dictionary(Of Integer, String), which you could use to store a lookup table of integer numbers which each have a corresponding string.

Entries in this series:
  1. Generics in .NET Part 1 - What are Generics and How to Use Them
Powered by Hackadelic Sliding Notes 1.6.5

Windows Forms DateTimePicker ShowCheckBox and not Checked bug in .NET 2.0

Monday, March 9th, 2009

I recently upgraded a project that I had inherited from another programmer to .NET 2.0. Shortly after, I received an email complaining that a date field had stopped saving correctly. I tried it out, and all looked fine in the user interface, but sure enough, the value was not being written to the database.

It took me some time to work out what was going on. The ShowCheckBox property was set to False, but the Checked property also was. For some reason, the DateTimePicker in .NET 2.0 interprets this as meaning that the value that is in the control is to be ignored, and it returns today’s date if you access the Value property.

This was a pretty simple fix – all it took was to set Checked to True, and it all worked fine again. It is an interesting gotcha to look out for though – I haven’t seen it documented anywhere as a breaking change of .NET 2.0

ASP.NET Sessions Part 2 – accessing session state variable data

Wednesday, March 4th, 2009
This entry is part of a series, ASP.NET Sessions»

To add/set a session variable, from within code on a page, you can simply use

Session(“VariableName”) = Value

To reset it, you can set it to Nothing in VB.NET or null in C#

If you aren’t inside a page’s code, you can use the HTTPContext class to get the current context and access the session via that (more coming on HTTPContexts at some point in the future).  You can do this like…

HTTPContext.Current.Session(“VariableName”) = Value

Upgrading Visual Studio 2005 Solutions to 2008

Friday, March 14th, 2008

Last night I upgraded a Visual Studio 2005 solution to 2008.  It contained 1 Windows Forms executable, 1 Class Library and an ASP.NET Web Forms website.  I was pleasantly surprised to find that Visual Studio did the upgrade with no hassle, and even asked me whether I would like to target .NET 3.5 or .NET 2.0.  For the moment, I selected .NET 2.0, as I will be upgrading it to 3.5 later.  I don’t know if it is so easy for all projects, but it was nice to see how easy it was for me.

Structured Exception Handling in .NET Part 1: Try/Catch/Finally

Thursday, March 13th, 2008
This entry is part of a series, Structured Exception Handling in .NET»

A friend has asked me to write some articles on how to handle exceptions in .NET, so here we go… A little bit of background first: -

Exceptions occur when things go wrong or other exceptional circumstances. They are a bit more complicated than the older error trapping systems, but the complexity lets you do a lot with them. Every Exception is an object, and it is either an object of type Exception, or of a type that inherits from Exception.

In this post, I’m just going to deal with catching a general Exception. I’ll cover what you can do with the Exception class itself, multiple Catch clauses, different Exception types, re-throwing and creating your own Exception classes in other posts.

So here is the way it works. When something goes wrong, an Exception object gets created and “thrown”. This means that the stack rewinds back until it finds something that catches the Exception. Basically, this means that anything that gets called inside a Try clause will be handled if an Exception gets thrown. Not only that, but any functions that that calls etc. on and on either forever or until there is another Try clause that will handle it. “Later” Try clauses take precedence over earlier ones.

This is how to use it…

Try
   Some code
Catch ex as Exception
   Some exception handling code
Finally
   Cleanup code that gets called whatever
End Try

Note that the Finally clause is actually optional – it is fine to leave it out. You would normally use it if you wanted to close a database connection, file, stream, network connection etc. or some other form of cleanup code. Also, you can have multiple Catch clauses, but I’ll cover that later.

Nullables in .NET

Wednesday, March 12th, 2008

I don’t know if you’ve ever had a problem with trying to store a null value in a value type in .NET.  If you have, you may be interested to know that there is a generic type wrapper in .NET which can sort the problem out.  It is called Nullable.

You can use it like this (in VB.NET)

Dim n as Nullable(of Integer)

You can then use the HasValue property on it to see if it has a value, and the Value property to retrieve the value.  You can also assign to and from it as normal, including assigning Nothing/null to it.