Create Your Next Application and Database in the Cloud
 
Published: 25 Jan 2011
Abstract
This article will demonstrate how to create a database in the Cloud and then create an ASP.NET web site which uses this database as the back end.
by Vince Varallo
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 25076/ 53

Introduction

In a previous article I wrote about how to build an ASP.NET web application using Visual Studio 2010.  This article will show you how to create a database using SQL Azure services and then how to create an ASP.NET web application for the cloud that communicates with that database.

Step 1: Download the SDK and tools for Visual Studio 2010

1.    Before you start creating applications for the Cloud you must first download the Windows Azure Tools for Visual Studio.  This can be found here.

2.    You also need to install and configure IIS 7.  You need to be running Windows 7, Vista, or Windows Server 2008.  Click here to read the instructions on setting up IIS 7.

3.    Now that you've installed IIS 7 you can install the Azure Tools by running the VSCloudService.exe file that you downloaded in Step 1.

4.    Once the Azure Tools are installed you need to install the hot fixes.  Depending on your operating system you'll need to install different ones.  They can be found here.

Step 2: Create the Database

1.    You will need to create a Windows Azure account to utilize the Azure services.  You can click here to find the latest offers from Microsoft.  There is usually a free 30 trial introductory offer so you can try before you buy.

2.    Once you've created a Windows Azure account you need to go to http://windows.azure.com to log into the Windows Azure Management Portal.

3.    The management portal is where you can create a new site, called a hosted service, or create a new database.  We'll first create a database and then create a web site with Visual Studio 2010 to display data from a table in the database.

4.    To create a new database click on the "New Database Server" icon at the top of the Management Portal.

5.    On the left side you will see "Subscriptions".  Click on the subscription name that you created when you created your Windows Azure account.  For this sample I'm using the "Windows Azure Pass" subscription.  When you click on it, the buttons on the top toolbar should become enabled.

6.    Click the Create button in the Server section.  This will bring up the "Create Server" wizard.

7.    Select the region where you want your database deployed and click the Next button.

8.    Create an administrator user name and password and click the Next button.

9.    The last page allows you to enable specific IP addresses that can access this server.  You should add your IP address by clicking the Add button.  When you click the Add button another page will display and tell you your current IP address.  Give this rule a name and then enter a start and end range.  In this example I'm using the same address for both.

10. Click the OK button and then click the Finish button.  You'll be brought back to the Management portal and the new server will be added to the subscription.  In this example the new server name is "its88ze5xt".

11. Click on the new server that you just created to highlight the row.  The "Create" button on the Database toolbar should become enabled.  Click the "Create" button.

12. A window will pop up and ask you to give the database a name, pick the edition, and a maximum size.

13. Enter SampleDB for the name and select Web for the edition.  Leave the maximum size at 1GB.  Now click the OK button.

14. The database will be created and will appear under the subscription tree in the left hand pane.

15. Click on the Database in the left hand pane and the "Manage" button should become enabled on the Database toolbar.  Click this button to create a table in the database.

16. The Database Manager will open in a new window and you will need to log in using the same user name and password that you created when you created the server.

17. Once you entered the correct credentials click the Connect button.

18. You may get an error when you click the Connect button.  If you click on the error details you'll see that the error is caused because another IP address needs to be entered in the firewall rule.  The details of the error tell you which IP address to add.  Copy this address and click back to the Management Portal.

19. Click on your server name under the Subscription node in the left hand pane.

20. Click the Firewall Rules button.

21. Click the Add button.

22. Enter another name and then add the IP address that you copied down in step 18.  Now click the OK button.

23. Click back the Database manager window and try to connect again.  You should now be able to log into the Database manager.

24. This is where you can create tables, stored procedures, views, and run queries.  Click on the "New Table" button in the toolbar.

25. The Database Manager will create a table template with 3 fields.  Change the table name to "Person".

26. Change the ID field so it is an Identity field.

27. Change Column1's name to "FirstName".  To change a column name double click on the name and the cursor should appear in the cell.  You can then delete the old and enter the new.

28. Change Column2's name to "LastName".

29. Change both fields to Required.

30. Click the "+ Column" button to add a new column.  Change the name of the new column to "Email" and check the "Is Required" check box.

31. Click the Save button on the toolbar to save the table.  The table should be created.  Click on the ">" button next to the word Tables (1) on the side menu.  You should see the dbo.Person node.

