Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 28th, 2006, 06:58 AM
Registered User
 
Join Date: Jan 2006
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem in exporting data in a gridview to MSExcel

Hi,

I am trying to export data from a GridView to a Microsoft Excel file.

But, I got an error message as below:
"Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server" , though the control tag is already inside the form tag with runat=server.

Please advise.

Thanks & Regards,
Rashmi
Reply With Quote
  #2 (permalink)  
Old January 28th, 2006, 07:36 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Rashmi,

Can you show us the code for the page and/or Code Behind? Without code, it's hard to see what's going on...


Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #3 (permalink)  
Old January 28th, 2006, 07:58 AM
Registered User
 
Join Date: Jan 2006
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar,

I used the following code:

Response.AddHeader("contentdisposition","attachmen t;filename=test1.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWrite(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();


But RenderControl doen't seem to work with GridView.

Thanks,
Rashmi
Reply With Quote
  #4 (permalink)  
Old January 28th, 2006, 08:06 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Rashmi,

Is GridView a control on your page? If so, this won't work. What you need to do is create an entirely new GridView control on the fly, databind it and then render it. E.g. (untested):
Code:
Response.AddHeader("contentdisposition","attachment;filename=test1.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWrite(stringWrite);
GridView GridView1 = new GridView();
GridView1.DataSource = BusinessLayer.GetDataForControl();
GridView1.DataBind();

GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
This assumes that you let the GridView determine the columns (e.g. AutoGenerateColumns is true). If you need more control, you can add columns / templates/ styles to your GridView programmatically. Let me know if you need more information on that and I'll try to dig up an example.

Clearly, I just made up the BusinessLayer.GetDataForControl() method but I hope you get the idea....
Reply With Quote
  #5 (permalink)  
Old March 28th, 2006, 04:49 PM
Registered User
 
Join Date: Mar 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mtscreator Send a message via Yahoo to mtscreator
Default

Hi,
This error can happen if you have paging or sorting turned on. What you do before exporting the datagrid to excel is to turn off paging and sorting, export your data, then turn your paging and sorting back on.

Reply With Quote
  #6 (permalink)  
Old April 26th, 2006, 12:49 PM
Registered User
 
Join Date: Apr 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to birch9397 Send a message via MSN to birch9397
Default

Quote:
quote:Originally posted by mtscreator
 Hi,
This error can happen if you have paging or sorting turned on. What you do before exporting the datagrid to excel is to turn off paging and sorting, export your data, then turn your paging and sorting back on.

Reply With Quote
  #7 (permalink)  
Old November 13th, 2006, 08:50 AM
Friend of Wrox
 
Join Date: Aug 2006
Location: Amman, , Jordan.
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Default

guys i generated the gridview dynamically, but what happened is that the outcome was an empty excel sheet,
any suggestions?


Reply With Quote
  #8 (permalink)  
Old February 1st, 2007, 01:42 PM
Registered User
 
Join Date: Feb 2007
Location: Golden, CO, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is the technique I use that have worked with me. I use master page as well. So hope this helps. You will see I added couple other things. This is base on ASP.NET 2.0 page. This is obviously a combination of different source on the internet and Imar's suggestion above creating new control.

1) On your ASPX add to Page Directive EnableEventValidation="false" Only use if the page complains about RenderControl must be in Form tag.

2) On the code behind of ASPX page as the following constructor:

public override void VerifyRenderingInServerForm(Control control)
    {
        // Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time.
    }
3) On the code behind of your grid (either aspx or if load on user control put there) This works if Session is not controled in SQL DB.

//First Step
//Declare Grid control after public partial class

public GridView GridView1 = new GridView();


//Second Step Method for button onclick

