Sunday, 3 January 2016

Upload data from CSV In Gridview





Concept

We need to upload the data from CSV File in Gridview in Asp.Net

Steps:

1)Drag Gridview control on a page
2)Include some namespace



Gridview export to CSV





Introduction
We are going to export GridView data to a .CSV file.

In Asp.Net
Drag and Drop a Gridview control and Linkbutton control to a web page (default.aspx). In the code behind, write the code for connecting database in the page_load event and in the LinkButton click event write the code for export to .CSV file.

protected void lnkbExportToexcel_Click(object sender, EventArgs e)
    {
        DateTime dt = new DateTime();
        dt = DateTime.Now;
        Response.ClearContent();
        string strFileName = dt.Day + "/" + dt.Month + "/" + dt.Year + "_Report.xls";

        Response.AddHeader("content-disposition", "attachment; filename=" + strFileName);
        Response.ContentType = "application/excel";
        System.IO.StringWriter sw = new System.IO.StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        gvTable.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }
public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }

 

I hope that the functionality of the above attribute is clear to you now. Now you can use this code in your application If you have further question, just drop a line below and I will try to answer you as soon as possible.
Last but not least, connect with me on Twitter , Facebook , LinkedIn and Google+ for technical updates and articles news. We won’t spam or share your email address as we respect your privacy.