SunQuest
 
       ASP.NET
  Home arrow ASP.NET arrow Inserting and Deleting Data with Parameter...
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 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
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

Inserting and Deleting Data with Parameters in ASP.NET
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 10
    2007-09-26

    Table of Contents:
  • Inserting and Deleting Data with Parameters in ASP.NET
  • The INSERT, DELETE Code Example
  • Explaining the Code Example
  • Fixing the @EmployeeID SqlParameter

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Inserting and Deleting Data with Parameters in ASP.NET


    (Page 1 of 4 )

    In the first part of this article we saw how to display data, retrieved from a database table, on a web page. We also saw how to perform update operations, using the T-SQL UPDATE statement along with the SqlCommand and SqlParameter objects, if the user has changed the data on the web page. Today we will learn more about updating data. Specifically, we are going to use the T-SQL INSERT and DELETE statements to perform insert and delete operations on the data. I will be using the same example I used in the last article to add those new operations.

    Before I start I want tell you that there are better ways of working with data in ASP.NET, specifically the Data Source controls and the Data Bound controls, but now we are discussing the basic concepts of ADO.NET so I'm just presenting simple examples with a lot of ADO.NET code. By using those new controls you can create data-aware pages in a few seconds without writing a single line of code, but believe me it's better to spend time on writing ADO.NET code by hand first, especially if you are a beginner. I will discuss those new controls in great detail in my upcoming articles, namely the SqlDataSource, ObjectDataSource, GridView and DetailsView controls. For now let's continue with our web page.

    As you know, we perform the insert and delete operations using T-SQL INSERT and DELETE statements. For our example, we need to insert a new employee with first and last names only. The EmployeeID column is set as an IDENTITY column which means that its value is generated automatically by SQL Server, so we will not permit the user to enter a value for this column. The INSERT statement that we can use for this operation may look like this:

    INSERT INTO Employees(LastName, FirstName)
    VALUES ('Mick', 'Joseph')

    But we don't want to hard-code the values that we insert into the columns in the string value that we assign to the SqlCommand.CommandText property. We need to use a parameterized INSERT statement so we can pass values to those parameters. The code may look as follows:

    DECLARE @LastName NVARCHAR(20), @FirstName NVARCHAR(20)
    SET @LastName = 'Paul'
    SET @FirstName = 'Mina'
    INSERT INTO Employees(LastName, FirstName)
    VALUES (@LastName, @FirstName)

    This means we need to use SqlParameter objects from our ADO.NET code to create the parameters, assign values to them and send the statement to be executed on the server. The statement that the SqlCommand object built is different from the one above; we will take a look at it in the following sections.

    From our DELETE statement, we might execute something like this:

    DECLARE @EmployeeID INT
    SET @EmployeeID = 11
    DELETE FROM Employees
    WHERE EmployeeID = @EmployeeID

    As you can see, we are using a parameterized DELETE statement, so we will use the SqlParameter object from our ADO.NET code with this statement too. Let's see the code, and note that I have removed the UPDATE button from the web page for simplicity. As I have said, there are better and easier ways to do the same set of operations but we are writing those lines of code so you can get a good idea of how ADO.NET works.

    More ASP.NET Articles
    More By Michael Youssef


       · Please read the article Using Parameters with ADO.NET to Update Data in ASP.NET 2.0...
     

    ASP.NET ARTICLES

    - 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
    - Developing a Dice Game Using ASP.NET Futures...
    - Completing an ASP.NET AJAX Server-Centric Ba...
    - Information Management for an ASP.NET AJAX S...
    - Comment and Order Management for an ASP.NET ...
    - Back-end Management Tasks for an ASP.NET AJA...
    - User Information Management for an ASP.NET A...
    - Adding Comments and Search to an ASP.NET AJA...
    - Order-Related Modules for an ASP.NET AJAX Se...
    - User and Role Management for an ASP.NET AJAX...
    - Programming an ASP.NET AJAX Server-Centric B...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway