ASP.NET
  Home arrow ASP.NET arrow Page 3 - Using Parameters with ADO.NET to Update Da...
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 
Moblin 
JMSL Numerical Library 
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

Using Parameters with ADO.NET to Update Data in ASP.NET 2.0 Pages
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2007-09-25

    Table of Contents:
  • Using Parameters with ADO.NET to Update Data in ASP.NET 2.0 Pages
  • Explaining the code in the example
  • Modifying the example to provide the update feature
  • Explaining the modified version of the example

  • 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 Parameters with ADO.NET to Update Data in ASP.NET 2.0 Pages - Modifying the example to provide the update feature


    (Page 3 of 4 )

    As usual let's see the code first, and then we will talk about what has been added to make this work. The following is the code for the Default.aspx page:

    <%@ Page Language="C#" AutoEventWireup="true"
    CodeFile="~/Default.aspx.cs" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
      <form id="form1" runat="server">
        <div>
          <asp:ListBox ID="ListBox1" runat="server" Height="180px"
    Width="200px"
    OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"
    AutoPostBack="True">
          </asp:ListBox> <br /><br />
          <asp:Label ID="EmployeeIDLabel" runat="server"
    Text="Employee ID"></asp:Label>
          <asp:TextBox ID="EmployeeIDTextBox" runat="server"
    Enabled="false"></asp:TextBox><br />
          <asp:Label ID="LastNameLabel" runat="server" Text="Last
    Name"></asp:Label>
          &nbsp; &nbsp;<asp:TextBox ID="LastNameTextBox"
    runat="server" OnTextChanged="TextBoxes_TextChanged"
    AutoPostBack="True"></asp:TextBox><br />
          <asp:Label ID="FirstNameLabel" runat="server" Text="First
    Name"></asp:Label>
          &nbsp;&nbsp;<asp:TextBox ID="FirstNameTextBox"
    runat="server" OnTextChanged="TextBoxes_TextChanged"
    AutoPostBack="True"></asp:TextBox><br /><br />
          <asp:Button ID="UpdateEmployeeButton" runat="server"
    Text="Update Employee" OnClick="UpdateEmployeeButton_Click"
    Enabled="False" /><br /><br />
          <asp:Label ID="MessageLabel" runat="server"></asp:Label>
        </div>
      </form>
    </body>
    </html>

    Next is the code for the Default.aspx.cs:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    // use the namespace of the ADO.NET SQL Server Data Provider
    using System.Data.SqlClient;
    public partial class _Default : System.Web.UI.Page{
      private string connectionString = "Data Source=(local);Initial
    Catalog=Northwind;Integrated Security=True";
      private string[] employeeFields = new string[3];
      private char[] splitChar = new char[1] { ',' };

      protected void Page_Load(object sender, EventArgs e){
        if (!IsPostBack){
          PopulateList();
        }
      }

      protected void ListBox1_SelectedIndexChanged(object sender,
    EventArgs e){
        employeeFields = ListBox1.SelectedItem.Value.Split
    (splitChar);
        EmployeeIDTextBox.Text = employeeFields[0];
        LastNameTextBox.Text = employeeFields[1];
        FirstNameTextBox.Text = employeeFields[2];
      }

      protected void GetEmployeesRecords(){
        try{
          using (SqlConnection connection = new SqlConnection
    (connectionString)){
            string commandText = "SELECT EmployeeID, LastName,
    FirstName FROM Employees";
            SqlCommand command = new SqlCommand(commandText,
    connection);

            connection.Open();
            using (SqlDataReader dataReader = command.ExecuteReader
    ()){
              while (dataReader.Read()){
                ListBox1.Items.Add(dataReader["EmployeeID"] + "," +
    dataReader["LastName"] + "," + dataReader["FirstName"]);
              }
            }
          }
        }
        catch (Exception ex){
          MessageLabel.Text = ex.Message;
        }
      }
      protected void UpdateEmployeeButton_Click(object sender,
    EventArgs e){
        try{
          using (SqlConnection connection = new SqlConnection
    (connectionString)){
            string commandText = "UPDATE Employees SET LastName =
    @LastName, FirstName = @FirstName " + "WHERE EmployeeID =
    @EmployeeID";
            SqlCommand command = new SqlCommand(commandText,
    connection);

            command.Parameters.AddWithValue("@LastName",
    LastNameTextBox.Text);
            command.Parameters.AddWithValue("@FirstName",
    FirstNameTextBox.Text);
            command.Parameters.AddWithValue("@EmployeeID",
    EmployeeIDTextBox.Text);

            connection.Open();
            int rowsAffected = command.ExecuteNonQuery();
            if (rowsAffected == 1){
              MessageLabel.Text = "The Employee record has been
    updated.";
              ListBox1.Items.Clear();
              PopulateList();
            }
          }
        }
        catch (Exception ex){
          MessageLabel.Text = ex.Message;
        }
      }
      protected void TextBoxes_TextChanged(object sender, EventArgs
    e){
        UpdateEmployeeButton.Enabled = true;
      }

      private void PopulateList(){
        GetEmployeesRecords();
        ListBox1.Items[0].Selected = true;
        ListBox1_SelectedIndexChanged(this, EventArgs.Empty);
      }
    }

    Now when you run the page you will be able to select any employee's item from the ListBox control, change its last name or first name through the TextBoxes and click the Update Employee button to submit the changes to the Employees database table. The following two screen shots are captured before and after clicking the update button. I just changed the first name of the first employee from Nancy to Nanci.

    More ASP.NET Articles
    More By Michael Youssef


       · I hope that you find this article is useful for learning the basics of ADO.NET
     

    ASP.NET ARTICLES

    - 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
    - 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 ...





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