CSharp Passing Objects As Parameters – ADO.Net Database

File: EmployeeData.csusing 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<companyEmployee> GetEmployees()
    {
        List<companyEmployee> employees = new List<companyEmployee>();        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<configuration>
  <connectionStrings>
    <add name="Employees"
         connectionString="Data Source=.\SQLEXPRESS;
         AttachDbFilename=|DataDirectory|MyDatabase.mdf;Integrated Security=True;User Instance=True" />
  </connectionStrings>
</configuration>File: Default.aspx<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Update Employees</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    <asp:DetailsView ID="DetailsView1"
        DataSourceID="srcEmployees"
        DataKeyNames="Id"
        AutoGenerateRows="True"
        AutoGenerateEditButton="True"
        AllowPaging="true"
        Runat="server" />
    <asp:ObjectDataSource
        id="srcEmployees"
        TypeName="EmployeeData"
        DataObjectTypeName="CompanyEmployee"
        SelectMethod="GetEmployees"
        UpdateMethod="UpdateEmployee"
        Runat="server" />    </div>
    </form>
</body>
</html>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.