namasayadin
namasayadin

Reputation: 45

DataBinding: 'System.Data.Common.DataRecordInternal' does not contain a property with the name 'fileName'

I am currently having trouble with data binding. I am still learning on how to have a correct way to make data binding. The problem is DataBinding: 'System.Data.Common.DataRecordInternal' does not contain a property with the name 'fileName'.

As far as I understand, the data binding is the fileName that need to be tally with the back end coding but although I made it the same, the error still occur. Below is my aspx.cs coding.

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();
    }

Here is my HTML coding:

<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>

From the coding, where actually did I miss with the parameter fileName?

Upvotes: 1

Views: 6210

Answers (1)

Imad
Imad

Reputation: 7490

The data source you are setting for GridView1, is List of System.Data.Common.DataRecordInternal and I think your class System.Data.Common.DataRecordInternal either don't have property with the name fileName or it's scope is limited. If it exist, try to make it public.

from your comment below. You should replace fileName by OLE_LINK_FILE_NAME in command argument of download link.

Upvotes: 2

Related Questions