How to Serialize the Data in C#

In this article I will explain you how to serialize the data. Suppose sometime we need to delete the record and again re-serialize the data ,so this program re-serialize the data.


usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Data.SqlClient;
namespaceDataSerialization
{
    public partial class Form1 : Form
    {
        //Connection String
        string con = “Data Source=(local);Initial Catalog=Northwind;Integrated Security=True”;
        DataSet ds = new DataSet();
       
        public Form1()
        {
            InitializeComponent();
        }
        private voidForm1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add(“FirstName”);
            comboBox1.Items.Add(“LastName”);
            SqlConnection cnn = newSqlConnection(con);
            DataG();
            dataGridView1.DataSource = ds;
            ds.Dispose();
            dataGridView1.DataMember = “Employees”;
       
        }
        private voidbutton1_Click(object sender, EventArgs e)
        {
            string sql = “Select idx,Employeeid,FirstName,LastName from Employees order by “ + comboBox1.Text  + “”;
            using (SqlConnectioncnn = new SqlConnection(con))
            {
                using (SqlCommandcmd = new SqlCommand(sql, cnn))
                {
                    cnn.Open();
                    SqlDataReader rd;
                    rd = cmd.ExecuteReader();
                    int k = 1;
                    while (rd.Read())
                    {
                     intid = Convert.ToInt32(rd[“idx”].ToString());
                        using (SqlConnectioncnn1 = new SqlConnection(con))
                        {
                            cnn1.Open();
                            string str = “Update Employees set Employeeid=” + k + ”  where idx=” + id;
                            SqlCommand qcmd = new SqlCommand(str, cnn1);
                            qcmd.ExecuteNonQuery();
                            qcmd.Dispose();
                            cnn1.Close();
                        }   
                       
                        k = k + 1;
                    }
                    cnn.Close();
                    MessageBox.Show(“Successfully”);
                }
            }
              
            
        }
     private void DataG()
    {
    string sql = “Select Employeeid,FirstName,LastName from Employees order by Employeeid “;
        SqlConnection cnn = newSqlConnection(con);
        SqlDataAdapter sd = newSqlDataAdapter(sql, cnn);
      
        cnn.Open();
       
        sd.Fill(ds, “Employees”);
        cnn.Close();
        
        }
    }
}


Leave a Reply

Your email address will not be published. Required fields are marked *