MS SQL Server
  Home arrow MS SQL Server arrow Page 3 - Pulling Information using DataAdapter with...
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? 
MS SQL SERVER

Pulling Information using DataAdapter with ADO.NET
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 13
    2006-05-02

    Table of Contents:
  • Pulling Information using DataAdapter with ADO.NET
  • How to retrieve several rows returned by a stored procedure using "DataAdapter" in ADO.NET from ASP.NET
  • How to retrieve only one row returned by a stored procedure using "DataAdapter" in ADO.NET from ASP.NET
  • How to retrieve multiple result sets from stored procedure using "DataAdapter" in ADO.NET from ASP.NET

  • 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


    Pulling Information using DataAdapter with ADO.NET - How to retrieve only one row returned by a stored procedure using "DataAdapter" in ADO.NET from ASP.NET


    (Page 3 of 4 )

    I already introduced "DataAdapter" in my previous section.  We shall further extend it to get only a single row!

    Let us consider a simple stored procedure as follows:

    CREATE PROCEDURE dbo.sp_emp_getEmployeeDetails
          (
                @empno int
          )
     
    AS
          SELECT * FROM emp
                WHERE empno = @empno
          RETURN

    From the above stored procedure, it is quite certain that it returns only one row (or even none).  I would like to read that row and present it in the text boxes!

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
    As System.EventArgs) Handles Button1.Click
            Dim name As String
            Dim cn As New SqlConnection("Data Source=.;initial
    catalog=Northwind;user id=sa")
            Dim cmd As New SqlCommand
            With cmd
                .CommandType = CommandType.StoredProcedure
                .CommandText = "sp_emp_getEmployeeDetails "
                .Parameters.Add("@empno",1001)
                .Connection = cn
            End With
     
            Dim dt As New DataTable
            Dim da As New SqlDataAdapter(cmd)
            da.Fill(dt)
            da.Dispose()
            cmd.Dispose()
            cn.Close()
            Dim dr As DataRow = dt.Rows(0)
            Me.txtEname.text = dr("ename")
            Me.txtSal.text = dr("sal")
            Me.txtDeptno.text = dr("deptno")
            dt.Dispose()
        End Sub

    We need to concentrate only on the highlighted rows (in red).  You can observe that I am retrieving only the first row from the data table using the following statement:

            Dim dr As DataRow = dt.Rows(0)

    Once we get the handle to that row, we can simply retrieve all the column information and assign it to the text boxes using: 

            Me.txtEname.text = dr("ename")
            Me.txtSal.text = dr("sal")
            Me.txtDeptno.text = dr("deptno")

    I developed the application using Microsoft Windows Server 2003 Standard Edition with Microsoft Visual Studio.NET 2003 Enterprise Architect and Microsoft SQL Server 2000. If anything does not work, please drop me a line so that I can guide you.  The entire solution for this article is freely available in the form of a zip downloadable. 

    More MS SQL Server Articles
    More By Jagadish Chaterjee


       · Hello guys, I hope you are enjoying this series. This is another contribution for...
     

    MS SQL SERVER ARTICLES

    - Completing the Introduction to Transact-SQL
    - A Brief Introduction to Transact-SQL
    - Lookups and Blocking Bad Data
    - Field Validation Rules for Blocking Bad Data
    - Using Masks to Block Bad Data
    - Blocking Bad Data
    - Using @@ROWCOUNT and TABLE Variables for Dat...
    - How to Use Variables, IF and CASE in Databas...
    - Creating Important Aspects of Notification S...
    - Working wth Variables in Database Interactio...
    - Delving Deeper into Notification Services
    - Notification Services
    - Building a Multi-table Report with SQL 2005 ...
    - A Secure Way of Building Connection Strings
    - Transferring a Database Using the SSIS Desig...





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