Error: Invalid code syntax for BindItem


In this article we will see how to resolve the below error Error: Invalid code syntax for BindItem when binding a DataBound control like FormView in a web page. I got this error when binding FormView with the datasource.

In this article we will see how to resolve the below error:
Error: Invalid code syntax for BindItem

I got this error when binding FormView with the datasource.

Here is the code which throws error:


<asp:FormView ID="FormView1" ItemType="StrongDTypeDataBoundDemo.Emp" runat="server">
<ItemTemplate>
<div>
Emp Name :
<asp:Label ID="lblEmpName" runat="server" Text='<%#BindItem.EmpName %>'></asp:Label><br />
</div>
<div>
Hire Date :
<asp:Label ID="Label2" runat="server" Text='<%#BindItem.HireDate.ToShortDateString() %>'></asp:Label>
</div>
</ItemTemplate>
</asp:FormView>

In the above code, <%#BindItem.HireDate.ToShortDateString() %> throws the error, In order to resolve this error, Instead of using BindItem, use Item as shown below:

<asp:Label ID="Label2" runat="server" Text='<%#Item.HireDate.ToShortDateString() %>'></asp:Label>


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: