C# 数据绑定:';System.Data.Common.DataRecordInternal';不包含名为';文件名';
我目前在数据绑定方面遇到问题。我仍在学习如何正确地进行数据绑定。问题是数据绑定:“System.Data.Common.DataRecordInternal”不包含名为“fileName”的属性 据我所知,数据绑定是需要与后端编码一致的文件名,但尽管我做了相同的操作,错误仍然会发生。下面是我的aspx.cs编码C# 数据绑定:';System.Data.Common.DataRecordInternal';不包含名为';文件名';,c#,asp.net,windows,visual-studio,data-binding,C#,Asp.net,Windows,Visual Studio,Data Binding,我目前在数据绑定方面遇到问题。我仍在学习如何正确地进行数据绑定。问题是数据绑定:“System.Data.Common.DataRecordInternal”不包含名为“fileName”的属性 据我所知,数据绑定是需要与后端编码一致的文件名,但尽管我做了相同的操作,错误仍然会发生。下面是我的aspx.cs编码 protected void DownloadFile(object sender, EventArgs e) { int id = int.Parse((sen
protected void DownloadFile(object sender, EventArgs e)
{
int id = int.Parse((sender as LinkButton).CommandArgument);
//byte[] bytes;
string fileName;
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Ulysses"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT TOP 10 OLE_LINK_FILE_NAME FROM OLE_LINK";
//cmd.Parameters.AddWithValue("@Id", id);
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
fileName = sdr["OLE_LINK_FILE_NAME"].ToString();
}
con.Close();
}
}
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
//Response.ContentType = contentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
//Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
以下是我的HTML编码:
<asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="OLE_LINK_FILE_NAME" HeaderText="File Name"/>
<asp:TemplateField ItemStyle-HorizontalAlign = "Center">
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="DownloadFile"
CommandArgument='<%# Eval("fileName") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
在编码过程中,参数fileName在哪里丢失了?您正在为
GridView1
设置的数据源,是System.Data.Common.DataRecordInternal
的列表,我认为您的类System.Data.Common.DataRecordInternal
要么没有名为fileName
的属性,要么它的范围有限。如果存在,请尝试将其公开
从你下面的评论。您应该在下载链接的命令参数中将
fileName
替换为OLE\u LINK\u FILE\u NAME
。加载时或单击下载后出现错误?甚至无法显示页面。它会自动显示错误。我对C不太熟悉,但我确实认为您的答案是正确的,因为Visual studio显示我的错误与System.Data.Common.DataRecordInternal有关。我在哪里更改它,因为我在我的aspx.cs页面中看不到它。将System.Data.Common.DataRecordInternal写入.cs文件中的任意位置,然后按F12键,它将带您进入类定义。一个问题,是inbuild类还是有人编写的?问题2:能否描述绑定到网格的结构?代码来自此链接:我使用它学习如何从数据库下载文件。我不确定结构,但基本上我想绑定数据库中表的数据,以便可以在网页上下载。我希望我的解释足够了。数据库中的数据表列和链接中的数据表列相同吗?