Project Resources
Note: I have tried to trim some of the resources out that no longer apply to the current project. If you have resources that you find valuable that you think should be listed send me an email.
Local Resources
- Getting Started With SQL Server
- Importing Data Into SQL Server
- Setting Up Your Webspace
- Getting the Most From Visual Studio
- Connecting to SQL from .NET
- Your Group Webspace
- Cross Database Queries
- Maintaining State Across Web Forms
Web Resources
- ASP.NET
- C# / .NET Framework
- C# Web Services
- SQL Server 2005
Getting Started With SQL Server
We will be using Microsoft SQL Server 2005 running on iisqlsrv.cs.washington.edu. Accounts on it have been set up for each of you, and you have been given your user names and passwords. For the first homework, you need to set up an interactive query session with the server. For this, you will need access to SQL Server Management Studio, which is installed on the machines in the undergrad labs. You will it for both administrative tasks and interactive querying. The first thing you need to do is register the server, and change your password.
- Go to Start -> All Programs -> Microsoft SQL Server(client) -> SQL Management Studio.
- A box labeled "Connect to Server" will show up.
- In the box labeled SQL Server, enter IPROJSRV.cs.washington.edu.
- Select the Use SQL Server Authentication option.
- Enter your login and password.
- Click OK.
- This will connect you to the server, on the left side of the screen is an expanding tree.
- Expand the security node, then the logins node.
- Find your user name, right click and go to properties.
- Change your password then click OK.
- Important: Use a password that you wont mind sharing with your teammates.
For the first homework, you will use Management Studio to connect to the server, and run SQL queries. This will involve the following steps:
- Log into the server using Management Studio. (You should know how to do this if you changed your password.)
- To start a new query click New Query in the upper left.
- Type in use movies, and press F5 to execute the statement. This will set your current database to be the movies database, used in the first homework.
- You can now enter and execute any SQL statements. Pressing F5 sequentially executes all the statements currently in the query pane, and the results are displayed in the results pane.
The variant of SQL supported by SQL Server is called Transact-SQL. In addition to executing all Transact-SQL statements, management Studio can also be used to view query execution plans, statistics, and more.
You are welcome to create and play with your own databases. You can either use SQL statements for this, or you can use Management Studio.
Importing Data Into SQL Server
At some point in the homework or the project you are going to need to import data into SQL server from a text file. There is good news and bad news. The good news is Management Studio has a nice wizard for importing data, the bad news is that it doesn't work. =) To work around this, you can import the data first into a MS Access database then use the wizard in Management Studio to import that database into SQL Server. To do that, follow these stpes:
- Open MS Access, create a new blank database
- Goto File -> Get External Data... -> Import
- Change the file type to txt, and locate your file
- Follow the wizard, remember to check the box if the first row contains column names and select "no primary key"
- CLOSE MS Access
- Open Management Studio
- In the Object Browser, right click your database and go to Tasks... -> Import Data...
- After clicking Next >, choose MS Access as your datasource, then locate your database file (it doesn't require a username/password)
- The destination is the database you are importing to, this time you will need to click "Server Authentification" and put in your credentials.
- Follow the wizard =)
- Did you know there were over 42 thousand zip codes in the USA as of the year 2000?
Setting Up Your Webspace
The server we have for publishing ASP.NET applications is the same server that we use to host the database, IPROJSRV. Unfortunately there are many ways to write buggy ASP.NET code that will crash the server in such a way that it needs reset. Thus you can only use IPROJSRV to publish your final product. For development and debugging, you must use a lab machine, or your own computer.
Configuring Workstation for Development
Setting up a lab workstation for ASP.NET development is a royal pain in the you know what. So if you have your own laptop with XP Professional, IIS and Visual Studio, I would suggest using that!
If you must use a lab computer, there are instruction here.
Publishing Your Project
Map your folder on IPROJSRV to your local machine. (Instructions for XP)
- Right Click My Computer
- Choose Map Network Drive...
- In Folder Put: iprojsrv.cs.washington.edu\[YOUR CSE NETID]
- If you're not on a lab machine, change your username/password using the "different user name" link.
- Click OK
Copy everything from your project directory into your newly mapped drive.
Now you should be able to see your project at http://iprojsrv.cs.washington.edu/phase1/[YOUR UW email]/default.aspx.
Getting the Most From Visual Studio
We recommend using Visual Studio for your projects in this class. The lab machines already have Visual Studio 2003 installed. Visual Studio is packed with useful features for .NET development. I'm just going to list a couple here that I think get overlooked alot.
- Object Browser
This is a browser that gives you quick and easy access to .NET Framework API reference.
- Find in Files/Replace in Files
These additional find dialogs allow you to find/replace over multiple files in your project or over your whole solution. They also allow you to use regular expressions to find and replace.
When working with web forms, avoid embedding the C# code in your web form. You can use what’s called 'code behind' to separate the C# into *.cs files. Using code behind lets use take advantage of Visual Studio's intellisinse and syntax correction features. (The starter files will have examples of codebehind.)
Connecting to SQL from .NET
To connect to a SQL database in .NET, you use the classes in the System.Data.SqlClient namespace. The following example shows how you would set up and connect to the movies database on IPROJSRV.
private SqlConnection sqlConn; // Declare a connection object sqlConn = new SqlConnection("server=IPROJSRV.cs.washington.edu; uid=yourID; pwd=yourPassword; database=movies"); sqlConn.Open(); // open the connection
Once you are connected to the database, you also use the APIs to execute queries. The following example shows how to select all rows from the actors table.
private SqlCommand sqlCmd; // Declare a new sql command private DataReader myResults; // Declare a new data reader sqlCmd = new SqlCommand("SELECT * FROM actors", sqlConn); // create a new command using an SQL statement myResults = sqlCmd.ExecuteReader(); // executes command on server and returns a reader if (myResults.HasRows) { // do something with the data }
Once you have finished using the sqlConnection on a page, make sure you close it so that it can return to the connection pool.
sqlConn.Close(); // close the connection
Your Group Webspace
Your group webspace works just like your personal webspace did in phase 1 only this time the folder is //iprojsrv/groups/[GROUP NAME]. [GROUP NAME] is the name of your group that you submitted, or an arbitrarily chosen group name if you failed to submit one on time. All of your group members should have access to the group folder.
You can also use your group folder on IPROJSRV for a CVS repository if you like. Check out the TortoiseCVS tool for CVS in the Windows shell.
Cross Database Queries
Some of the tasks in Phase 2 will require you to do a cross-database query. In order to do so you need to grant your teammates access to your database. The easy way to do this is by using a GRANT query. (This can also be done using Management Studio, but that's more difficult.)
USE [Your Database]; GRANT SELECT, INSERT, UPDATE, DELETE TO [teammate1], [teammate2]
Then when you're writing your queries, you can refer to your teammate's tables as [teammate1's_database].dbo.[table_name]. Example:
SELECT * FROM nbales_inventory.dbo.products AS a, akgupta_inventory.dbo.products AS b WHERE a.upc_code = b.upc_code
The same example in join syntax:
SELECT * FROM nbales_inventory.dbo.products AS a INNER JOIN akgupta_inventory.dbo.products AS b ON a.upc_code = b.upc_code
Maintaining State Across Web Forms
But wait . . . there are different meanings to "across web forms."
On the same page when it has to post back to the server.
This one Microsoft has done its best to take care of for you! Yay! The concept they use is called "viewstate." Basically its just an invisible form field that holds a bunch of encrypted data about the page state. It gets sent back and forth between the client and the server as long as the same page is being requested over and over. If you want to add your own state to the viewstate then use the ViewState object. It's basically a name value collection that you can access via Page.ViewState[key].
Between two different pages in a users session.
There are two ways to accomplish this. One is to use the Session object, the other is to use cookies. The Session object is preferred because cookies are limited size and a lot of people naively reject cookies. The Session object, like the ViewState object, is a name value collection that you can access via Page.Session[key].
Between two sessions by the same user.
There are also two ways to persist state across separate user sessions, cookies and keeping it in the database. Both have advantages. Cookies are stored on the client side and can thus help you remember who this is. You have to use cookies if you want users to automatically log into something. On the other hand, cookies can't hold much data, so if you wanted to remember what products a user has looked at, for example, this wouldn't fit in the cookie and must be stored in the database. If you want to write to the cookie, use the Response object: Page.Response.Cookies[key] = value;. To read cookies use the request object: value = Page.Request.Cookies[key];.