32. Click on the "dbo.Person" node and then click the "Data" button on the toolbar.

33. This brings up a page that allows you to add records to the table.

34. Click the "+ Row" button to add a row to the grid.

35. Double click on a cell to enter a first name, last name, and email.  Add a few records because this is what will be displayed in the application we will build.

36. Click the save button to commit your changes.

Now you can log out of the Database Manager and we will build an ASP.NET web site to pull the data from this table and display it in a GridView control.

Step 3: Create a New Solution

1.    Launch Visual Studio 2010.

2.    Select FileàNew Project… from Visual Studio's menu.

3.    Expand the Visual C# templates and click on the Cloud node.  You should see the Windows Azure Cloud Service template.

4.    Change the name of the project to "ASPAllianceSample" and click the OK button.

5.    The "New Cloud Service" dialog box should appear.

6.    Click on the ASP.NET Web Role in the left pane and click the > arrow to move it to the right pane.  This allows you to create a traditional ASP.NET web site for the cloud.  You can create a MVC web site, WCF service, background process or GCI application but for this example we'll stick with the traditional ASP.NET web site.

7.    Click the OK button.

8.    Visual Studio will add a whole bunch of standard files for a typical ASP.NET application. Some of these files include a master page, the login and password management pages, a style script, an about page, and a scripts folder. 

9.    Click the run button to start the project.  You should get the following message.

10. To remedy this you need to close Visual Studio and then launch Visual Studio again as an administrator.  You can do this by clicking on StartàAll ProgramsàMicrosoft Visual Studio 2010 and then right click on Microsoft Visual Studio 2010.  Select "Run as administrator" from the pop-up menu.  Now you need to open the ASPAllianceSample project again.

11. Now try running the project again.  If you don't have SQL Express installed you'll get the following message.

12. The Output window will give you a more descriptive message.

"Windows Azure Tools: Failed to initialize Development Storage service. Unable to
start Development Storage. Failed to start Development Storage: the SQL Server 
instance ‘localhost\SQLExpress’ could not be found.   Please configure the SQL
Server instance for Development Storage using the ‘DSInit’ utility in the Windows 
Azure SDK."

13. To fix this open the Windows Azure SDK Command Prompt.  This is found at StartàWindows Azure SDK v1.2àWindow Azure SDK Command Prompt.

14. Enter the following in the command window.

dsinit /sqlinstance:.

15. You should see the following dialog.

 

16. Click the OK button and then run the project again.  You should see the following web page.

17. Close the browser to stop debugging.  Now we'll add a GridView control to the default page that displays the records in the Person table in our database in the cloud.

18. Open the Default.aspx page in design mode and drag a GridView control from the toolbox to anywhere on the page.

19. When the GridView is dropped on the page the GridView Task window should appear which allows you to configure the control.  Select <New data source…> from the "Choose Data Source:" drop down list.  The "Data Source Configuration Wizard" will appear.

20. Select "SQL Database" from the list of sources and change the ID to SampleDB.

21. Click the OK button.  The "Configure Data Source" window will appear. 

22. Click the "New Connection" button.  This will display the "Add Connection" window.

23. The server name should be the server name where your cloud database is located.  To figure this out you can go back to the Database Manager.  To do this open your browser and navigate to http://windows.azure.com.  You need to log into your account and then click on the "Database" icon on the left pane in the dashboard.

24. The "Subscriptions" should appear on the left hand side of your screen.  Expand your subscription.  This will display your servers.  Expand your servers and you should see the "SampleDB".  Click on this node and then click the "Manage" icon in the toolbar.

25. The Database Manager will appear and your server name is displayed in the "Server" input box.  Copy this value and go back to Visual Studio to enter this name in the Connection server name.

26. Change the authentication type to "Use SQL Authentication" and enter the user name and password you created when you created the database.

27. Change the database to SampleDB by selecting it from the list in the "Connect to a database" section.

28. If you click the "Test Connection" button you should get a "Test connection succeeded" message if you've entered the correct information.

29. Now click the OK button.  This will bring you back to the "Configure Data Source" window.

30. Click the "Next" button.

31. Keep the box checked that says "Do you want to save the connection in the application configuration file?"  Click the "Next" button.

32. The next screen allows you to specify the table and the fields you want to select from the database.  The "Person" table should appear in the list of tables or views.  This table should already be selected but if it isn't you should select it.

