C# 选择导出到文本文件时命令初始化错误
我正在尝试将gridview导出到文本文件,当我单击“导出”时,出现以下错误。“System.InvalidOperationException:SelectCommand属性在调用'Fill'之前尚未初始化。”因此,按钮ClickSearchCredit2从连接到SqlDataSource的链接数据库运行存储过程,并在GridView4中返回一些数据。我想在单击ExportTxt按钮时将该数据导出到文本文件中 我不确定我需要在我的C代码中添加什么才能让它正常工作C# 选择导出到文本文件时命令初始化错误,c#,asp.net,gridview,C#,Asp.net,Gridview,我正在尝试将gridview导出到文本文件,当我单击“导出”时,出现以下错误。“System.InvalidOperationException:SelectCommand属性在调用'Fill'之前尚未初始化。”因此,按钮ClickSearchCredit2从连接到SqlDataSource的链接数据库运行存储过程,并在GridView4中返回一些数据。我想在单击ExportTxt按钮时将该数据导出到文本文件中 我不确定我需要在我的C代码中添加什么才能让它正常工作 void GVProsFill
void GVProsFill()
{
SqlDataAdapter adp = new SqlDataAdapter();
DataSet ds = new DataSet();
adp.Fill(ds);
GridView4.DataSource = ds.Tables[0];
GridView4.DataBind();
}
完整代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.IO;
using System.Text;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
namespace crifaccess
{
public partial class CrifCredit : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
void GVProsFill()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConToSEPL"].ConnectionString);
SqlDataAdapter adp = new SqlDataAdapter();
DataSet ds = new DataSet();
adp.Fill(ds);
GridView4.DataSource = ds.Tables[0];
GridView4.DataBind();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void ExportTextFile (object sender, EventArgs e)
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "creditfile.txt"));
Response.ContentType = "text/plain";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView4.AllowPaging = false;
GVProsFill();
GridView4.HeaderRow.Style.Add("background-color", "#FFFFFF");
for (int a = 0; a < GridView4.HeaderRow.Cells.Count; a++)
{
GridView4.HeaderRow.Cells[a].Style.Add("background-color", "#507CD1");
}
int j = 1;
foreach (GridViewRow gvrow in GridView4.Rows)
{
gvrow.BackColor = Color.White;
if (j <= GridView4.Rows.Count)
{
if (j % 2 != 0)
{
for (int k = 0; k < gvrow.Cells.Count; k++)
{
gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
}
}
}
j++;
}
GridView4.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
protected void btnClear_Click20 (object sender, EventArgs e)
{
SearchSSNCredit.Text = "";
}
protected void btnClear_Click21 (object sender, EventArgs e)
{
SearchAppCredit.Text = "";
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用系统数据;
使用System.Data.SqlClient;
使用系统配置;
使用系统图;
使用System.IO;
使用系统文本;
使用System.Web.Security;
使用System.Web.UI.HTMLControl;
使用System.Web.UI.WebControl.WebParts;
名称空间CRIFACESS
{
公共部分类CrifCredit:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
}
void/prosfill()
{
SqlConnection con=新的SqlConnection(ConfigurationManager.ConnectionString[“ConToSEPL”].ConnectionString);
SqlDataAdapter adp=新的SqlDataAdapter();
数据集ds=新数据集();
自动进料(ds);
GridView4.DataSource=ds.Tables[0];
GridView4.DataBind();
}
公共覆盖无效VerifyRenderingInServerForm(控件)
{
}
受保护的void ExportTextFile(对象发送方,事件参数e)
{
Response.ClearContent();
Response.Buffer=true;
AddHeader(“内容处置”,string.Format(“附件;文件名={0}”,“creditfile.txt”);
Response.ContentType=“text/plain”;
StringWriter sw=新的StringWriter();
HtmlTextWriter htw=新的HtmlTextWriter(sw);
GridView4.AllowPaging=false;
GVProsFill();
GridView4.HeaderRow.Style.Add(“背景色”和“#FFFFFF”);
对于(int a=0;a
函数w3_open(){
document.getElementById(“main”).style.marginLeft=“5%”;
document.getElementById(“mySidebar”).style.display=“内联块”;
document.getElementById(“openNav”).style.display=“无”;
}
函数w3_close(){
document.getElementById(“main”).style.marginLeft=“0%”;
document.getElementById(“mySidebar”).style.display=“无”;
document.getElementById(“openNav”).style.display=“内联块”;
}
这就是现在显示在文本文件顶部和中间的内容
div>
<table cellspacing="0" cellpadding="3" rules="rows" id="GridView4" style="background-color:White;border-color:#E7E7FF;border-width:1px;border-style:None;border-collapse:collapse;">
<tr style="color:#F7F7F7;background-color:#4A3C8C;font-weight:bold;background-color:#FFFFFF;">
<th scope="col" style="background-color:#507CD1;">AppID</th><th scope="col" style="background-color:#507CD1;">Credit File</th><th scope="col" style="background-color:#507CD1;">CreationDate</th>
</tr><tr style="color:#4A3C8C;background-color:White;">
<td align="left" style="background-color:#EFF3FB;">1017</td><td align="left" style="background-color:#EFF3FB;">
<div style="width: 100px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap;">
<span id="GridView4_Label1_0">
div>
AppIDCredit文件创建日期
1017
11/17/2016
1017
您的SqlDataAdapter不知道要运行什么样的SQL查询。您必须给它一个“我有一个已链接到gridview的存储过程”。我该如何将其添加到该存储过程中?很抱歉,我在这里是新手。我可以写一个答案,但我仍然不清楚此代码与导出到文本文件的关系。这与您的代码所描述的内容不同正在做的是尝试将数据绑定到网格。我将添加我的fu
div>
<table cellspacing="0" cellpadding="3" rules="rows" id="GridView4" style="background-color:White;border-color:#E7E7FF;border-width:1px;border-style:None;border-collapse:collapse;">
<tr style="color:#F7F7F7;background-color:#4A3C8C;font-weight:bold;background-color:#FFFFFF;">
<th scope="col" style="background-color:#507CD1;">AppID</th><th scope="col" style="background-color:#507CD1;">Credit File</th><th scope="col" style="background-color:#507CD1;">CreationDate</th>
</tr><tr style="color:#4A3C8C;background-color:White;">
<td align="left" style="background-color:#EFF3FB;">1017</td><td align="left" style="background-color:#EFF3FB;">
<div style="width: 100px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap;">
<span id="GridView4_Label1_0">
</div>
</td><td align="left" style="background-color:#EFF3FB;">11/17/2016</td>
</tr><tr style="color:#4A3C8C;background-color:White;">
<td align="left">1017</td><td align="left">
<div style="width: 100px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap;">
<span id="GridView4_Label1_1">