Click here to Skip to main content
15,904,503 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tested a below code to export database from Gridview to Excel,but error
"Control 'gvInHoaDon' of type 'GridView' must be placed inside a form tag with runat=server"
C#
private void InHoaDonDuocChon(GridView gvInHoaDon)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition",
         "attachment;filename=HoaDon.xls");
        Response.Charset = "";
        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        
        gvInHoaDon.AllowPaging = false;
        gvInHoaDon.DataBind();
        gvInHoaDon.RenderControl(hw);
        //Thay đổi Style
        string style = @"<style> .textmode { mso-number-format:\@; } </style>";
        Response.Write(style);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
    }

I referenced 2 ways: http://www.codedigest.com/Articles/ASPNET/130_Export_to_Excel_in_ASPNet_20_-Gridview_to_Excel_DataTable_to_Excel.aspx[^]
but error "Control 'gvInHoaDon' of type 'GridView' must be placed inside a form tag with runat=server"
Please help me.
Many thanks.
Posted
Updated 24-Jul-12 2:30am
v2
Comments
jeyamothi 24-Jul-12 8:45am    
r u bind the data from gv only?

Post your aspx code here.
It looks like runat=server is missing inside <asp:gridview xmlns:asp="#unknown"> tag. Add that.
 
Share this answer
 
Try this, i think its working

you bind your datagrid value into xlDV and you can access code like this,


Dim dt As New DataTable
        dt = xlDV.ToTable
        'Get the data from database into datatable
        Response.Clear()
        Response.Buffer = True
        Response.AddHeader("content-disposition", "attachment;filename=ExpData.csv")

        Response.Charset = ""
        Response.ContentType = "application/text"
        Dim sb As New StringBuilder()
        For k As Integer = 0 To dt.Columns.Count - 1
            'add separator
            sb.Append(dt.Columns(k).ColumnName + ","c)
        Next
        'append new line
        sb.Append(vbCr & vbLf)



        For Each resultDR As DataRow In dt.Rows
            For Each resultDC As DataColumn In dt.Columns
                Dim strTemp As String = resultDR.Item(resultDC.ColumnName).ToString()
                If strTemp.Contains(System.Environment.NewLine) = True Then
                    strTemp = strTemp.Replace(System.Environment.NewLine, " ")
                End If
                'add separator
                sb.Append(strTemp.Replace(",", ";") + ","c)
            Next
            'append new line
            sb.Append(vbCr & vbLf)
        Next






        Response.Output.Write(sb.ToString())
        Response.Flush()
        Response.End()


Thanks,
jeyamothi
 
Share this answer
 
XML
<asp:GridView ID="gvInHoaDon" runat="server" AutoGenerateColumns="False"
                    ShowHeader="False" style="margin-right: 0px"
                    ToolTip="Hiển thị hóa đơn được chọn." Width="333px">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <table ID="thongtin" style="height: 199px; width: 519px">
                                    <tr>
                                        <td align="center">
                                            <asp:Image ID="Image1" runat="server" Height="61px"
                                                ImageUrl="~/Images/Layout/logo1.jpg" Width="125px" style="margin-left: 0px" />
                                        </td>
                                        <td align="center">
                                            <h2>
                                                Hóa đơn bán lẻ</h2>
                                            Mẫu số: 01GTKT/001
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="center">
                                            &nbsp;</td>
                                        <td>
                                            &nbsp;</td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Mã hóa đơn:</td>
                                        <td>
                                            <asp:Label ID="lblMaHoaDon" runat="server" Text='<%# Eval("MaHoaDon") %>'></asp:Label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Mã phòng:</td>
                                        <td>
                                            <asp:Label ID="lblMaPhong" runat="server" Text='<%# Eval("MaPhong") %>'></asp:Label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Giá phòng:
                                        </td>
                                        <td>
                                            <asp:Label ID="lblGiaPhong" runat="server" Text='<%# Eval("GiaPhong") %>'></asp:Label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Giá điện:
                                        </td>
                                        <td>
                                            <asp:Label ID="lblGiaDien" runat="server" Text='<%# Eval("GiaDien") %>'></asp:Label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>

                                            Giá nước:</td>
                                        <td>

                                            <asp:Label ID="lblGiaNuoc" runat="server" Text='<%# Eval("GiaNuoc") %>'></asp:Label>

                                        </td>
                                    </tr>
                                    <tr>
                                        <td>

                                            Phụ thu:</td>
                                        <td>

                                            <asp:Label ID="lblPhuThu" runat="server" Text='<%# Eval("PhuThu") %>'></asp:Label>

                                        </td>
                                    </tr>
                                    <tr>
                                        <td>

                                            Ghi chú:</td>
                                        <td>

                                            <asp:Label ID="lblGhiChu" runat="server" Text='<%# Eval("GhiChu") %>'></asp:Label>

                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Nợ cũ:</td>
                                        <td>
                                            <asp:Label ID="lblNoCu" runat="server" Text='<%# Eval("NoCu") %>'></asp:Label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Ngày lập:</td>
                                        <td>
                                            <asp:Label ID="lblNgayLap" runat="server" Text='<%# Eval("NgayLap") %>'></asp:Label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Tổng tiền:</td>
                                        <td>
                                            <asp:Label ID="lblTongTien" runat="server" Text='<%# Eval("TongTien") %>'></asp:Label>
                                            &nbsp;VNĐ</td>
                                    </tr>
                                </table>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>


Please check.
Many thanks.
 
Share this answer
 
Check you are missing runat="server" for any form tags.
Like this

runat="server">
 
Share this answer
 
Thanks everyone very much...I've solved this myself.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900