ASP.NET Introduction - Quick Start Tutorial
Post Page Rank
Hello, this post gives an overview of .NET then describes how to get from nothing to a database enabled web site using .NET in as few steps as possible. The end result will be quite basic, but the aim is write to a series of posts that will compare the minimal get up and go process with other competing technologies such as PHP and Coldfusion.
The intended audience is the mass of PHP developers out there that use LAMP ( Linux, Apache, MySQL and PHP) and want an overview of the .NET thing , anyone who has an interest in .NET but no knowledge might find this post of usefull.
My own journey into web development began with PHP using Apache server software running on a Linux operating system. To me .NET seemed a vague accronym, was it a langauge, a sever or a frame work. However after using .NET for some real projects the mystery has gone and actually it turns out that its quite good with amazing free development tools.
.NET is a framework of code that can can be used to compile desktop or web applications. ASP.NET is the web part of this and is the upgrade to classic asp ( active server pages ).
Classic asp worked much like PHP, you would write an asp script and post it on the server with the extension .asp. The server would be a windows server running IIS ( Internet Information Server), the equivalent of Apache. However, confusingly, you would write your asp pages using the language visual basic, compare that to writing PHP pages using the langauge PHP.
ASP.NET, the latest technology, allows you to write .NET pages using either visual basic or the new language C# ( c-sharp ), whichever you choose your scripts will have the extension .aspx but still run on IIS.
Microsoft have structured the .NET framework so that code written in different languages compiles down to a CLR ( Common Language Runtime ). It is a step above assembly and produces ‘managed code’. When you deploy a .NET site you can supply uncompiled .aspx scripts to the server or compile the site offline and put .dll files ( dynamic link libraries ) onto the server. This allows you to deploy pre-compiled web apps to clients that they would find hard to reverse engineer.
Visual Studio, VB or C#
Personally I prefer to develop using C# because it has a similar syntax to all the other big langauges such as C, C++, Java, PHP and Actionscript. VB on the other hand seems out there on its own, curly braces are generally out in favour of IF THEN and END IF kind of statements giving rise to the freaky WHILE and WEND urghh! Sorry VB lovers.
You pretty much have to develop your site on a windows machine and deploy it to a windows server. There is a framework called mono that lets you serve .NET stuff from Linux but its not something I have had any need to look into. You could also write all your .NET as text files on a mac ( or windows ) but you would miss out on all the features of Visual Studio, Microsofts mega IDE.
The Visual Studio IDE has an abundance of great features, particularily the excellent intellisense. When you are writing code you get autocomplete for methods in the .NET framework but even better you get intellisense for code that you wrote yourself. If you declare a variable you start to get intellisense for it after the next compile. One of my favourite features is refactoring. You can highlight a big block of random code and instantly turn it into a new method, the IDE automatically looks through the code, figures out all the parameters the method will need and creates the new method.
Creating .NET Web Sites with Visual Studio
ASP.NET is based around writing scripts for the web server, mixing code with html and applying CSS for styling. However much like Coldfusion and Flex, .NET works using a tag based syntax to position and place controls onto the page. The tag based markup is generally mixed with html and all C# is kept seperatly at the head of the file or preferably in a seperate code behind file of the same name but with an aspx.cs extension.
For each page Visual Studio provides a design view ( showing a graphical representation of how the page will look), a code view of the html and tag based mark-up and also the code behind showing raw C#.
The general process is drag and arrange some controls into the page using the design view. Then write methods in the C# code behind that will get fired by events generated by the controls.
The whole system is event based. Apparently this was to make ASP.NET easy to understand for desktop developers who were used to dragging windows controls and hooking them up to event handlers.
As a page loads, or is POSTed back to as the result of a click a whole series of events ranging from the web page being loaded to a row in a grid view about to get the data for a particular cell are fired. The event based model takes a bit of getting used to. At times it seems like the clearest simplest methodology there could be, at other times it can leave you frustrated and stuck.
The best part about developing web sites with .NET is that you can test them locally with the total confidence that a remote server will behave in exactly the same way as your PC. When you press control-F5 in Visual Studio it fires up an instance of a .NET server and pops open your website in the browser of your choice. Super convenient. Even better, if you run MS_SQL on your development machine you get full working DB functionality also.
.NET Quick Start
1. You have a windows PC
2. You download and install Visual Web Developer Express 2008.
3.You download and install MS_SQL Express 2008.
4.You download and install SQL Server Management Studio Express.
5.You Create a new DB table with some test data.
6.You open Visual Studio and choose create new web site.
7.You Drag some controls onto a page.
8.You databind some control to a DB table.
9. You test your page by pressing cntrl-F5.
Steps 2,3,4 Download the IDE and DB.
Yes the Express Editions are free! The main limitation to Visual Studio Express 2008 is that you can’t extend it with third party controls. You can only use the in built ones that come with VS, fortunately there are heaps and you can code anything you like anyway. The express editions come as different applications such as Visual C++ Express, Visual C# Express and Visual Web Developer Express. The full Visual Studio has all of these wrapped into one app but thats hardly a problem.
The main limitation to SQL Express and Management Studio is.. mmh, well im not sure but probably its limited to 4G in size or cant be used in a cluster or something like that.
To get to the downloads visit
Choose to download ‘Visual Web Developer 2008 Express Edition’. Next download ‘SQL Server 2008 Express’ and also get SQL management Studio Express, which might be harder to find but there is a link to it at the bottom of the SQL Server Express download page.
When you install SQL Server Express you will have to answer some set up questions. The main ones are ‘allow user instances’ and allow mixed mode authentication’. Allow user instances lets each user on the PC have their own instance of each DB. One user could fill their DB with stuff while the other user sees an empty DB. Not a big concern for me either way. WIndows authentication will log a user into the DB if they are logged into windows whilst mixed mode lets a user log in by supplying a username and password. In the real world DB servers generally use mixed mode and expext a username and password, so I like to choose mixed mode.
Step 5 Create Test Data
Start up SQL Server Management Studio Express. This application lets you control a SQL Server in the same way PHPMyAdmin lets you control a MySQL DB, the main difference is that it is a desktop app. You can connect to any local or remote SQL Server with Management Studio Express as long as you have permissions and a username and password.
To set up Management Studio Express to connect to your local SQL Server Express instance you can use Windows Authentication, this logs you in as the user that is logged into windows. When the app opens you will likely be presented with a connect dialogue where you will have to locate your local SQL Server Express and connect to it. If it is not already shown choose browse for new from the drop down. The SQL Express Server is usually named YOURCOMPUTERNAME\SQLEXPRESS. Click to connect with windows authentication.
Once connected the side panel will show a view of the whole Server. Right Click on the Databases folder and select New Database. In the panel that opens fill in DB name as ‘TestDataBase’ and click OK. There is a short delay and then the ‘TestDataBase’ appears in the DataBases folder.
Expand the ‘TestDataBase’, right click the ‘Tables’ folder and choose add new table. A blank database table design view opens. For the first Column Name enter Name and choose varchar(50) for the type. Choose Age for the second column and choose type Int.
When you have completed the table design right click the tab at the top and choose save as. You will be prompted for a name. Call the table People.
Right click on the newly created People table and choose Open Table. You will see the empty rows in the table. You can start to enter some test data into each row by clicking and entering data. Once you have 4 or 5 rows filled in then that is the test data complete.
Step 6 Open Visual Studio
And create a new web site. Thats File->New Web Site. A pop-up appears. Select new ASP.NET web site and Language C#. Choose location File System and either choose a location or go with the default. click OK.
A new folder is created at the specified location containing all of the files required for your web site. A start page is created for your site called ‘Default.aspx’ and opens in the IDE. Microsoft like to start things with Capital letters.
Step 7 Drag some controls onto the page
Switch to the design view for Default.aspx by clicking the ‘design’ button near the bottom of the screen. Open the toolbox, if it is not open already, by choosing View->toolbox. Browse down to the data tab then locate the GridView Control. Drag it onto the screen.
Step 8 DataBind the Control to the DB table
There is a small tab to the top right of the GridView control. If you click it the GridView Tasks panel opens to the right of the control. Open the ‘Choose Data Source’ drop down and click on ‘new data source..’. A Data Source Configuration Wizard opens asking ‘where will the application get data from’ showing a list of possible data sources. Choose Database and click OK for the default name SqlDataSource1.
The next step asks ‘Which data connection should your application use to connect to the database?’. Click new connection. In the Add Connection dialogue select your SQL Server Express from the server drop down list. For ‘Select or enter a database name’ choose the ‘TestDataBase’ created earlier. Click to test the connection and if it is ok click OK. You should now be able to preview the connection string that the data source will use.
The next step of the wizard prompts you to store the connection string in the web.config file. A web.config file is stored by each .NET web site and contains settings specific to that site. Click Next to store the connection in the web.config file. You can browse the web.config file later and find the entry for the connection string.
Next you will be asked ‘How would you like to retreive data from your database?’.
Click on the ‘Specify Columns from a table or view radio button and check the * column to return all the data in the table. The equivalent Select statement is shown. The final step lets you test the query to see that the data is returned correctly.
When you are returned to the design view you will see that the GridView now shows headings for each column selected from the DB table.
Step 9 Press Cntrl F5 to Test
When you test the web site a bubble pops up informing you that the ASP.NET development server has started running and gives you the URL of the local server. Internet Explorer ( you can change target browser ) fires up opening the Default.aspx page. If everything is in order the GridView will display the data from the People DB table. Hurrah!
On my machine the page above has a URL http://localhost:2073/quickstart/Default.aspx ,this is only valid when the development server is actually running. If you are a Flash developer developing a swf that requires data from a backend database, you can do everything locally just set URLs to point at pages on the local development server that could serve the data to your SWF.