Background

There is often a need in a project's reporting module to export a GridView to Access. So by considering that requirement I decided to write this article especially focusing on beginners and those who want to learn how to export a GridView to Access Using ASP.NET C# and using the itextsharp DLL.

Prerequisites
  • To export the Grid view, we need to use a reference of itextsharp.dll.
  • Download the itextsharp.dll from the internet.

The itextsharp.dll is the free DLL available for download that provides some methods to export a Grid view into an Access file. After adding the reference, use the following reference of itextsharp.dll:

  1. using iTextSharp.text;
  2. using iTextSharp.text.pdf;
  3. using iTextSharp.text.html.simpleparser;
Now before creating the application, let us create a table named employee in a database from where we show the records in a Grid view, the table has the following fields (shown in the following image):
I hope you have created the same type of table.
Now create the project as:
  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
  2. "File" - "New Project" - "C#" - "Empty Project" (to avoid adding a master page).
  3. Provide the Project name such as ExportGridToAccess or another as you wish and specify the location.
  4. Then right-click on Solution Explorer - "Add New Item" - Default.aspx page.
  5. One Button, one label, and a gridview.

Now let us create a function to bind the records to the Grid view from the database. If you are a beginner and don't know in detail how to bind a Grid view from a database then refer to my following article.

Now, for this article create the following function in the default.aspx.cs page to bind the Grid view:
  1. private void Bindgrid()
  2. {
  3. connection();
  4. query = "select *from Employee";//not recommended this i have written just for example,write stored procedure for security
  5. com = new SqlCommand(query, con);
  6. SqlDataReader dr = com.ExecuteReader();
  7. GridView1.DataSource = dr;
  8. GridView1.DataBind();
  9. con.Close();
  10. }
Now, call the above function on page load as:
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (!IsPostBack)
  4. {
  5. Bindgrid();
  6. }
  7. }
Now run the application and then we can see the following records in the Grid view:
We now have records to export into Access. Let us start coding for our actual requirements. Add the VerifyRenderingInServerForm event after the page load that is required when exporting a Grid view to Excel, Word and PDF to avoid the run time error "GridView' must be placed inside a form tag with runat=server."
  1. public override void VerifyRenderingInServerForm(Control control)
  2. {
  3. //required to avoid the runtime error "
  4. //Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server."
  5. }
Now before creating the function to Export the grid view to Access add the reference of itextsharp.dll by right-clicking the Solution Explorer. After adding the reference the Solution Explorer will look as in the following:
Now create the following function to export the Grid view to Access:
  1. private void ExportGridToAccess()
  2. {
  3. Response.ContentType = "application/ms-access";
  4. Response.AddHeader("content-disposition", "attachment; filename=Vithal_Wadje.mdb");
  5. Response.Cache.SetCacheability(HttpCacheability.NoCache);
  6. StringWriter sw = new StringWriter();
  7. HtmlTextWriter hw = new HtmlTextWriter(sw);
  8. GridView1.RenderControl(hw);
  9. StringReader sr = new StringReader(sw.ToString());
  10. Document AccessDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
  11. HTMLWorker htmlparser = new HTMLWorker(AccessDoc);
  12. PdfWriter.GetInstance(AccessDoc, Response.OutputStream);
  13. AccessDoc.Open();
  14. htmlparser.Parse(sr);
  15. AccessDoc.Close();
  16. Response.Write(AccessDoc);
  17. Response.End();
  18. GridView1.AllowPaging = true;
  19. GridView1.DataBind();
  20. }
Now double-click on the Export button and call the preceding function on "onclick" as in the following:
  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3. ExportGridToAccess();
  4. }
Now the entire code of the Default.aspx.cs page will be as follows:
  1. using System;
  2. using System.Web.UI;
  3. using System.Configuration;
  4. using System.Data.SqlClient;
  5. using System.IO;
  6. using System.Web;
  7. using iTextSharp.text;
  8. using iTextSharp.text.pdf;
  9. using iTextSharp.text.html.simpleparser;
  10. public partial class _Default : System.Web.UI.Page
  11. {
  12. private SqlConnection con;
  13. private SqlCommand com;
  14. private string constr, query;
  15. private void connection()
  16. {
  17. constr = ConfigurationManager.ConnectionStrings["getconn"].ToString();
  18. con = new SqlConnection(constr);
  19. con.Open();
  20. }
  21. protected void Page_Load(object sender, EventArgs e)
  22. {
  23. if (!IsPostBack)
  24. {
  25. Bindgrid();
  26. }
  27. }
  28. public override void VerifyRenderingInServerForm(Control control)
  29. {
  30. //required to avoid the runtime error "
  31. //Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server."
  32. }
  33. private void Bindgrid()
  34. {
  35. connection();
  36. query = "select *from Employee";//not recommended this i have written just for example,write stored procedure for security
  37. com = new SqlCommand(query, con);
  38. SqlDataReader dr = com.ExecuteReader();
  39. GridView1.DataSource = dr;
  40. GridView1.DataBind();
  41. con.Close();
  42. }
  43. protected void Button1_Click(object sender, EventArgs e)
  44. {
  45. ExportGridToAccess();
  46. }
  47. private void ExportGridToAccess()
  48. {
  49. Response.ContentType = "application/ms-access";
  50. Response.AddHeader("content-disposition", "attachment; filename=Vithal_Wadje.mdb");
  51. Response.Cache.SetCacheability(HttpCacheability.NoCache);
  52. StringWriter sw = new StringWriter();
  53. HtmlTextWriter hw = new HtmlTextWriter(sw);
  54. GridView1.RenderControl(hw);
  55. StringReader sr = new StringReader(sw.ToString());
  56. Document AccessDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
  57. HTMLWorker htmlparser = new HTMLWorker(AccessDoc);
  58. PdfWriter.GetInstance(AccessDoc, Response.OutputStream);
  59. AccessDoc.Open();
  60. htmlparser.Parse(sr);
  61. AccessDoc.Close();
  62. Response.Write(AccessDoc);
  63. Response.End();
  64. GridView1.AllowPaging = true;
  65. GridView1.DataBind();
  66. }
  67. }
Now run the application and click on the Export Button. The following popup is shown:
Now click and save the preceding file and open it with Excel by establishing the connection or you can open this file with the tool mdb file viewer.
Notes
  • Download the Zip file from the attachment for the full source code of the application.
  • Change the connection string in the web.config file to specify your server location.

Summary

I hope this article is useful for all readers, if you have any suggestion then please contact me including beginners also.