Click here to download the source code for this lesson.
In this previous lesson, I demonstrated how to use the ASP.NET FormView to display data from the database in some TextBox controls with one line of C# code. In this lesson I am going to show you how to update a database table with values from TextBox controls using the FormView and SqlDataSource controls. Just like in the other examples with the FormView and SqlDataSource, you don’t have to write much database integration code and in some cases, no database code at all. This helps to reduce redundant work in your ASP.NET programs.
For this example I am going to keep using a web form named Customer_Edit.aspx which has three TextBox controls: first name, last name and e-mail address. This web form is designed to allow the user to edit information about a customer and then save the information back to the database. For this lesson, I will focus on how to save the edited customer’s information to the database. The data is stored in a database table named “customer”.
The first thing I had to do is to setup my SqlDataSource so that it knows what database command to run when an Update command occurs. I set the UpdateQuery to the following value: “UPDATE customer set first_name = @first_name, last_name = @last_name, email_address = @email_address where customer_id = @customer_id”. Notice that I am using named @ parameter variables for all of the columns. This means that the value for these variables is going to be determined at execution time by ASP.NET. In this example the value for the variables is going to come from the TextBox controls because each one of the TextBox controls has something similar to this: Text=’<%# Bind(”first_name”) %>’, which binds the TextBox to the variable first_name in the SqlDataSource.
The value for the variable @customer_id comes from a special property of the FormView control. I had to add a property named “DataKeyNames”. This property tells the FormView which database column is the unique key for the Select, Insert, Update and Delete operations that can occur. In my example I set this property to DataKeyNames=”customer_id” since the customer_id column is the unique indentifier that I used for my Select and Update operations on the customer table.
Next, I set another special property of the FormView control: OnItemUpdated=”FormView1_Updated”. OnItemUpdated tells the FormView what event method to call in your source code when an Update operation occurs. The reason that I added this in my example is because I want to redirect to another web page when the Update operation completes.
Next, I added a Save Button control inside the EditItemTemplate, just below all of the TextBox controls. I set a special property of the button: CommandName=”Update”. The CommandName property in this example tells the FormView to execute the UpdateQuery in the underlying SqlDataSource when the button is clicked.
Finally, there is one last thing that I had to do in order to complete this lesson. I had to add code for the FormView1_Updated method so that ASP.NET will call it when the Update operation completes:
protected void FormView1_Updated(Object sender, FormViewUpdatedEventArgs e) { Response.Redirect("Customer_List.aspx"); }
Notice that I didn’t have to add any C# code to make the database Update operation work.
In case you missed or didn’t understand some of the things in this lesson, don’t worry, there was a lot to learn here. Go back to the previous two lessons and read them again along with this one. Watch the videos again as well a couple of times so that you become familiar with these techniques and then experiment with them on your own.