subscribe to the RSS Feed

Thursday, March 18, 2010

ASP.NET 2.0: Error message when you use nested data controls that have different data sources in ASP.NET 2.0 or in later versions: “Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control”

By TechSupport


SUPPORT PROBLEM: Error message when you use nested data controls that have different data sources in ASP.NET 2.0 or in later versions: “Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control”

Applications Supported:

COPYRIGHT NOTICE: (c) 2007 Microsoft Corporation. All rights reserved.

SUPPORT SOLUTION:
You have a computer that is running Microsoft ASP.NET 2.0 or later versions. When you use nested data controls that have different
data sources, you
intermittently
receive the following error message:Databinding methods such as Eval(), XPath(), and Bind() can only
be used in the context of a databound control.
You receive the error message in one of the following situations.Situation 1
2, ’symptoms’);
You use a
nested data presentation control to call the DataBind method. The data presentation control is bound
to a data source. The following is
a sample code that can be used to call the DataBind method:<asp:DetailsView ID=”DetailsView1″ runat=”server” AllowPaging=”True” AutoGenerateRows=”False”
DataKeyNames=”EmployeeID” DataSourceID=”SqlDataSource2″ Height=”50px” Width=”125px”
AutoGenerateEditButton=”True”>
<Fields>
<asp:TemplateField HeaderText=”Department”>
<ItemTemplate>
<%#Eval(”DepartmentId”) %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID=”ddrDepartment” runat=”server” DataSourceID=”SqlDataSource1″ DataTextField=”Name” DataValueField=”DepartmentID” SelectedValue=’<%#Bind(”DepartmentId”) %>’ onselectedindexchanged=”ddrDepartment_SelectedIndexChanged” AutoPostBack=”True”>
</asp:DropDownList>
<asp:SqlDataSource ID=”SqlDataSource1″ runat=”server” ConnectionString=”<%$ ConnectionStrings:AdventureWorksConnectionString %>”
SelectCommand=”SELECT DepartmentID, Name FROM HumanResources.Department”></asp:SqlDataSource>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField=”Title” HeaderText=”Title” SortExpression=”Title” />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID=”SqlDataSource2″ runat=”server” ConnectionString=”<%$ ConnectionStrings:AdventureWorksConnectionString %>”
SelectCommand=”SELECT EmployeeID, DepartmentId, Title FROM HumanResources.Employee”
UpdateCommand=”UPDATE HumanResources.Employee SET DepartmentId = @DepartmentId, Title = @Title WHERE (EmployeeID = @employeeid)”>
<UpdateParameters>
<asp:Parameter Name=”DepartmentId” />
<asp:Parameter Name=”Title” />
<asp:Parameter Name=”employeeid” />
</UpdateParameters>
</asp:SqlDataSource>
Microsoft Visual C# codeprotected void ddrDepartment_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddr = (DropDownList)DetailsView1.Rows[1].FindControl(”ddrDepartment”);
ddr.DataBind();
}
Microsoft Visual Basic .NET codeProtected Sub ddrDepartment_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim ddr As DropDownList = DirectCast(DetailsView1.Rows(1).FindControl(”ddrDepartment”), DropDownList)
ddr.DataBind()
End Sub
Situation 2
2, ’symptoms’);
You use an ASP.NET control to retrieve the title of a
Web page. The control does not have the
DataSource property. The following is
a sample code that can be used to retrieve titles of Web pages:<asp:TextBox id=”txtBox” runat=”server” Text=’<%#Eval(“Title”)%>’/>Microsoft Visual C# codetxtBox.DataBind();Microsoft Visual Basic .NET codetxtBox.DataBind()Cause of problem in situation 1
2, ’cause’);
When the DropDownList instance calls the DataBind method,
the SelectedValue property tries to obtain the value
that is in the data
binding
context
of the DetailsView control. However, the value is not
included in the
data binding
context
of the DetailsView control.
Therefore, the error occurs.Cause of problem
in situation 2
2, ’cause’);
The ASP.NET controls
that do not have the DataSource property can be used
only in
the context of data presentation controls. Examples
of data presentation controls include the DetailsView control and
the GridView control. Workaround for problem in situation 1
2, ‘workaround’);
Delete
the source code in the SelectedIndexChanged event of the DropDownList control. When
you select an item from the DropDownList control
and the item is in edit mode, the item is automatically bound
to the SelectedValue property. Then,
the value of the item
is automatically
updated in the DataTable object.
Note You cannot find the SelectedValue property by using the IntelliSense feature.
However, you can write
the property manually. Workaround for problem in situation 2
2, ‘workaround’);
Use
the following code to work
around this problem: <asp:TextBox id=”txtBox” runat=”server” Text=’<%#Page.Title%>’/>

For File Repair and Data Recovery, visit File Repair / Data Recovery