ASP.NET
  Home arrow ASP.NET arrow 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  
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 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 / 13
    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


    (Page 1 of 4 )

    As promised in the previous article, this article will discuss in detail how to create and use Sqlparameter objects with parameterized queries and stored procedures. You will use these queries and procedures to retrieve and update information in a database.

    The code that we introduce in this article is divided into two steps. The first step involves using a ListBox control to retrieve data from the Employees table in the Northwind database and displaying each record's fields in textboxes to give the user the ability to change the values of those fields. The second step provides the update feature in the web page with the new values and submits them to the database. This process involves using the SqlParameter class with the SqlCommand class as we are going to see. In the next article we are going to add the insert and delete operations to the web page.

    So let's see the code first. The following is the code that you need to have in the Default.aspx file:

    <%@ 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 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"></asp:TextBox><br />
          <asp:Label ID="FirstNameLabel" runat="server" Text="First
    Name"></asp:Label>
          &nbsp;&nbsp;<asp:TextBox ID="FirstNameTextBox"
    runat="server"></asp:TextBox><br /><br />
          <asp:Button ID="UpdateEmployeeButton" runat="server"
    Text="Update Employee" OnClick="UpdateEmployeeButton_Click" />
          <br />
          <br />
          <asp:Label ID="MessageLabel" runat="server"
    ForeColor="Red"></asp:Label>
        </div>
      </form>
    </body>
    </html>

     

    Place the following code in place of the auto-generated code of the Default.aspx.cs file:

    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){
          GetEmployeesRecords();
          ListBox1.Items[0].Selected = true;
          ListBox1_SelectedIndexChanged(this, EventArgs.Empty);
        }
      }

      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){ }
    }

    When you run the web page you will find that the ListBox control has been populated with items representing a concatenated string of the EmployeeID, Last Name and First Name from the Employees table. When you select another item in the ListBox control, the textbox controls will reflect the new selected record, giving the user the ability to change the Last Name and the First Name values of any employee that is selected in the ListBox control. Note that we don't allow the user to change the EmployeeID field by assigning false to the TextBox.Enable property.

    When you first run the page you will note that the first item in the ListBox is selected and the textboxes are populated with the values of that list item. Select another item and the textboxes will reflect that item as shown in the next screen shot.

    The update button's click event handler is not operational yet, so for now let's talk about the code that makes this example work.

    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

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek