2010-12-01 115 views
0

我正在同时将数据导出到Excel工作表异常数据导出到Excel

ERROR: 
Message : 
Exception of type 'System.Web.HttpUnhandledException' was thrown. 
Error Description : 
System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.Data.SqlClient.SqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. 

有人建议我什么我必须做以下错误,当到来。

CODE: 
gridData.dataSource = GetData() 
gridData.DataBind() 


Response.Clear() 
Response.AddHeader("content-disposition", "attachment;filename=CompletionDatesReport.xls") 
Response.Charset = "" 
Response.Cache.SetCacheability(HttpCacheability.NoCache) 
Response.ContentType = "application/vnd.xls" 
Dim stringWrite As StringWriter = New StringWriter() 
Dim htmlWrite As HtmlTextWriter = New HtmlTextWriter(stringWrite) 
gridData.RenderControl(htmlWrite) 
Response.Write(stringWrite.ToString()) 
Response.End() 
+0

抛出的异常是** sql **异常;你添加的代码告诉我们在编辑中仍然没有告诉我们你的数据库访问。我们所知道的只是`gridData`被介入到某个地方。 – 2010-12-01 06:17:16

+0

在我的代码我将记录绑定到DataGrid,然后将数据导出到excel sheet.In使用存储过程的GetData()函数我从数据库中获取数据。当我在查询分析器中运行存储过程它的确定,但在我的代码给出错误即将到来。 – Sukhjeevan 2010-12-01 06:33:58

回答

0

没有代码,我们只能猜测,但在SqlException: Timeout expired一大线索 - 这表明你的查询时间太长。您可以通过SqlCommand.CommandTimeout来增加命令的超时时间(但要尽量保持它的理智......),但是编写查询更有效率,或者不要在一个查询中拉取尽可能多的数据,通常会更可取。

0

我同意马克。看看你的查询并优化它,删除DISTINCT语句等,以使其更快。