File: EmployeeData.cs
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Web.Configuration;
public class EmployeeData
{
string _connectionString;
public void UpdateEmployee(CompanyEmployee employeeToUpdate)
{
SqlConnection con = new SqlConnection(_connectionString);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "UPDATE Employees SET FirstName=@FirstName," +
"LastName=@LastName,Phone=@Phone WHERE Id=@Id";
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Id", employeeToUpdate.Id);
cmd.Parameters.AddWithValue("@FirstName", employeeToUpdate.FirstName);
cmd.Parameters.AddWithValue("@LastName", employeeToUpdate.LastName);
cmd.Parameters.AddWithValue("@Phone", employeeToUpdate.Phone);
using (con)
{
con.Open();
cmd.ExecuteNonQuery();
}
} public List GetEmployees()
{
List employees = new List();
SqlConnection con = new SqlConnection(_connectionString);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT Id,FirstName,LastName,Phone FROM Employees";
cmd.Connection = con;
using (con)
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
CompanyEmployee newEmployee = new CompanyEmployee();
newEmployee.Id = (int)reader["Id"];
newEmployee.FirstName = (string)reader["FirstName"];
newEmployee.LastName = (string)reader["LastName"];
newEmployee.Phone = (string)reader["Phone"];
employees.Add(newEmployee);
}
}
return employees;
}
public EmployeeData()
{
_connectionString = WebConfigurationManager.ConnectionStrings["Employees"]. ConnectionString;
}
}
public class CompanyEmployee
{
private int _id;
private string _firstName;
private string _lastName;
private string _phone;
public int Id
{
get { return _id; }
set { _id = value; }
}
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
public string Phone
{
get { return _phone; }
set { _phone = value; }
}
}
File: Web.config
connectionString="Data Source=.\SQLEXPRESS;
AttachDbFilename=|DataDirectory|MyDatabase.mdf;Integrated Security=True;User Instance=True" />
File: Default.aspx
<%@ Page Language="C#" %>
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
Update Employees