ASP.NET
  Home arrow ASP.NET arrow Interacting with Databases Using ASP.NET 2...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Interacting with Databases Using ASP.NET 2.0 with the Microsoft Data Access Application Block
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 9
    2006-10-16

    Table of Contents:
  • Interacting with Databases Using ASP.NET 2.0 with the Microsoft Data Access Application Block
  • Retrieving a table of rows in the form of a data table using the Data Access Application Block
  • Retrieving a single row in the form of a data row using the Data Access Application Block
  • Retrieving a single value using the Data Access Application Block
  • Executing a DML statement using the Data Access Application Block
  • Summary

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Interacting with Databases Using ASP.NET 2.0 with the Microsoft Data Access Application Block


    (Page 1 of 6 )

    This is the third article in a series focusing on developing applications using ASP.NET 2.0 and the Data Access Application Block available in the Microsoft Enterprise Application Block Library for .NET 2.0. In this article, I shall go through the most used routines in almost every .NET application with several examples.
    A downloadable file for this article is available here.

    If you are new to Microsoft application blocks, I strongly suggest you refer to my first article in this series titled Developing ASP.NET 2.0 Applications with Microsoft Data Access Application Block.  It gives a solid foundation for beginners. 

    If you are unsure of how to select and use the proper database class, I suggest that you go through my second article in this series titled Connecting to Different Databases using ASP.NET 2.0.  In this article, I focus on working with Microsoft SQL Server.

    Retrieving a table of rows in the form of a dataset using the Data Access Application Block

    In this section, we shall retrieve a dataset by passing a SELECT statement to the Data Access Application Block.  Before we begin, modify your web.config in such a way that it looks something like the following:

    <configuration>
          <appSettings
    />
          <connectionStrings>            <add name=
    "AdventureWorks" connectionString=
    "
    Database=AdventureWorks;
    Server=(local)SQL2k5;Integrated
    Security=SSPI;
    " providerName="System.Data.SqlClient"
    /> 
          </connectionStrings>

    The following is the complete code needed to retrieve a dataset by passing a SELECT statement to the Data Access Application Block.

    ImportsSystem.Data
    Imports
    Microsoft.Practices.EnterpriseLibrary.Data.Sql
    Imports
    Microsoft.Practices.EnterpriseLibrary.Data
    Partial
    Class _Default
        Inherits System.Web.UI.Page
        Protected Sub btnConnect_Click(ByVal sender As Object, ByVal
    e As System.EventArgs) Handles btnConnect.Click
            Me.GridView1.DataSource = getDataset("select * from
    HumanResources.department"
    ).Tables(0)
            Me.GridView1.DataBind()
        End Sub
        Private Function getDataset(ByVal SQL As String) As DataSet
            Dim db As SqlDatabase = DirectCast
    (DatabaseFactory.CreateDatabase("AdventureWorks"), SqlDatabase)
            Dim ds As DataSet = db.ExecuteDataSet(CommandType.Text,
    SQL)
            Return ds
        End Function   

    EndClass

    You can observe that in the above code, I defined my own function “getDataset,” which accepts a SELECT statement as a parameter and returns a “Dataset” object. This lets us reuse the function any number of times within the same form very easily.

    You can also observe that I am assigning only the data table as the data source to the GridView control (as shown in the following):

    Me.GridView1.DataSource = getDataset("select * from
    HumanResources.department"
    ).Tables(0)
            Me.GridView1.DataBind()

    More ASP.NET Articles
    More By Jagadish Chaterjee


       · Hello guys! This is another contribution which extends the series on Data Access...
     

    ASP.NET ARTICLES

    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT