Article
Top 5 .NET Newbie Q and A
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.
Visit the