Top 5 .NET Newbie Q and A

Tweet

Interested in .NET? Don’t miss SitePoint’s .NET Feature Guide — it’s an excellent resource!

New to .NET? Still deciding whether to take the plunge? You’re not alone. Here, SitePoint experts answer the 5 most frequently asked questions about this technology…

1. What are the Advantages of the .NET Platform?

"It’s faster, easier, sleeker than anything else I’ve ever used!"

This is the cry of many a .NET advocate — but what does it actually mean in real terms?

Languages

You can code .NET in a multitude of languages, which is nice. If you already have a language syntax you like, you don’t have to change too much in order to accommodate .NET.

Also, .NET includes C#. C# was designed by Anders Hejlsberg, the man behind Turbo Pascal and Borland Delphi. It’s a combo of C, C++ and Java, and is arguably one of the best programming languages around.

Speed

In every benchmark I’ve seen yet, .NET has been very fast in comparison to other solutions, such as Java. I’m yet to see a comparison between .NET and PHP, but it’s most likely that .NET will win (because it’s compiled and has extremely powerful caching capabilities) unless you use some form of third-party acceleration software for PHP.

The Class Library

But the greatness of .NET lies in the superb class library. Saying "the class library is great" simply does not communicate how good it is:

ASP.NET

ASP.NET is part of the class library, and it a mighty powerful thing. It will save you lots of time and offers extremely elegant separation of code and content.

Check this out.

WebForm1.aspx: 

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"  
Inherits="GuitarSite.GuitarSelectionPage" %>
<html>
<body>
<head>
</head>
<form runat=server>
 <select id="GuitarSelectBox" runat="server" />  
</form>
</body>
</html>

WebForm1.aspx.cs:

using System; 
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace GuitarSite
{
 public class GuitarSelectionPage : System.Web.UI.Page
 {
   Protected HtmlSelect GuitarSelectBox;
   void Page_Load(Object sender, EventArgs e)  
   {  
     if (!Page.IsPostBack)
     {
       String[] guitarMakers = {"Gibson", "Fender",  
"Paul Reed Smith", "Hamer"};
       GuitarSelectBox.DataSource = guitarMakers;
       GuitarSelectBox.DataBind();
     }
   }
 }  
}

And that will produce something like this:

<select name="GuitarSelectBox" id="GuitarSelectBox"> 
 <option value="Gibson">Gibson</option>
 <option value="Fender">Fender</option>
 <option value="Paul Reed Smith">Paul Reed Smith</option>
 <option value="Hamer">Hamer</option>
</select>

I’ll explain the code. The important lines are these:

