DataBinding:' System.Data.Common.DataRecordInternal'不包含名称为' fileName'的属性

时间:2016-05-04 08:40:26

标签: c# asp.net windows visual-studio data-binding

我目前遇到数据绑定问题。我还在学习如何使用正确的方法来进行数据绑定。问题是DataBinding:' System.Data.Common.DataRecordInternal'不包含名称为' fileName'的属性。

据我所知,数据绑定是需要与后端编码一致的fileName,但是尽管我做了同样的事情,但错误仍然存​​在。下面是我的aspx.cs编码。

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?

1 个答案:

答案 0 :(得分:1)

您为GridView1设置的数据源是System.Data.Common.DataRecordInternal的列表,我认为您的班级System.Data.Common.DataRecordInternal要么没有名称为fileName的属性,要么就是范围有限。如果存在,请尝试将其设为public

来自您的评论如下。您应该在下载链接的命令参数中将fileName替换为OLE_LINK_FILE_NAME