• home
  • Email: trainings@synapseindia.careers



Serialization in C#

Jul 11, 2016

Serialization in C# allows developers to convert an object into a stream of bytes so that it can be stored in database or file or can be sent to remote applications. In the below example user is submitting the information through the input fields. The data received through input fields are then set into properties accessed by various instances of class Employee which is decorated by attribute [Serializable]. Employee's instances are stored into generic list which then added into ViewState. It is very understood that ViewState data flows from server to client due to which the instances stored into Lists are to be serialized. See the below example

Below is the code for Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<form id="form1" runat="server">
<h1>Employee Details</h1>
<hr />
<asp:Label ID="Label1" runat="server" Text="EmployeeID"></asp:Label><br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br /><br />

<asp:Label ID="Label2" runat="server" Text="EmployeeName"></asp:Label><br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <br /><br />

<asp:Label ID="Label3" runat="server" Text="EmployeeMobile"></asp:Label><br />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> <br /><br />

<asp:Label ID="Label4" runat="server" Text="EmployeeCity"></asp:Label><br />
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox> <br /><br />

<asp:Label ID="Label5" runat="server" Text="EmployeeDOB"></asp:Label><br />
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox> <br /><br />

<asp:Button ID="Button1" runat="server" Text="Add" OnClick="Button1_Click" /> <br /><hr />
<h3>Employee Details Display</h3>

<asp:GridView ID="GridView1" runat="server"></asp:GridView>

Below is the code for Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
    protected void Button1_Click(object sender, EventArgs e)
        Employee emp = new Employee() 
                EmployeeID=int.Parse(TextBox1.Text) ,
                EmployeeName = TextBox2.Text,
                EmployeeMobile = TextBox3.Text,
                EmployeeCity = TextBox4.Text,
                EmployeeDOB = DateTime.Parse(TextBox5.Text)

        if (ViewState["Emp"] == null)
            ViewState["Emp"] = new List();

        List employee = ViewState["Emp"] as List;

        GridView1.DataSource = employee;

        ViewState["Emp"] = employee;

Below is the code for Employee.cs :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class Employee
    public int EmployeeID { get; set; }
    public string EmployeeName { get; set; }
    public string EmployeeMobile { get; set; }
    public string EmployeeCity { get; set; }
    public DateTime EmployeeDOB { get; set; }
GridView is used to display the details of all Employee data retrieved from List. GridView is bound to List instance which automatically fills itself from the data taken from List.

Leave a Reply

Your email address will not be published.