Passing Objects as Parameters : ObjectDataSource « ADO.net Database « ASP.Net






Passing Objects as Parameters


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<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>

 








Related examples in the same category

1.Binding to a LINQ to SQL Query
2.Binding to a Web Service
3.Using Parameters with the ObjectDataSource Control
4.Using Different Parameter Types
5.Filtering Data
6.Handling ObjectDataSource Control Events
7.Handling Method Errors
8.Handling the Object Creating Event
9.Concurrency and the ObjectDataSource Control, ConflictDetection: CompareAllValues / OverwriteChanges
10.Creating a Custom ObjectDataSource Control
11.Creating Custom Parameter Objects
12.Creating a Page Property Parameter
13.Define your own collection for ObjectDataSource
14.objectdatasource with control parameter
15.GridView with ObjectDataSource
16.ObjectDataSource with selectmethod, deletemethod, updatemethod, insertmethod
17.ObjectDataSource based on XML
18.ObjectDataSource and backend database