33. The ID, FirstName, LastName, and Email fields should be listed in the columns list.  Leave the "*" selected so all the fields are returned.

34. Click the "Next" button.

35. The next screen allows you to test your query.  If you click the "Test Query" button you should see the data in your table.

36. Click the "Finish" button.

37. Run the application again and you should see the Default page with the data from the table.

Now that you have that working we can deploy this to the cloud.  You don't have to deploy it to the cloud.  You can just stop here and deploy it to your local production servers and leave the database hosted in the cloud but for those of you interested in how to deploy the application to the cloud read on.

Step 4: Deploy the Solution to the Cloud

1.    Before you deploy your project to the Cloud you need to create a Service Package using Visual Studio 2010.  This is done by right clicking on the "ASPAllianceSample" node in your solution explorer.

2.    Select Publish from the pop-up menu.  You should see the Publish Cloud Service dialog box.

3.    Choose the option to "Create Service Package Only" and click the OK button.

4.    Visual Studio will create two files and open up Windows Explorer so you can upload them to the Windows Azure web site.  The two files are called ASPAllianceSample.cspkg and ServiceConfiguration.csfg.

5.    Open up Internet Explorer and navigate to https://windows.azure.com/

6.    Once you log in to your account you should see your home page.

7.    Click on the "New Hosted Service" button in the upper left hand corner of the page.

8.    This will display the "Create a new Hosted Service" dialog.

9.    Enter "ASPAllianceSample" for the name.

10. Enter "ASPAllianceSample" for the URL Prefix.  The URL Prefix must be unique across all sites so you'll have to append a number to the end of the prefix until the site tells you it is unique.  The name you enter here is the publicly available name that people will use to get to your web site.

11. Next, select a region where you want your site hosted.  I'll choose "Anywhere US" for this example but you may want to choose a region closer to your target audience.

12. Under "Deployment options" chose "Deploy to production environment".  You can deploy to the stage environment and then promote stage to production but for this example we'll just deploy directly to production.

13. Enter "ASPAllianceSample" for the "Deployment Name".

14. Now you need to upload the package and configuration files that were created by Visual Studio.  Click the "Browse Locally…" button on the respective sections to upload the files.

15. Now click the OK button.  You'll get a warning regarding the fact that there is only one role with one instance for this deployment.  You can ignore the warning by clicking the Yes button.

16. The hosted service will then be created after a several minutes.  The first time I did this it took about 10 minutes.

17. Once the service is created you can then open Internet Explorer and navigate to http://aspalliancesample.cloudapp.net/ to see your application.  Remember to change the prefix in the URL to whatever you used when you created the URL Prefix in step 10.

18. Just when you thought you were done, you're not.  If you run the application you'll get an error.  This is because you have to create a firewall rule to allow the IP address of the hosted application to access the database.  To find out the IP address you'll need to look at the details of the error message.  To do this you'll can add a section to your web.config file and redeploy the application.  Add the following section to the <system.web> section

<customErrors mode="Off"></customErrors>

19. You'll need to save and then publish the configuration files again.  Once this setting is in place on your server you can navigate to the site and you should get the details of the error message in the default .NET error page.

20. You'll need to add this IP address to the list of allowed IP addresses in the firewall rules just as you did when you setup the database.

21. Once the firewall rule is in place you should be able to navigate to your application's URL and see the default page with the records from the database displayed.

Summary

This article demonstrated how to create a database in the cloud and then how to use Visual Studio 2010 to create a cloud application that communicates with that database.  As you saw in this article, even if your database is in the cloud, creating an application is not any different than if the database were local.  There are obvious financial benefits for having your database hosted in the cloud but there are security issues that go along with it.  It is up to you to decide what makes sense to put in the cloud and what not to put in the cloud.  At least you have the option to do so and Microsoft has made it pretty easy to get yourself up and running quickly.  Good luck on your project and happy coding. 



User Comments

Title: garland.kg69@asp.alliance/com   
Name: Kenneth A Garland III
Date: 2011-03-04 12:53:26 PM
Comment:
I wish to have my own web-site in the cloud .
Title: Cloud at GoDaddy   
Name: Kevin Tweedy
Date: 2011-01-25 5:19:44 PM
Comment:
Does anyone have any experience with the Cloud at a GoDaddy hosted area? None of their "Help" [s.i.c.] files reference the Cloud at all and in the past they've been extremely lagging in getting with "the latest and greates". Any pointers, info?

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-20 10:29:52 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search