tags:

views:

3543

answers:

12

Exporting from c#.net I am getting a problem I have a form that when I export to excel as a result in excel Any ideas why is this happening I am including the ASP code below.

<%@ Page Language="C#" MasterPageFile="~/masterpages/Admin.master" AutoEventWireup="true" CodeFile="members-search-adv.aspx.cs" Inherits="masteradmin_members_search_adv" Title="LISA - Life Insurance Settlement" %>
<%@ Register TagPrefix="UC" TagName="Paging" Src="~/controls/Paging.ascx" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <script type="text/javascript" src="sorttable.js"></script>
 Go to <a href="members-search.aspx" class="link">Quick Search</a>
<h1>Contact Database - Advanced Search</h1>
<asp:Label ID="lblSearchCriteria" runat="server" Text=""  ForeColor="blue"></asp:Label>
<table width="100%" cellspacing="0" cellpadding="0">
    <tr>
     <td valign="top" width="50%">
<table border="0" cellpadding="3" cellspacing="1" style="height: 214px; width: 101%;">  
    <tr>
     <td class="form1" align="right"><strong>OpenSearch:</strong></td>
     <td class="form2">
            <asp:TextBox ID="txtSearch" runat="server" CssClass="fields" Width="245px" ></asp:TextBox><b>*</b>&nbsp;<br><b>*</b><small>Search By Company Name, First Name, Last Name, Tags, Comments</small></td>
    </tr>
    <tr>
     <td class="form1" align="right"><strong>Industry Segment:</strong></td>
     <td class="form2"><asp:DropDownList ID="ddlIndSegment" runat="server" CssClass="fields" /></td>
    </tr>
    <tr>
     <td class="form1" align="right"><strong>Member Type:</strong></td>
     <td class="form2"><asp:DropDownList ID="ddlMemberType" runat="server" CssClass="fields" /></td>
    </tr>
    <tr>
     <td class="form1" align="right"><strong>Member Rep only:</strong></td>
     <td class="form2"><asp:CheckBox ID="cbxMemberRep" runat="server" /></td>
    </tr>
    <tr>
     <td class="form1" align="right"><strong>Board Only:</strong></td>
     <td class="form2"><asp:CheckBox ID="cbxBoardOnly" runat="server" /></td>
    </tr>
    <tr>
     <td colspan="2" align="right">
      <asp:ImageButton ID="btnSearch" runat="server" ImageUrl="/RadControls/images/bu_search.jpg" />
     </td>
    </tr>
    </table>
    </td>
     <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
    <td valign="top" width="50%">
    <%--split the table here--%>
     <table border="0" cellpadding="3" cellspacing="1" 
            style="width: 98%; margin-left: 0px;">
    <tr>
     <td class="form1" align="right"><strong>Suspended Only:</strong></td>
     <td class="form2"><asp:CheckBox ID="cbxSuspended" runat="server" /></td>
    </tr>
    <tr>
     <td class="form1" align="right"><strong>Active Only:</strong></td>
     <td class="form2"><asp:CheckBox ID="cbxActiveOnly" runat="server" /></td>
    </tr>
    <tr>
     <td class="form1" align="right"><strong>Allow Other Members To See My Info Only:</strong></td>
     <td class="form2"><asp:CheckBox ID="cbxAllowOtherMembers" runat="server" /></td>
    </tr>
    <tr>
     <td class="form1" align="right"><strong>State:</strong></td>
     <td class="form2"><asp:DropDownList ID="ddlState" runat="server" CssClass="fields" /></td>
    </tr>
     <tr>
     <td class="form1" align="right"><strong>By Special Level:</strong></td>
     <td class="form2"><asp:DropDownList ID="ddlSpecialLevel" runat="server" CssClass="fields" /></td>
    </tr>

    <tr>
     <td class="form1" align="right"><strong>Sort by:</strong></td>
     <td class="form2">
      <asp:DropDownList ID="ddlSortBy" runat="server" CssClass="fields">
       <asp:ListItem Value="CompanyName">Company</asp:ListItem> 
       <asp:ListItem Value="FirstName">First Name</asp:ListItem>
       <asp:ListItem Value="LastName">Last Name</asp:ListItem>
       <asp:ListItem Value="MemberCategoryId">Membership Type</asp:ListItem>
       <asp:ListItem Value="IndustrySegmentId">Industry Segment</asp:ListItem>
      </asp:DropDownList>
     </td>
    </tr>

</table>    
</td>
</tr>
</table>
    <table  border="0" cellpadding="3" cellspacing="1" width="100%">  
<tr>
<td><asp:Literal ID="litTotalCount" runat="server" /></td><td align=left class="form3">
    <asp:Button ID="Button1" runat="server"  onclick="Button1_Click" Text="Button" />
    <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Export To Excel</asp:LinkButton>
    <small>* Click on the column to SORT</small></td></tr>
</table>

    <asp:Repeater ID="rptList" runat="server">
     <HeaderTemplate>
     <table class="sortable" border="0" cellpadding="3" cellspacing="1" width="100%">  
            <tr>
             <td align="left" valign="top" class="form1"><b>Company</b></td>
             <td align="left" valign="top" class="form1"><b>First Name</b></td>
             <td align="left" valign="top" class="form1"><b>Last Name</b></td>
             <td align="left" valign="top" class="form1"><b>Email</b></td>
            </tr>
     </HeaderTemplate>
     <ItemTemplate>
      <tr>
       <td class="form2" align="left" valign="top"><asp:HyperLink ID="hlCompany" runat="server" CssClass="link"></asp:HyperLink></td>
       <td class="form2" align="left" valign="top"><asp:HyperLink ID="hlFirstName" runat="server" CssClass="link"></asp:HyperLink></td>
       <td class="form2" align="left" valign="top"><asp:HyperLink ID="hlLastName" runat="server" CssClass="link"></asp:HyperLink></td>
       <td class="form2" align="left" valign="top"><asp:HyperLink ID="hlEmail" runat="server" CssClass="link"></asp:HyperLink> </td>
      </tr>
     </ItemTemplate>
     <FooterTemplate>
     </table>
     </FooterTemplate>
    </asp:Repeater>

    <asp:GridView ID="GridView2" runat="server">
    </asp:GridView>

    <asp:PlaceHolder ID="PlaceHolder1" runat="server"><br />
    <UC:Paging Id="ctlPaging" runat="server" />
    </asp:PlaceHolder>
<%--    <div align="center">
        <br /><UC:Paging Id="ctlPaging" runat="server" />
    </div>--%>

</asp:Content>
A: 

Sorry but this is a better explanation of the issue Exporting from c#.net I am getting a problem I have a form that when I export to excel I ge the following <div></div> I have data in the datagrid but not in excel I am just getting <div></div>

Please edit your original post in lieu of creating an answer to provide additional information.
Quintin Robinson
Where is the code to export? All I can see is some repeater placed on a page. Post some C# page that is exporting and error message that you are getting.
Pradeep
A: 

And this is the function that I am using

void ExportToExcel3()
 {
     Response.Clear();
     Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
     Response.Charset = "";
     Response.ContentType = "application/vnd.xls";
     System.IO.StringWriter stringWrite = new System.IO.StringWriter();
     System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
     GridView2.RenderControl(htmlWrite);
     Response.Write(stringWrite.ToString());
     Response.End();
 }



public override void VerifyRenderingInServerForm(Control control)
 {

 }
+1  A: 

Are you populating gridview2 prior to the export (in the same request)?

Unless it is databound/populated prior to the export call through the page hierarchy (because it doesn't appear to happen in the export method) the results will be an empty grid. That might be your issue.

Quintin Robinson
A: 

YES I am populating the datagrid prior to the export. This is the line of code. It's funny I create a new webform just with this section of code and the export worked. Do you think there's a problem with the datarepeater that I have in the page that is causing the conflict.

MemberList list = MemberDB.GetMembers(sql, m_page, m_RecordPerPage, out count, _state);
          this.GridView2.DataSource = list;
          this.GridView2.DataBind();
A: 

Where do you call the ExportToExcel3() function? It looks to me like you're rendering the datagrid before the databinding event in the page lifecycle.

Joel Coehoorn
A: 

From a button So the sequence is I [LOAD event] fill out the Datagrid with my storedprocedure and then I ask the user to clik on the ExportTOExcel button.

Note, this is why in my answer I said "In the same request". You must populate the grid in the same request as the click. You must consider your page stateless. The current request doesn't really know that the previous request populated data unless you make it aware.
Quintin Robinson
You're right, CQ, but I've been finding a lot of people have no idea what you mean by "in the same request" or "pages are stateless" until they see something like what I've written in the next response.
Joel Coehoorn
Yes your response was very concise, I'm glad someone could express verbosely what I had intended to say!
Quintin Robinson
+1  A: 

That's not the sequence at all- it's just not how asp.net and the web work.

You don't fill the grid during the load phase: you retrieve the data and set it as the datasource for the table, but the grid (actually a repeater) isn't filled yet. That doesn't happen until the databinding phase. After the databinding phase (during the render phase) the page with the databound repeater is finally sent by the server to the browser. At this point all your server-side stuff is disposed.

Now the browser can finally show the page with the big "Export to Excel" button which the user can click. Again: the server is already destroying all the data you worked so hard to build. When the user clicks your button, a brand new request is created and a brand new page will be sent back to the browser. You have to load all your data into the grid again.

Joel Coehoorn
A: 

so do you advise to bind the datagrid again inside the exportToExcel button?

You must re-populate either inside or prior to the call within the same request as the click.
Quintin Robinson
A: 

I found the solution to my problem. Thank you all for contributing. On the Load_Page Event I had the binding inside of an if !postback.

A: 

I am getting a problem while exporting data to excel.... I have a generate report button. once I click on that button a reportin generated... but my generate report button get disabled unless I refresh the page. what should I do????

+1  A: 

There is a wonderful library through which you can export data to excel. For Details see this link:-

Export To Excel in .Net

Zeeshan Umar