Table of Contents
Scenario In these labs, you will create a Web application to manage a list of products as
part of a Sales Order System. You will also learn how to create and deploy this
Web application. To sign into the Sales Order System, you need to enter the
User ID and password in the login page. You will first create the login page,
which is an ASP.NET page (Web form) using ASP .NET server controls.
After successfully logging into the Sales Order System, the user can view
details of existing products and modify the same. Finally you will deploy this
application to a web server.
The working solution and associated files for these labs are located at
C:\Microsoft Hands-On-Lab\DEV-HOL03\Solution.
This lab is the second part of two. It uses files created in part one. It is strongly
recommended that the two lab sections be done in order.
Estimated time to
complete this lab: 45
minutes
4 Creating ASP.NET Web Applications with C# - Part 2
Exercise 1
State Management
Scenario
In this exercise, you will use both Client-side and Server-side state management options to preserve
the values of the controls between round trips.
1. Use the ViewState object to a. Select Start | All Programs | Visual Studio .NET 2003 | Visual
preserve the Password Studio .NET 2003.
value in the client side. b. Open the base web application for modification by selecting File |
As Web applications are Open | Project.
stateless between server round c. Navigate to C:\Microsoft Hands-on-Lab\DEV-HOL03\Starter
trips the values of a page's Solution\Lab3-CS-Starter-2.
variables and controls are not
preserved. We need to preserve d. Select Lab3-CS-Starter-2.sln and click Open.
these values and they can be e. To view Login.aspx code, in the Solution Explorer, right-click
stored in the client or server. To Login.aspx and select View Code.
store in client, ViewState is f. To preserve value of the Password field using the ASP.NET
used. ViewState object, add the following code at the end of the
btnSignin_Click method.
private void btnSignin_Click(object sender,
System.EventArgs e)
{
.......
.......
.......
//Debugging code
Debug.WriteLine(txtUserID.Text);
Debug.WriteLine(txtPassword.Text);
Debug.Write(lblResult.Text);
l. Click Restore
You can see The password you typed is mypassword in lblResult.
m. Close the Web browser.
2. Use the Session object to a. Select View | Code or press F7.
preserve the User ID value b. To preserve the value of the User ID field on the server side using the
in the server side. ASP.NET session, add the following code in btnSignin_Click method:
private void btnSignin_Click(object sender,
System.EventArgs e)
{
//Add user authentication logic here
if (txtUserID.Text.Length >= 4 &&
txtPassword.Text.Length >= 4 &&
txtUserID.Text.StartsWith("J"))
{
lblResult.Text= "Valid user!";
Exercise 2
Data Access in Web Forms Pages
Scenario
This exercise explains how to use data access in Web form and ASP.NET DataGrid control to bind
to the results of SQL queries. You will also learn to use ADO.NET objects, bind data to DataGrid
and customize the data display.
1. Use SqlConnection class to a. Add the following code at the top in the LoginDemo page, which
create a connection to allows you to use SqlConnection class:
Northwind database. using System.Data.SqlClient;
b. Add a class-level variable of type SqlConnection as shown below:
public class LoginDemo : System.Web.UI.Page
{
private SqlConnection myConnection;
............
............
c. Add the following code in Page_Load method, which creates a
SqlConnection object to connect to the local SQL server’s Northwind
database (Snippet 3).
private void Page_Load(object sender,
System.EventArgs e)
{
// Put user code to initialize the page here
// Create a connection to the Northwind database
myConnection = new
SqlConnection("server=localhost;database=northwind
;Trusted_Connection=yes");
............
............
2. Fetch data from a Products a. Click View | Designer or press SHIFT+F7.
table and how to display it b. Select View | Toolbox or press CTRL+ALT+X.
in a DataGrid.
c. Select DataGrid control in Web Forms tab of Toolbox.
d. Drag and drop it on to the Design view of LoginDemo.apx page.
e. Set the following properties for the DataGrid. (To view the Properties
window of a control, click the control and select View | Properties
Window or press F4).
ID - dgProducts
BackColor - LemonChiffon
BorderColor - Black
BorderStyle - Solid
BorderWidth - 2px
DataKeyField - ProductID
Font (Expand the Font node)
Name - Arial
8 Creating ASP.NET Web Applications with C# - Part 2
Size - X-Small
HeaderStyle (Expand the HeaderStyle node)
BackColor - Khaki
Font (Expand the Font node)
Name - Arial
Size - X-Small
Width - 100%
f. Select View | Code or press F7.
g. To get data from the Products table and bind it to DataGrid, add the
following method after the Page_Load event handler: (Snippet 4)
private void Page_Load(object sender,
System.EventArgs e)
{
...
}
public void BindGrid(string sortfield)
{
//Create DataAdapter to fetch data from Prodcuts
table
SqlDataAdapter myCommand = new
SqlDataAdapter("select * from Products",
myConnection);
("server=(local);database=Northwind;User
ID=sa;Password=;");
if (!Page.IsPostBack)
{
// Server side State management using
Session
lblUserID.Text=lblUserID.Text+" "+
Session["UserID"];
BindGrid("ProductID");
}
}
i. Select Debug | Start or press F5.
j. Enter John in User ID and mypassword in Password.
Creating ASP.NET Web Applications with C# - Part 2 9
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton"
UpdateText="Update" CancelText="Cancel"
EditText="Edit"><ItemStyle
Wrap="False"></ItemStyle>
</asp:EditCommandColumn>
</Columns>
</asp:Datagrid>
d. Click the Design tab (at the bottom of the page).
e. Select Debug | Start or press F5.
f. Enter John in User ID and mypassword in Password.
g. Click Sign In.
You are redirected to another page LoginDemo.aspx, where the Products
data is displayed in the DataGrid with new column Edit.
h. Close the Web browser.
To display data updated message to the user, add a Label control:
i. Select View | Designer or press SHIFT+F7.
j. Select View |Toolbox or press CTRL+ALT+X.
k. Click Web Forms tab to see Web form controls.
l. Select Label control in Web Forms tab of Toolbox and drag it above
Creating ASP.NET Web Applications with C# - Part 2 11
myCommand.Connection.Open();
try
{
myCommand.ExecuteNonQuery();
lblMessage.Text= lblMessage.Text=
"<b>Product " +
dgProducts.DataKeys[(int)e.Item.ItemIndex]
+" is updated</b><br>" ;
dgProducts.EditItemIndex = -1;
}
catch (Exception exc)
{
lblMessage.Text = "ERROR: "+ exc.Message;
}
myCommand.Connection.Close();
BindGrid("ProductID");
}
y. Select Debug | Start or press F5.
z. Enter John in User ID and mypassword in Password.
aa. Click Sign In.
You are redirected to another page LoginDemo.aspx, where the Products
data is displayed in the DataGrid.
bb. Click Edit link in any row in the DataGrid.
Your DataGrid is in editable format
You can see that the row is in edit mode, where you can update the details
of a ProductID.
cc. Change UnitInStock value to a positive integer value.
dd. Click Update link of that row.
You get a message displaying Product <xxx> is updated in the lblMessage
control.
ee. Close the Web browser.
Creating ASP.NET Web Applications with C# - Part 2 13
Exercise 3
Deploying ASP.NET Web Application
Scenario
You can use XCOPY or FTP to deploy an ASP.NET application on to a server. You also can use
Microsoft Visual Studio .NET 2003 IDE to deploy a Web Application.
In this exercise, you will learn how to deploy Web application from within the Microsoft Visual
Studio .NET 2003 IDE.