About this Tutorial –
Objectives –
Delegates will learn to develop applications using C #4.0 After completing this course, delegates will be able to:
- Use Visual Studio 2010 effectively
- Create Two Tier Business Web Application with ADO.Net
Audience
This course has been designed primarily for programmers new to the .Net development platform. Delegates experience solely in Windows application development or earlier versions of ASP.Net will also find the content beneficial.
Prerequisites
Before attending this workshop, students must:
- Be able to manage a solution environment using the Visual Studio 2010 IDE and tools
- Be able to program an application using a .NET Framework 4.0 compliant language
Contents
Copyright 20/12/12 – David Ringsell
Quick Access
Exercise 1
Create a Customer Class that reads details from the Northwind database
In this exercise, you will create a customer class in a web application. The class will contain a method that reads the customers table.
- Create an empty Web Application project called Business Application.
- Add a class file called Customers.cs to the project.
- Import the these namespaces in to the class:
using System.Data.SqlClient;
using System.Data; - Declare at class level these ADO.Net objects:
private SqlConnection conn;
private SqlDataAdapter da;
private DataSet ds; - Add a GetCustomers() method that returns a DataSet to the class.
- Add code to GetCustomers() that reads data from the Customers table and copies this to the DataSet: – change the name of DataSource to your instance of SQL Server.
public DataSet GetCustomers()
{
conn = new SqlConnection(@"DataSource=.; Initial
Catalog=Northwind;Integrated Security=True");
string queryString
="SELECT CustomerID, CompanyName FROM dbo.Customers";
da = new SqlDataAdapter(queryString, conn);
ds = new DataSet();
da.Fill(ds, "Customers");
return ds;
} - View code file.
Exercise 2
Show Customer Data on a Web Form
In this exercise, you will add a web form that shows customer data. The form will use the customer class to access the database.
- Add a web form called Default.aspx to the project. Set this as the start page.
- Add a GridView control to Default.aspx called gvCustomers.
- In the form’s Page_Load event handler create a Customers object & call the GetCustomers method.
Customers custObject = new Customers();
DataSet ds = custObject.GetCustomers(); - Bind the GridView to the DataSet returned from the GetCustomers method.
gvCustomers.DataSource = ds.Tables["Customers"];
gvCustomers.DataBind(); - View code file.
- Run the application to test if customer data is displayed.
- Enhance the application, for example:
- Allow customer data to be updated.
- Move the Customer class to a separate project
- Improve the UI
If you would like to see more content like this in the future, please fill-in our quick survey.