ASP.NET
  Home arrow ASP.NET arrow Page 2 - 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 - Explaining the code in the example


    (Page 2 of 4 )

    The first method that we need to talk about is the GetEmployeesRecords() method. It uses a SqlConnection, SqlCommand and a SqlDataReader objects to retrieve the employee's EmployeeID, LastName and FirstName columns from the database, and assigns the returned rows to the ListBox control. The ListBox control represents its list in the form of ListItem objects; we can use the ListBox.Items property to access the collection of the list items of the ListBox control.

    The ListBox.Items.Add() method is used to create a ListItem object; to add a ListItem object to the collection, you simply pass the string that represents the list item to the method. We read a row from the database by calling the SqlDataReader.Read() method; combined with the while block, we can access the fields (that we have asked to return through the T-SQL statement), create one string that represents the row and add it to the ListBox control. We have placed the data access code inside a try/catch block to tell the user about the exception message if an exception is raised.

    We have created two private arrays, namely employeeFields and splitChar, which are used by the ListBox1_SelectedIndexChanged() event handler of the ListBox control. At this point, we need a mechanism for displaying the individual fields of each record in the three textboxes we have on the page, when the user selects a ListBox item.

    You already retrieved this data and stored it in the ListBox control. The mechanism we used doesn't access the database, but it uses the already existing data in the ListBox control. When the user selects another item, the SelectedIndexChanged event fires and the ListBox1_SelectedIndexChanged() method is called. Inside the method we can get to the new list item that the user has selected, through the use of the ListBox.SelectedItem property.

    The ListBox.SelectedItem property in turn has a property called Value that returns the string value that represents the ListItem object. So it's a string and all strings have the Split() method that accepts an array of type char, which contains one or more characters that are used to split the string, and returns an array of type string that contains the new partial strings. In our code, we have passed the comma as the separator character that's used to cut a string like "1, Davolio, Nancy" to 1 Davolio Nancy. The items are stored in the employeeFields string array and then assigned to the textboxes.

    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];
    }

    This happens every time you select a new item from the ListBox.

    When the ListBox control is populated its doesn't select any items. This means that our SelectedIndexChanged event will not fire for the first time the page loads. We have solved this problem by using the following code for the Page_Load event handler:

    protected void Page_Load(object sender, EventArgs e){
      if (!IsPostBack){
        GetEmployeesRecords();
        ListBox1.Items[0].Selected = true;
        ListBox1_SelectedIndexChanged(this, EventArgs.Empty);
      }
    }

    The Page class has a very important property called IsPostBack which returns false if the page is loaded for the first time and returns true if the page is posted back to the server. We test whether the page is loading for the first time. If so, we call the GetEmployeesRecords() method to populate the ListBox control with the data from the Employees table, then we select the first item in the ListBox through accessing it, through the indexer, and setting the Selected property to true.

    You may think that the last line of code is not necessary, but try to comment out that third line and run the page. You will find that the ListBox's first item is selected, but the textboxes are not reflecting that. This happens because the SelectedIndexChanged event fires when the selected index changes and because the selected index was -1. Before we select the first item we need to call this event handler directly from the page_Load method to make sure that the textboxes are populated with the appropriate values. If you need to know more about C# events please consult my articles C# Delegates Explained and C# Events Explained. Now let's modify the code to give the user the ability to update the values on those textboxes and submit the changes to the database.

    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

    - More Advanced ASP.NET 3.5 Functions and Subr...
    - ASP.NET 3.5 Functions and Subroutines
    - Coding an IQ Test with Conditionally Driven ...
    - Developing Conditionally Driven Event Handle...
    - ASP.NET 3.5 Debugging Using Visual Web Devel...
    - Understanding Event Handlers in ASP.NET 3.5
    - Building a Web Form in ASP.NET and PHP: a Co...
    - Inserting Data into a Microsoft SQL 2008 Dat...
    - Creating an ASP.NET Dynamic Web Page Using M...
    - Retrieving Data from Microsoft SQL Server 20...
    - Building ASP.NET Web Forms to Use a MySQL Da...
    - Creating an ASP.NET Database using MS SQL 20...
    - Building an ASP.NET Website Using Include Ta...
    - Create ASP.NET Web Forms to Use a Microsoft ...
    - Editing Web Design Layout in Visual Web Deve...





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