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.
Next: Modifying the example to provide the update feature >>
More ASP.NET Articles
More By Michael Youssef