XmlSerializer : Entity Object to DataSet



Below code is used to convert Entity Object to DataSet.

  1. Create Book.cls
  2.            Serializing using XmlSerializer

  1. Create Book.cls

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

    public class Book
    {
            public string FirstName;
            public string MI;
            public string LastName;
    }
 2. Create console application for Serializing using XmlSerializer .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Data;

class Program
{
      Book objbooks;
      static void Main(string[] args)
        {
            fnserialise();
        }

        static void fnserialise()
        {
            Book objbooks = new Book();
            objbooks.FirstName = "Ram";
            objbooks.LastName = "Kumar";
            objbooks.MI = "A";
            XmlSerializer serializer;
            MemoryStream stream;
            serializer = new XmlSerializer(objbooks.GetType());
            stream = new MemoryStream();
            serializer.Serialize(stream, objbooks);
            DataSet BookData = new DataSet("Books");
             stream.Position = 0;
            BookData.ReadXml(stream);
            stream.Close();

        }

ASP.NET :DataList to Word

 

Below code is used to export row from DataList to Word.
 
private void ExportToWord(DataList DataList1)
        {

            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=mydoc.doc");
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            Response.ContentType = "application/vnd.word";
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            //stringWrite.ToString(       
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

            DataList1.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.End();

        }

GridView: Export GridView Rows To Excel .



Below code is used to export row from Gridview to Excel.

private void ExportToExcel(GridView gv)
        {
            Response.Clear();

            Response.AddHeader("content-disposition", "attachment; filename=NewExcel.xls");

            Response.Charset = "";

            Response.ContentType = "application/vnd.xls";

            System.IO.StringWriter stringWrite = new System.IO.StringWriter();

            System.Web.UI.HtmlTextWriter htmlWrite =new HtmlTextWriter(stringWrite);

            gv.RenderControl(htmlWrite);

            Response.Write(stringWrite.ToString());

            Response.End();

        }

GridView : Export Selected GridView Rows To Excel .(DataTable to Excel)


Below code is used for export selected row from GridView to Excel .Here you can find code for DataTable to Excel.
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    DataTable dt = new DataTable();
    DataTable dt1 = new DataTable();
    DataRow dr;
    DataRow dr1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindGrid();
        }
    }

    void BindGrid()
    {
        dt.Columns.Add("eno");
        dt.Columns.Add("empname");
        dt.Columns.Add("sal");
        dr = dt.NewRow();
        dr["eno"] = "101";
        dr["empname"] = "Ravindran";
        dr["sal"] = "45000";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr["eno"] = "102";
        dr["empname"] = "James";
        dr["sal"] = "35000";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr["eno"] = "103";
        dr["empname"] = "Mike";
        dr["sal"] = "20000";
        dt.Rows.Add(dr);

        if (dt.Rows.Count > 0)
        {
            Session["source_table"] = dt;
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
    //Export all records to excel
    protected void Button1_Click(object sender, EventArgs e)
    {
        //here session value is stored before bind data on gridview
        DataTable dt = Session["source_table"] as DataTable;
        ExportExcel(dt);
    }
    void ExportExcel(DataTable dt)
    {
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment; filename=" + DateTime.Now.ToString("ddMMyyyy") + ".xls");
        Response.ContentType = "application/ms-excel";
        string tab = "";
        foreach (DataColumn dc in dt.Columns)
        {
            Response.Write(tab + dc.ColumnName);
            tab = "\t";
        }
        Response.Write("\n");

        int i;
        foreach (DataRow dr in dt.Rows)
        {
            tab = "";
            for (i = 0; i < dt.Columns.Count; i++)
            {
                Response.Write(tab + dr[i].ToString());
                tab = "\t";
            }
            Response.Write("\n");
        }
        Response.End();

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if ((e.Row.RowType == DataControlRowType.Header))
        {
            ((CheckBox)e.Row.FindControl("SelectAll")).Attributes.Add("onclick", "javascript:SelectAll('" + ((CheckBox)e.Row.FindControl("SelectAll")).ClientID + "')");
        }
    }
    //Export only selected record
    protected void Button2_Click(object sender, EventArgs e)
    {
        int k = 0;
        //Checkther whether atleast one check box is selected or not
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            GridViewRow row = GridView1.Rows[i];
            CheckBox Ckbox = (CheckBox)row.FindControl("CheckBox1");
            if (Ckbox.Checked == true)
            {
                k++;
            }
        }

        if (k == 0)
        {
            Page.RegisterStartupScript("Alert Message", "");
            return;
        }

        dt1.Columns.Add("eno");
        dt1.Columns.Add("empname");
        dt1.Columns.Add("sal");

        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {

            GridViewRow row = GridView1.Rows[i];
            CheckBox Ckbox = (CheckBox)row.FindControl("CheckBox1");
            if (Ckbox.Checked == true)
            {
                dr1 = dt1.NewRow();
                dr1["eno"] = GridView1.Rows[i].Cells[1].Text;
                dr1["empname"] = GridView1.Rows[i].Cells[2].Text;
                dr1["sal"] = GridView1.Rows[i].Cells[3].Text;
                dt1.Rows.Add(dr1);
            }
        }
        ExportExcel(dt1);
    }
}


Using XmlSerializer : Enity object to Dataset in C#

Below Program is example for converting Entity Object into Dataset using Xmlserilizer.

Step 1 :Create a class called Book.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class Book
{
            public string FirstName;
            public string MI;
            public string LastName;
}


Step 2: Create Console application.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Data;

class Program
{
    Book objbooks;
    static void Main(string[] args)
    {
            fnserialise();
    }

    static void fnserialise()
    {

            Book objbooks = new Book();
             objbooks.FirstName = "Ram";
            objbooks.LastName = "Kumar";
            objbooks.MI = "A";
 

            XmlSerializer serializer;
            MemoryStream stream;
            serializer = new XmlSerializer(objbooks.GetType());
            stream = new MemoryStream();
            serializer.Serialize(stream, objbooks);
            DataSet BookData = new DataSet("Books");
            stream.Position = 0;
              BookData.ReadXml(stream);
              stream.Close();
        }
   }

How to write macro in Excel

Please  follow below steps to write Macro.

Step 1: Create a new Excel Application
Step 2: Press Alt + F11,it will open code behind window.


Step 3: By right clicking -ThisWorkbook- select Insert -> Module


Step 4: Write Hello world Program.

Sub Helloworld()
MsgBox "Hello World!!!"
End Sub

Step 5: Press F5.Msgbox will shown as   "Hello World!!!"




How to access worksheet in excel using VBA

By declaring variable as Worksheet, we can access worksheet and cell in excel.
Dim sht1 As Worksheet
Set sht1 = Worksheets(1)
sht1.Cells(1,1) = "Hello World"