More Ways to Update Databases using ASP.NET 2.0 SqlDataSource - Using a SqlDataSource control in the Default2.aspx page
(Page 3 of 5 )
On this page, we are going to put Labels and TextBoxes to get the updated fields from the users. The code that you need to put in the Default2.aspx page is as follows:
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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:Label ID="EmpIdLabel" runat="server" Text="You are Updating
a record for the Employee with the ID: "></asp:Label><br />
<asp:Label ID="Label1" runat="server" Text="First
Name"></asp:Label>
<asp:TextBox ID="TextBoxFirstName"
runat="server"></asp:TextBox><br />
<asp:Label ID="Label2" runat="server" Text="Last
Name"></asp:Label>
<asp:TextBox ID="TextBoxLastName"
runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Update" /></div>
</form>
</body>
</html>
As you can see, we haven't put the SqlDataSource control in the markup. We have just placed TextBoxes, Labels and a Button control to get the data from the user and update it using a SqlDataSource control that we create programmatically. The code takes place in the click event's handler for the Button1 control. The following is the complete code for the Default.aspx.cs file
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using System.Web.Configuration;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
EmpIdLabel.Text += Request.QueryString["EmpID"];
TextBoxFirstName.Text = Request.QueryString["FirstName"];
TextBoxLastName.Text = Request.QueryString["LastName"];
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlDataSource sqlDataSource1 = new SqlDataSource();
this.Controls.Add(sqlDataSource1);
sqlDataSource1.ConnectionString =
WebConfigurationManager.ConnectionStrings
["NorthwindConnection"].ConnectionString;
sqlDataSource1.UpdateCommand = "UPDATE Employees SET LastName =
@LastName, FirstName = @FirstName " +
"WHERE EmployeeID = @EmployeeID";
QueryStringParameter qsParameter = new QueryStringParameter();
qsParameter.Name = "EmployeeID";
qsParameter.QueryStringField = "EmpID";
qsParameter.Type = TypeCode.String;
sqlDataSource1.UpdateParameters.Add(qsParameter);
ControlParameter parameter = new ControlParameter();
parameter.Name = "FirstName";
parameter.ControlID = "TextBoxFirstName";
parameter.PropertyName = "Text";
parameter.Type = TypeCode.String;
sqlDataSource1.UpdateParameters.Add(parameter);
parameter = new ControlParameter();
parameter.Name = "LastName";
parameter.ControlID = "TextBoxLastName";
parameter.PropertyName = "Text";
parameter.Type = TypeCode.String;
sqlDataSource1.UpdateParameters.Add(parameter);
sqlDataSource1.Update();
Response.Redirect("Default.aspx");
}
}
Next: Testing the Example >>
More ASP.NET Articles
More By Michael Youssef