1

我正在努力从经典 ASP 过渡到 ASP.NET。如果我硬编码开始日期和结束日期但给出错误,我有下面的代码:System.Data.SqlClient.SqlException:当我使用参数时必须声明标量变量“@SDate”。当我搜索它时,可能会引用此错误,但我找不到对我有帮助的错误。该页面旨在显示两个文本框,供用户输入搜索的开始和结束日期,然后单击按钮将数据导出到 Excel。

<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="iso-8859-1" EnableViewStateMac="false"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<html>
<head>
<title>Export results to Excel</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="C#" runat="server">

    protected void btnExportToExcel_Click(object sender, EventArgs e) {
        ExportToExcel();
    }

    //Export to Excel from a GridView
    protected void ExportToExcel() {
        Response.Clear();
        Response.Buffer = true;
        Response.ContentType = "application/vnd.ms-excel";
        Response.AddHeader("content-disposition", "attachment;filename=MyFiles.xls");
        Response.Charset = "";
        this.EnableViewState = false;

        System.IO.StringWriter sw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);

        gvData.RenderControl(htw);

        Response.Write(sw.ToString());
        Response.End();
    }

    public override void VerifyRenderingInServerForm(Control control) {
    }
</script>

</head>
<body>
Export results to excel
</body>
    <form id="form1" runat="server">
    <div>
        <h1>
            My Files</h1>
        <table border="0" cellpadding="0" cellspacing="0" style="width: 100%; height: 12%">
            <tr>
                <td>
                    Start date</td>
                <td>
                    <asp:TextBox ID="StartDate" runat="server" Width="200px" Text="2000/01/01"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>

                    End date</td>
                <td>
                    <asp:TextBox ID="EndDate" runat="server" Width="200px" Text="2012/11/01"></asp:TextBox>
                    </td>
            </tr>
            <tr>
                <td>

                </td>
                <td>
                    <asp:Button ID="btnExportToExcel" runat="server" Text="Export to Excel" onclick="btnExportToExcel_Click" />
                </td>
            </tr>
        </table>
    </div>
    <asp:SqlDataSource ID="SqlDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:KpraxConn %>" 
        SelectCommand="SELECT Results.IDNumber, [FirstName], [LastName], [DOB], [Gender] FROM [Results] LEFT JOIN Login ON (Results.IDNumber=Login.IDNumber) where ([LogDate]>=@SDate and [LogDate]<=@EDate) ORDER BY [LastName]">
        <asp:SelectParameters>
            <asp:FormParameter FormField="StartDate" Name="SDate" DefaultValue="2000/01/01" />
            <asp:FormParameter FormField="EndDate" Name="EDate" DefaultValue="2012/11/04"  />
        </asp:SelectParameters>
    </asp:SqlDataSource>

    <asp:GridView ID="gvData" runat="server" AutoGenerateColumns="False" DataKeyNames="IDNumber" 
        DataSourceID="SqlDataSource" PageSize="5">
        <Columns>
            <asp:BoundField DataField="IDNumber" HeaderText="IDNumber" ReadOnly="True"/>
           <asp:BoundField DataField="FirstName" HeaderText="FirstName"/>
           <asp:BoundField DataField="LastName" HeaderText="LastName"/>
           <asp:BoundField DataField="DOB" HeaderText="DOB"/>
           <asp:BoundField DataField="Gender" HeaderText="Gender"/>
        </Columns>
    </asp:GridView>
    </form>

</html>
4

1 回答 1

0

在按钮单击事件上调用此方法

受保护的无效 ExportToExcel() {

 Response.Clear();
 Response.AddHeader("content-disposition", "attachment;filename=GridView1.xls");
 Response.Charset = "";
 Response.ContentType = "application/vnd.xls";
 StringWriter StringWriter = new System.IO.StringWriter();
 HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter);
 GridView1.RenderControl(HtmlTextWriter);
 Response.Write(StringWriter.ToString());
 Response.End();

}

于 2012-11-04T09:25:51.110 回答