logo for asktheoracle.net
leftimage for asktheoracle.net

How To Integrate an ASP.NET Web Application With Oracle

This tutorial on using Oracle with Visual Studio and ASP.NET covers the basics of  database access and data manipulation from Visual C#. Security considerations (whilst important) are beyond the scope of this tutorial.

Prerequisites

  1. Install Visual Studio 2008 for building your application
  2. Install Oracle Database 10g XE. Make sure that you follow the instructions to enable the HR database. If you accepted the default settings of the installer, the server is in C:\oraclexe. The
listener.ora, sqlnet.ora and tnsnames.ora files are in c:\oraclexe\app\oracle\admin\XE\product\10.2.0\server\NETWORK\ADMIN.
3. Start the database
4. Install 11g Data Access Components (ODAC) for Visual Studio 11.1.07.20 or later. If you accepted the default settings of the installer this is installed in c:\app.
6. Copy listener.ora, sqlnet.ora and tnsnames.ora to c:\app\product\11.1.0\client_1\Network\Admin to enable the application to connect to the database and to prevent a TNS-12514: TNS: could not resolve the connect identifier error.

How To Create Your ASP.NET Web Site




Watch the above video and see how to do it in real time.
  1. Start Visual Studio 2008
  2. Select File->New Web Site from the menu
  3. You will see the list of templates appear on the "New Web Site" page. Select the "ASP.NET website" template; Visual C# as the language  and set the location to "c:\MyFirstWebSite"

    new ASP.NET web site

  4. Click on the Design tab when you see the “Default.aspx” page in your new ASP.NET web site. Your screen should look like this:

    Visual Studio blank canvas

  5. Move the mouse over the Toolbox tab to bring up the Toolbox.

    Visual Studio 2008 oracle data access
  6. Oracle Tips & Tricks
    to SKYROCKET Your Career!

    If you're not already a subscriber to Oracle Tips & Tricks you're  missing out on a myriad of tips and techniques to help you become a better, faster, smarter developer. Subscribe now and ignite your career.

  7. Expand Data and drag the GridView control to the canvas so that it  appears like this:

    Visual Studio Grid Control

  8. Next click the ">" to bring the GridView Tasks into view if it is not already and select <New data source…> from the drop down box.

    Create Data Source

  9. This starts the Dat Source Configuration Wizard as shown below. Choose Database as the Data source type, enter "HR" as the ID and click “OK” to advance to the next screen.

    Choose database as data source

  10. Click on the "New Connection" button to bring up the Add Connection screen.

    create database connection

  11. On the Add Connection screen, click on the “Change…” button.

    Change connection

  12. In the Change Data Source Dialog box ensure the Data source and the Data provider are set correctly as shown in the following screen shot.  Tick “Always use this selection” and click "OK"

    change data source

  13. Back on the "Add Connection" screen, set the Data Source Name to “HR.XE” and enter “HR” for both the user name and the password fields in the connection details tab.

    add connection

  14. Ensure all the details have been entered/set correctly by clicking on the "Test Connection" button. If an error message appears ensure the database is running, all the details have been entered correctly and that ODAC has been installed as per step 4 of the pre-requisites. If the test succeds the following message box will pop up.

    test connection to database

  15. Click the OK button then click Next on the Configure Data Source screen and click Next again to save the connection string and advance to configuring the select statement

    save database connection string

  16. To configure the select statement, select DEPARTMENTS from the Names drop down list and select “*” from the columns list to retrieve all columns from the departments table when the query is run. Then click the “Next” button to bring up the test query screen.

    create select stateement
  17. The “Test Query” dialog box appears with the select statement that you created in the previous step. Click the “Test Query” button to run the query and display the results.

    query results
  18. Click "Finish", to display the Data Grid View, then click the “Enable Paging” check box in the Grid View Tasks panel.

    data grid

  19. Navigate to Build->Build Web Site on the menu to build your ASP.NET web site.

    build website

  20. Assuming the build is successful, navigate to “Debug->Start Debugging” to run and test your web site.

    debug website
  21. Enable debugging in the web.config file and click OK.

    enable debugging of web site

  22. This causes the results of your query to appear in a browser. That's it - you've succesfully built an ASP.NET web site and connected it to the database.

    Query results ion browser
Looking for instructor-led training to learn SQL, PL/SQL, database administration, Linux/Unix system administration?

If so, learn fast with high quality, instructor-led training available in NZ and in the UK.