ASP.NET
  Home arrow ASP.NET arrow Page 4 - 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 
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

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


    Inserting and Deleting Data with Parameters in ASP.NET - Fixing the @EmployeeID SqlParameter


    (Page 4 of 4 )

    We need to modify the DeleteEmployee() method to be able to send the following code, which declares the @EmployeeID as an integer instead of nvarchar parameter.

    exec sp_executesql N'DELETE FROM Employees WHERE EmployeeID =
    @EmployeeID',N'@EmployeeID int',@EmployeeID=11

    Instead of the code:

    exec sp_executesql N'DELETE FROM Employees WHERE EmployeeID =
    @EmployeeID',N'@EmployeeID nvarchar(2)',@EmployeeID=N'11'

    We can do that by converting the value that we assign to the @EmployeeID SqlParameter to int value, using the .NET Convert class and its Int32 method which converts the value given to it as a parameter to int data type, and that's it.

    SqlParameter parameter = new SqlParameter("@EmployeeID",
    Convert.ToInt32(EmployeeIDTextBox.Text));

    The complete code for the method is:

    private void DeleteEmployee(){
      try{
        using (SqlConnection connection = new SqlConnection
    (connectionString)){
          string commandText = "DELETE FROM Employees WHERE
    EmployeeID = @EmployeeID";
          SqlCommand command = new SqlCommand(commandText,
    connection);

          SqlParameter parameter = new SqlParameter("@EmployeeID",
    Convert.ToInt32(EmployeeIDTextBox.Text));
          command.Parameters.Add(parameter);

          connection.Open();
          int rowsAffected = command.ExecuteNonQuery();
          if (rowsAffected == 1){
            ListBox1.Items.Clear();
            PopulateList(true);
          }
        }
      }
      catch (Exception ex){
        MessageLabel.Text = ex.Message;
      }
    }

    If you want to learn more about SqlParameter objects and how they can be used in your ASP.NET web pages, please read my three article series Creating the StudentDB Class for ASP.NET 2.0.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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

    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 2 hosted by Hostway
    Stay green...Green IT