Hi there
I get the following error message while displaying a page

DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'f'.

I have a FormView control which is bound to an sqldatasource

when it is loaded, it displays the above error msg

here is the formview ItemTemplate code

<ItemTemplate>
                            <table>
                                <tr>
                                    <td align = "right"><b>Forum ID: </b></td>
                                    <td align = "left">
                                        <asp:TextBox ID = "txtForumID" runat = "server" Text = '<%# Eval("f.name") %>' ReadOnly = "true" />
                                    </td></tr>                                
                                <tr>
                                    <td align = "right"><b>Topic ID: </b></td> 
                                    <td align = "left">
                                        <asp:TextBox ID = "txtTopicID" runat = "Server" Text = '<%# Eval("topicID") %>' ReadOnly = "true" />
                                    </td></tr>
                                <tr>
                                    <td align = "right"><b>Topic Name: </b></td>
                                    <td align = "left">
                                        <asp:TextBox ID = "txtTopicName" runat = "server" Text = '<%# Eval("t.name") %>' ReadOnly = "true" />
                                    </td></tr>
                                <tr>
                                    <td align = "right"><b>Descripttion: </b></td>
                                    <td align = "left">
                                        <asp:TextBox ID = "txtDescription" runat = "Server" Text = '<%# Eval("t.description") %>' ReadOnly = "true" />
                                    </td></tr>
                                <tr>
                                    <td>
                                        <br /><br />
                                        <asp:LinkButton ID = "lbtnEdit" runat = "server" Text = "Edit" CommandName = "Edit" />&nbsp;
                                        <asp:LinkButton ID = "lbtnDelete" runat = "server" Text = "Delete" CommandName = "Delete" />&nbsp;
                                        <asp:LinkButton ID = "lbtnNew" runat = "Server" Text = "New" CommandName = "New" />
                                    </td>
                                </tr>                    
                            </table>
 </ItemTemplate>

the code of the datasource is as below

<asp:SqlDataSource ID = "FVDataSource" runat = "server" 
                            ConnectionString = "<%$ ConnectionStrings:ForumConnectionString %>"
                            SelectCommand = "SELECT f.forumID, f.name, t.topicID, t.name, t.description FROM tblTopics AS t 
                                             INNER JOIN tblForums AS f ON t.forumID = f.forumID 
                                             WHERE t.topicID = @topicID"
                            InsertCommand = "INSERT INTO [tblTopics] (forumID, name, description) VALUES (@forumID, @name, @description);
                                             SELECT @topicID = SCOPE_IDENTITY()"
                            UpdateCommand = "UPDATE [tblTopics] SET forumID = @forumID, name = @name, description = @description WHERE topicID = @topicID"
                            DeleteCommand = "DELETE [tblTopics] WHERE topicID = @topicID"
                            OnInserting = "FVDataSource_Inserting" OnDeleting = "FVDataSource_Deleting">
               <SelectParameters>
                   <asp:Parameter Name = "topicID" Type = "int32" DefaultValue = "0" />
               </SelectParameters>
  </asp:SqlDataSource>

Thanks

Dani AI

Generated

is spot on. The FormView binds each row as a DataRowView, and Eval(...) expects the name of a field in that row. The f and t parts are SQL table aliases, not properties on the data item, so Eval("f.name") (or Eval("t.name")) will throw the error you saw. Use the field name (or a column alias), not a qualified SQL name. (learn.microsoft.com)

One more gotcha here: your SELECT returns two columns named name (one from each table). ADO.NET tables require unique column names, so duplicate names lead to confusion or exceptions at runtime. The simplest fix is to alias the overlapping columns and bind to those aliases. (learn.microsoft.com)

Quick, concrete fix:

-- SqlDataSource.SelectCommand
SELECT
  f.forumID,
  f.name AS ForumName,
  t.topicID,
  t.name AS TopicName,
  t.description AS TopicDescription
FROM tblTopics AS t
JOIN tblForums AS f ON t.forumID = f.forumID
WHERE t.topicID = @topicID;
<!-- FormView ItemTemplate -->
<asp:TextBox ID="txtForum" runat="server" Text='<%# Eval("ForumName") %>' ReadOnly="true" />
<asp:TextBox ID="txtTopicID" runat="server" Text='<%# Eval("topicID") %>' ReadOnly="true" />
<asp:TextBox ID="txtTopicName" runat="server" Text='<%# Eval("TopicName") %>' ReadOnly="true" />
<asp:TextBox ID="txtDescription" runat="server" Text='<%# Eval("TopicDescription") %>' ReadOnly="true" />

If you plan to use the FormView’s built-in Update/Delete, also set DataKeyNames="topicID" so the control knows which record to modify. (learn.microsoft.com)

Small UX note: your label says "Forum ID" but the binding shows the forum name. Either bind to forumID or change the label text so the UI reflects what you are displaying.

Recommended Answers

All 2 Replies

instead of Eval("f.name")

try

Eval("name")

You shouldn't need the table alias to select fields. If there are duplicate fields, like forumid, use a field alias, like "Select f.forumid as forumID_1, s.forumid as forumID_2.....".

thank you very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.