protected void btnExport_Click(object sender, EventArgs e) {
        Response.Clear();

        Response.AddHeader("content-disposition", "attachment; filename=FileName_"+DateTime.Now.ToShortDateString ().Replace("/","_")+".xls");

        Response.Charset = "";
        // If you want the option to open the Excel file without saving than
        // comment out the line below
        // Response.Cache.SetCacheability(HttpCacheability.No Cache);
        Response.ContentType = "application/vnd.ms-xls";
        //"application/vnd.ms-excel"
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

        //Reference the originally renders grid id name and your data source
        Session["ExportGridviewColumns"] = this.gvList.Columns;
        Session["ExportGridViewDataSource"] = this.objDsList.Select();

        //Add the columns to the print gridview
        foreach (DataControlField dcf in (DataControlFieldCollection)Session["ExportGridviewColumns"])
        {
            this.GridView1.Columns.Add(dcf);
        }

        //Turning off paging will avoid errors and get all contents
        //Provide some basic attributes to make excel data pretty

        GridView1.AllowPaging = false;
        GridView1.AllowSorting = false;
        GridView1.Font.Name = "Verdana";
        GridView1.Font.Size = 8;

        //Using ObjectDataSource Grabs Entire Contents
        this.GridView1.DataSource = Session["ExportGridViewDataSource"];
        this.GridView1.DataBind();

        GridView1.RenderControl(htmlWrite);

        Response.Write(stringWrite.ToString());

        Response.End(); }
Reply With Quote
  #9 (permalink)  
Old February 1st, 2007, 01:47 PM
Registered User
 
Join Date: Feb 2007
Location: Golden, CO, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Imar for the suggestion. Helped me out.

http://www.advernets.com
Reply With Quote
  #10 (permalink)  
Old March 11th, 2007, 09:06 AM
Friend of Wrox
 
Join Date: Aug 2006
Location: Amman, , Jordan.
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by robgerwing
 Here is the technique I use that have worked with me. I use master page as well. So hope this helps. You will see I added couple other things. This is base on ASP.NET 2.0 page. This is obviously a combination of different source on the internet and Imar's suggestion above creating new control.

1) On your ASPX add to Page Directive EnableEventValidation="false" Only use if the page complains about RenderControl must be in Form tag.

2) On the code behind of ASPX page as the following constructor:

public override void VerifyRenderingInServerForm(Control control)
    {
        // Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time.
    }
3) On the code behind of your grid (either aspx or if load on user control put there) This works if Session is not controled in SQL DB.

//First Step
//Declare Grid control after public partial class

public GridView GridView1 = new GridView();


//Second Step Method for button onclick

protected void btnExport_Click(object sender, EventArgs e) {
        Response.Clear();

        Response.AddHeader("content-disposition", "attachment; filename=FileName_"+DateTime.Now.ToShortDateString ().Replace("/","_")+".xls");

        Response.Charset = "";
        // If you want the option to open the Excel file without saving than
        // comment out the line below
        // Response.Cache.SetCacheability(HttpCacheability.No Cache);
        Response.ContentType = "application/vnd.ms-xls";
        //"application/vnd.ms-excel"
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

        //Reference the originally renders grid id name and your data source
        Session["ExportGridviewColumns"] = this.gvList.Columns;
        Session["ExportGridViewDataSource"] = this.objDsList.Select();

        //Add the columns to the print gridview
        foreach (DataControlField dcf in (DataControlFieldCollection)Session["ExportGridviewColumns"])
        {
            this.GridView1.Columns.Add(dcf);
        }

        //Turning off paging will avoid errors and get all contents
        //Provide some basic attributes to make excel data pretty

        GridView1.AllowPaging = false;
        GridView1.AllowSorting = false;
        GridView1.Font.Name = "Verdana";
        GridView1.Font.Size = 8;

        //Using ObjectDataSource Grabs Entire Contents
        this.GridView1.DataSource = Session["ExportGridViewDataSource"];
        this.GridView1.DataBind();

        GridView1.RenderControl(htmlWrite);

        Response.Write(stringWrite.ToString());

        Response.End(); }

I tried this code and it worked perfectly, so thanks. but does anyone knows how I can export a report created in crystal report to MS_Word? I've done it in .pdf files, I managed to export the data into an excel sheet, I only need it as a word document :) so any suggestions?

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in exporting data into pdf santhoshsanjeevi J2EE 0 April 3rd, 2008 06:15 AM
Problem in exporting data from datagrid to Excel deb_kareng ASP.NET 2.0 Professional 4 August 4th, 2007 09:21 AM
Problem Exporting Data... mat41 SQL Server 2000 1 September 29th, 2005 08:35 PM
Exporting data to excel from datagrid, Problem. swadhinm ASP.NET 1.x and 2.0 Application Design 0 May 31st, 2005 01:44 AM
Problem in exporting data to PDF vasansrini Crystal Reports 1 December 10th, 2004 11:48 AM



All times are GMT -4. The time now is 07:59 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.