ASP.NET
  Home arrow ASP.NET arrow Page 3 - Using T-SQL Stored Procedures with ASP.NET...
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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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

Using T-SQL Stored Procedures with ASP.NET 2.0
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 26
    2007-10-17

    Table of Contents:
  • Using T-SQL Stored Procedures with ASP.NET 2.0
  • CREATE PROCEDURE T-SQL Statement
  • ALTER PROCEDURE T-SQL Statement
  • Creating a Stored Procedure with Input and Output Parameters

  • 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


    Using T-SQL Stored Procedures with ASP.NET 2.0 - ALTER PROCEDURE T-SQL Statement


    (Page 3 of 4 )

    You can modify your stored procedure's code by using the ALTER PROCEDURE T-SQL statement. You need to specify the name of the stored procedure you want to ALTER along with the stored procedure's body. Let's modify our GetBasicCustomerData to include more columns. Run the following code in SQL Server Management Studio with the Northwind database as the current database:

    ALTER PROCEDURE GetBasicCustomerData
    AS
    SELECT CustomerID, CompanyName, ContactName, City, Country
    FROM Customers

    Of course you need to change your code to include the additional columns indicated and write their fields to the page. The following is the modified version of the Page_Load() event handler method that runs with any number of columns returned from the stored procedure. Replace the previous event handler method with the following:

    protected void Page_Load(object sender, EventArgs e)

    {

    string connectionString = "Data Source=(local);Initial
    Catalog=Northwind;Integrated Security=True";

      try

    {

    using (SqlConnection connection = new SqlConnection
    (connectionString))

    {

      string commandText = "GetBasicCustomerData";

       SqlCommand command = new SqlCommand(commandText, connection);

        command.CommandType = CommandType.StoredProcedure;

         connection.Open();

       using (SqlDataReader dataReader = command.ExecuteReader())

    {

      while (dataReader.Read())

    {

      for (int i = 0; i < dataReader.FieldCount; i++)

    {

    if(!(i + 1 == dataReader.FieldCount))
    Response.Write(dataReader[i] + ", ");

      else

       Response.Write(dataReader[i]);
    }

      Response.Write("<br />");

       }

      }

     }

    }

      catch (Exception ex)

    {

      Response.Write(ex.Message);

     }

    }

    Now run the page and you will get all the fields returned from the stored procedure.

    What we have done in this example is access the fields using their column index instead of their column name. And because we can know how many columns have been retrieved in the result set from the database, through the SqlDataReader.FieldCount property, we can use a for loop to access every field and write its value to the web page. After the loop ends we can write a <br /> before the end of this loop iteration. The if statement inside the for loop tests to see if this field is the last one in the row or not; if it's the last field then it doesn't write the colon character to the page.

    Now we can alter the stored procedure one more time to include the ContactTitle column as in the following code:

    ALTER PROCEDURE GetBasicCustomerData
    AS
    SELECT CustomerID, CompanyName, ContactName, ContactTitle, City, Country
    FROM Customers

    When you run the page, without any modifications to the code, you will see the new column values written to the page as in the following screenshot.

    You can create more complicated stored procedures using both input parameters and output parameters, so let's see an example.

    More ASP.NET Articles
    More By Michael Youssef


       · Feel free to post a comment on the article. If you need more practice, and examples,...
       · Conditional logic if blah do this SQL statementelse do that SQL...
     

    ASP.NET ARTICLES

    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...
    - Building an In-Text Advertising System Under...
    - Developing a Mini ASP.NET AJAX Server Centri...





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