String[] guitarMakers = {"Gibson", "Fender", "Paul Reed  
Smith", "Hamer"};
GuitarSelectBox.DataSource = guitarMakers;
GuitarSelectBox.DataBind();

The first line just creates an array with guitars. The second line tells the Select box to pull its entries from the array. The third line makes it happen.

Cool, huh? What’s even cooler is that if you’d like the Select box to get its results from the database instead, you’d only have to do this:

String myConnectionString = "server=(local) 
MyMSSQLServer;database=myDataBase;User ID=Admin"
SqlConnection myConnection =  
new SqlConnection(myConnectionString);
SqlDataAdapter myCommand =  
new SqlDataAdapter("SELECT * FROM Guitars", myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "Guitars");

GuitarSelectBox.DataSource = ds.Tables["Guitars"].DefaultView;
GuitarSelectBox.DataBind();

Notice that you do not have to change the file that contains the HTML at all. Another nifty thing with this approach is that, once you’ve used DataBind, the Data stays bound, so to speak. So, for instance, if the user submits the form incorrectly, and the form reloads (with red text and stars next to the incorrect form item), you don’t have to re-query the database or set the datasource again. The "if (PageIsPostBack)" portion of the code ensures that the data isn’t reloaded unnecessarily.

This is because ASP.NET maintains state of all the Web controls (such as form items) between page loads. This also means that ASP.NET will automatically remember what the user entered into the form before e made the error – you don’t have to worry about that logic. ASP.NET is full of stuff like this, which is what saves you time.

ADO.NET

ADO.NET is the data layer of .NET, and it’s very good. ADO.NET is (unlike its predecessor technology, ADO) built around a disconnected data architecture. Database connections are "expensive" performance-wise, so ADO.NET only uses data connections briefly. It connects to the database to get data and update data. When it’s not completing either of those tasks, the connection is freed up for other pages to use.

Most Web applications spend a large portion of their time simply reading through data and displaying it, and ADO.NET provides a disconnected subset of the data (called a DataSet) for the application while reading and displaying. ADO.NET then updates the database only when the DataSet is altered.

Given all the opening and closing of database connections, this approach has scalability issues, of course. ADO.NET remedies this by pooling connections, i.e. while it looks to your application as if you’re creating and destroying connections, you’re actually borrowing and returning connections from a pool that ADO.NET manages for you.

Also, ADO.NET offers extremely powerful database abstraction. Remember the earlier example in the ASP.NET section, with the database and the select box?

String myConnectionString = "server=(local)  
MyMSSQLServer;database=myDataBase;User ID=Admin"  
SqlConnection myConnection =  
new SqlConnection(myConnectionString);  
SqlDataAdapter myCommand =  
new SqlDataAdapter("SELECT * FROM Guitars", myConnection);  
 
DataSet ds = new DataSet();  
myCommand.Fill(ds, "Guitars");  
 
GuitarSelectBox.DataSource = ds.Tables["Guitars"].DefaultView;  
DataBind();

Now, think of the DataSet as a bucket. In this bucket, you have cached versions of all the DB tables your application needs. The SqlDataAdapter can be imagined as a bottle, in which you mix your DB Connection string and your SQL SELECT command. The contents of the SqlDataAdapter-bottle are then poured into the DataSet-bucket, and your application has access to all the tables.

Now, why are the DataSet and DataAdapter separate? Why can’t you mix the SQL Connection string and SELECT command directly into the DataSet-bucket? Because you can use several different types of DataAdapters, such as XML DataAdapters, Oracle DataAdapters etc., so that one DataSet can be comprised of several different data sources. So you can essentially make this one DataSet the only object your application needs for data access, even though you might use 4 different RDBMS and 3 XML data sources. Pretty cool, huh?

Caching

.NET automatically caches classes when they are compiled, so that they can be delivered with extremely speed when they are called. But the Framework also offers some other cool types of caching, for instance, output caching. Here, certain parts of dynamically generated Web pages can be stored on the hard drive in static form, so that they can be delivered directly instead of being generated every time. This is so simple to implement that my cat could do it:

<%@ Page language="C#">  
<%@ OutputCache Duration="120" VaryByParam="*" %>  
 
<html>  
<!-- rest of page goes here -->

This caches the page output for 120 seconds after originally running the page, and saves a separate version of the page for every unique combination of query string values of form variables. You can also use the VaryByParam attribute to let the page to only vary the cache if certain variables defined by you changes. — MJohansson

.NET also provides cross-platform compatibility.

Cross-platform on Windows only?

It sounds like a contradiction in terms, but I was involved in Visual Basic client/server app a couple of years. We had the server set up and the development PC working well. As we went around the building installing the new app on users’ PCs, we encountered problems left and right, most of which were unique to the individual PCs.

Similar to Java, the .NET platform runs as a layer between the basic operating system and your program. Your program isn’t running on Windows 98 or 2000 or XP, it’s running on a single .NET platform — this is what makes the multiple languages possible.

In a perfect world, Microsoft would open this up to other non-MS operating systems. Think of it: you could write a program in any language to work with other programs written in any language to run on any computer! – Westmich

2. Well, if it works with so many languages, which is the best to learn?

Language choice is a matter of preference. Here’s an excellent article titled "VB .NET vs. C#" .

If you feel your PHP skills are rather strong, you would likely want to start with C#. It is a lot like C++, but not as complex. Also, keep in mind that C# is almost identical to Java. You’ll recognize a lot of the Syntax as well, since it is C-based. — Jeremy

If you’re coming from a basis of experience in Visual Basic, VBScript, or VBA (such as creating macros in Office), you might want to try VB.NET. — Westmich

Or, if you’re thinking of your employability in the long run, check out the job scene in your area. Get an idea of what employers are seeking locally and further afield on job sites, and, if you can, join a .NET user group and ask around. — JeremyL

Don’t be afraid to contact recruitment agencies and ask them what skills are most commonly required by employers, and ask more experienced programmers where they see the market heading.

3. OK. So I want to get into .NET. What’s the best way to learn it?

Great resources include:

To get started learning, check out:

  • The .NET Framework
  • MSDE – The free version of Microsoft SQL server. It works exactly like the real thing, except that you can’t use it in a production environment.
  • Download the .NET SDK — in a recent poll, 84.62% of SitePoint’s .NET developers said they couldn’t live without it!
  • Don’t forget Kevin Yank’s Getting Started with ASP.NET, right here in SitePoint’s ASP and .NET category.

If you’re into print, take a look at these books:

4. Is There an ASP.NET Editor I can use?

There is! Microsoft have released their own free WYSIWYG Editor, called the Web Matrix Project, which you can download here.

If you’d like a little more control over your code’s formatting, you might be able to reconfigure another editor — mtffafl reconfigured his copy of Editpad Lite to auto-indent on .aspx and .aspx.cs files.

Macromedia’s Dreamweaver MX does an okay job of recognizing and highlighting ASP.NET code, while providing a live preview, too.

5. Where can I get .NET hosting?

The first stop should be your existing Web host — call or email them and ask them whether they support .NET.

If you don’t have a host, or you’re shopping around, www.asp.net has an extensive list of .net hosts on their Resources page.

More ASP hosts become .NET-enabled all the time, including www.adehost.com, personally recommended by Jeremy

You can also get free .NET hosting, from companies such as http://www.dotnetplayground.com and http://www.brinkster.com.

This article is part of SitePoint’s .NET Feature Guide — an excellent resource for aspiring and experienced .NET developers. Don’t miss it!

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

No Reader comments