In my ASP.NET 4.5 WebForms app, In my ListView I am trying to bind DropDownList, but am not able to do it. Actually, the Model that is bind to ListView, has a Property of UnitdirectionId & on Edit & Insert Templates' I want to show the dropdown of all UnitDirection table.
Here is my EditTempalte :
<EditItemTemplate>
<tr>
<td>
<asp:TextBox ID="unitDirTxt" runat="server" Text='<%# Bind("UnitDirectionId") %>'></asp:TextBox>
<!-- <asp:DynamicControl runat="server" DataField="UnitDirectionId" ID="UnitDirectionId" Mode="Edit" /> -->
<asp:DropDownList ID="unitDirEditDrop" runat="server" ItemType="VincitoreCRMApplication.Models.UnitDirection"
DataTextField="DirectionTitle" DataValueField="UnitDirectionId"
SelectMethod="GetUnitDirections"></asp:DropDownList>
</td>
</tr>
</EditItemTemplate>
In Code behind,
public IQueryable<UnitDirection> GetUnitDirections()
{
return _db.UnitDirections;
}
Now, the Model of LsitView has the proerpty as follows :
[Display(Name= "Unit Direction")]
public int UnitDirectionId { get; set; }
[ForeignKey("UnitDirectionId")]
public virtual UnitDirection UnitDirection { get; set; }
Now, the UnitDirection Model :
public class UnitDirection
{
[ScaffoldColumn(false)]
public int UnitDirectionId { get; set; }
[Required]
[Display]
public string DirectionTitle { get; set; }
}
ERROR that I get is :
########## ERRO : An error occurred while executing the command definition. See the inner exception for details.
STACK : at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteSto reCommands(EntityCommand entityCommand, CommandBehavior behavior)
at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResul tType](ObjectContext context, ObjectParameterCollection parameterValues)
at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__6()
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__5()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
at System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable dataSource)
at System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e)
at System.Web.UI.WebControls.ListControl.PerformSelect()
at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
at System.Web.UI.Control.DataBindChildren()
at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding)
at System.Web.UI.Control.DataBind()
at System.Web.UI.WebControls.ListView.CreateItemsWithoutGroups(ListViewPagedDataSource dataSource, Boolean dataBinding, InsertItemPosition insertPosition, ArrayList keyArray)
at System.Web.UI.WebControls.ListView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding)
at System.Web.UI.WebControls.ListView.PerformDataBinding(IEnumerable data)
at System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data)
at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)
at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
at System.Web.UI.WebControls.ListView.PerformSelect()
at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
at System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound()
at System.Web.UI.WebControls.BaseDataBoundControl.OnPreRender(EventArgs e)
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
In db the UnitDirection table has 3 items.
With the above drop down, I believe I should atleast get list of all UnitDirection in drop down. But on that I am getting error. Then comes to set the selected item in Edit Template.
Can you please help me identify why I am getting this error ?? Any help is highly appreciated.
Thanks
As we discuss in the stackoverflow chat, you are facing below error in your drop down binding method.
Please add
MultipleActiveResultSets=true
in your web.config connection string to solve your problem.For fetch you UnitDirection value, you need to write one method like below and return UnitDirection title as return type.
after that call above method in your ItemDataBound event,
Please let me know if you have any questions.